You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IaGhost.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #include "..\C-Engine\CHeader.hpp"
  2. #include "GUI.hpp"
  3. #include "IaGhost.hpp"
  4. void IAG::Start()
  5. {
  6. direction=0;
  7. directionuser=0;
  8. couldown = 0;
  9. }
  10. void IAG::Initialisation(Script_GUI * v)
  11. {
  12. SGUI = v ;
  13. }
  14. void IAG::SetGhost(int v)
  15. {
  16. ghost = v;
  17. }
  18. void IAG::Update()
  19. {
  20. if(SGUI->GetVulnerability() > 0)
  21. {
  22. SetIt(1);
  23. couldown = 0;
  24. }
  25. else SetIt(0);
  26. if(GetObject()->CollisionTag("Jail"))
  27. {
  28. ScriptJail();
  29. }
  30. else
  31. {
  32. switch(ghost)
  33. {
  34. case 1: ScriptRed();break;
  35. case 2: ScriptYellow();break;
  36. case 3: ScriptBlue();break;
  37. case 4: ScriptPink();break;
  38. }
  39. }
  40. UpdateDirection();
  41. }
  42. void IAG::InvDirection()
  43. {
  44. switch(direction)
  45. {
  46. case 1: direction = 3; directionuser = 3; break;
  47. case 2: direction = 4; directionuser = 4; break;
  48. case 3: direction = 1; directionuser = 1; break;
  49. case 4: direction = 2; directionuser = 2; break;
  50. }
  51. if(recur < 2)UpdateDirection();
  52. }
  53. void IAG::UpdateDirection()
  54. {
  55. if(!GetObject()->GetRigibody()->CollisionDecor( GetX() + (directionuser == 2) - (directionuser == 4) , GetY() + (directionuser == 1) - (directionuser == 3) ))direction = directionuser;
  56. if(couldown <= 1)
  57. {
  58. recur ++;
  59. switch(direction)
  60. {
  61. case 1: if(!GetObject()->CollisionTag("Ghost",0,1))Move(0,1);else InvDirection(); break;
  62. case 2: if(!GetObject()->CollisionTag("Ghost",1,0))Move(1,0);else InvDirection(); break;
  63. case 3: if(!GetObject()->CollisionTag("Ghost",0,-1))Move(0,-1);else InvDirection(); break;
  64. case 4: if(!GetObject()->CollisionTag("Ghost",-1,0))Move(-1,0);else InvDirection(); break;
  65. }
  66. recur = 0;
  67. }
  68. //************Teleportation*************//
  69. if(GetObject()->CollisionTag("TP1"))
  70. {
  71. GetObject()->GetTransforms()->SetXY(229,138);
  72. direction = 4;
  73. }
  74. if(GetObject()->CollisionTag("TP2"))
  75. {
  76. GetObject()->GetTransforms()->SetXY(2,138);
  77. direction = 2;
  78. }
  79. }
  80. void IAG::ScriptJail()
  81. {
  82. if(GetObject()->GetRigibody()->CollisionDecor(GetX() , GetY()+1))
  83. {
  84. Object * Jail = GetObject()->CollisionTagO("Jail");
  85. if(Jail != NULL)
  86. {
  87. if(Jail->GetTransforms()->GetX() + 20 < GetX())directionuser = 4;
  88. else directionuser = 2;
  89. }
  90. }
  91. else directionuser = 1;
  92. }
  93. void IAG::ScriptRed()
  94. {
  95. int difx = GetEngine()->GetListeObject()[0]->GetTransforms()->GetX() - GetX();
  96. int dify = GetEngine()->GetListeObject()[0]->GetTransforms()->GetY() - GetY();
  97. if(couldown <= 0)
  98. {
  99. if(!GetObject()->GetRigibody()->CollisionDecor(GetX() + (direction%2), GetY() + (!(direction%2))) || !GetObject()->GetRigibody()->CollisionDecor(GetX() - (direction%2), GetY() - (!(direction%2))))
  100. {
  101. bool possibledirection[4];
  102. for(int i = 0; i < 4 ; i++)possibledirection[i] = !(GetObject()->GetRigibody()->CollisionDecor(GetX() + (i == 1) - (i == 3), GetY() + (i == 0) - (i == 2)));
  103. if(GetObject()->CollisionTag("Jail",0,-1))possibledirection[2] = false;
  104. int betterdirection[4]; int best;
  105. if(ABS(difx) > ABS(dify))best = 1;
  106. else best = 2;
  107. if(difx == 0)best = 2;
  108. if(dify == 0)best = 1;
  109. if(difx > 0)
  110. {
  111. betterdirection[ (best == 2)] = 2;
  112. betterdirection[ 3 -(best == 2)] = 4;
  113. }
  114. else
  115. {
  116. betterdirection[ (best == 2)] = 4;
  117. betterdirection[ 3 -(best == 2)] = 2;
  118. }
  119. if(dify > 0)
  120. {
  121. betterdirection[ (best == 1)] = 1;
  122. betterdirection[ 3 -(best == 1)] = 3;
  123. }
  124. else
  125. {
  126. betterdirection[ (best == 1)] = 3;
  127. betterdirection[ 3 -(best == 1)] = 1;
  128. }
  129. if(SGUI->GetVulnerability() > 0)
  130. {
  131. bool buffer[4];
  132. for(int i = 0; i < 4 ; i++)buffer[i] = betterdirection[i];
  133. for(int i = 0; i < 4 ; i++)betterdirection[i] = buffer[3 - i];
  134. }
  135. int end = 4;
  136. while(end > 0)
  137. {
  138. if(possibledirection[betterdirection[4 - end] - 1])
  139. {
  140. directionuser = betterdirection[4 - end];
  141. end = 0;
  142. }
  143. else end --;
  144. }
  145. couldown = rand_int(10);
  146. }
  147. }
  148. else couldown --;
  149. }
  150. void IAG::ScriptYellow()
  151. {
  152. int hasard;
  153. if(!(!GetObject()->GetRigibody()->CollisionDecor(GetX(), GetY() + 1) && !GetObject()->GetRigibody()->CollisionDecor(GetX() , GetY() - 1) &&!GetObject()->GetRigibody()->CollisionDecor(GetX() + 1 , GetY()) && !GetObject()->GetRigibody()->CollisionDecor(GetX() - 1 , GetY())))
  154. {
  155. switch(direction)
  156. {
  157. case 1:
  158. if(!GetObject()->GetRigibody()->CollisionDecor(GetX(), GetY() + 1) || !GetObject()->GetRigibody()->CollisionDecor(GetX() , GetY() - 1))
  159. {
  160. hasard = rand_int(3) + 1;
  161. switch(hasard)
  162. {
  163. case 1: directionuser = 1;break;
  164. case 2: directionuser = 2;break;
  165. case 3: directionuser = 4;break;
  166. }
  167. }
  168. break;
  169. case 2:
  170. if(!GetObject()->GetRigibody()->CollisionDecor(GetX() + 1 , GetY()) || !GetObject()->GetRigibody()->CollisionDecor(GetX() - 1 , GetY()))
  171. {
  172. hasard = rand_int(3) + 1;
  173. switch(hasard)
  174. {
  175. case 1: directionuser = 2;break;
  176. case 2: directionuser = 1;break;
  177. case 3: directionuser = 3;break;
  178. }
  179. }
  180. break;
  181. case 3:
  182. if(!GetObject()->GetRigibody()->CollisionDecor(GetX(), GetY() + 1) || !GetObject()->GetRigibody()->CollisionDecor(GetX(), GetY() - 1))
  183. {
  184. hasard = rand_int(3) + 1;
  185. switch(hasard)
  186. {
  187. case 1: directionuser = 3;break;
  188. case 2: directionuser = 2;break;
  189. case 3: directionuser = 4;break;
  190. }
  191. }
  192. break;
  193. case 4:
  194. if(!GetObject()->GetRigibody()->CollisionDecor(GetX() + 1 , GetY()) || !GetObject()->GetRigibody()->CollisionDecor(GetX() - 1 , GetY()))
  195. {
  196. hasard = rand_int(3) + 1;
  197. switch(hasard)
  198. {
  199. case 1: directionuser = 4;break;
  200. case 2: directionuser = 1;break;
  201. case 3: directionuser = 3;break;
  202. }
  203. }
  204. break;
  205. }
  206. }
  207. }
  208. void IAG::ScriptBlue()
  209. {
  210. int difx = GetEngine()->GetListeObject()[0]->GetTransforms()->GetX() - GetX();
  211. int dify = GetEngine()->GetListeObject()[0]->GetTransforms()->GetY() - GetY();
  212. if(couldown <= 0)
  213. {
  214. if(!GetObject()->GetRigibody()->CollisionDecor(GetX() + (direction%2), GetY() + (!(direction%2))) || !GetObject()->GetRigibody()->CollisionDecor(GetX() - (direction%2), GetY() - (!(direction%2))))
  215. {
  216. bool possibledirection[4];
  217. for(int i = 0; i < 4 ; i++)possibledirection[i] = !(GetObject()->GetRigibody()->CollisionDecor(GetX() + (i == 1) - (i == 3), GetY() + (i == 0) - (i == 2)));
  218. if(GetObject()->CollisionTag("Jail",0,-1))possibledirection[2] = false;
  219. int betterdirection[4]; int best;
  220. if(ABS(difx) > ABS(dify))best = 1;
  221. else best = 2;
  222. if(difx == 0)best = 2;
  223. if(dify == 0)best = 1;
  224. if(difx > 0)
  225. {
  226. betterdirection[ (best == 2)] = 4;
  227. betterdirection[ 3 -(best == 2)] = 2;
  228. }
  229. else
  230. {
  231. betterdirection[ (best == 2)] = 2;
  232. betterdirection[ 3 -(best == 2)] = 4;
  233. }
  234. if(dify > 0)
  235. {
  236. betterdirection[ (best == 1)] = 3;
  237. betterdirection[ 3 -(best == 1)] = 1;
  238. }
  239. else
  240. {
  241. betterdirection[ (best == 1)] = 1;
  242. betterdirection[ 3 -(best == 1)] = 3;
  243. }
  244. if(SGUI->GetVulnerability() > 0)
  245. {
  246. bool buffer[4];
  247. for(int i = 0; i < 4 ; i++)buffer[i] = betterdirection[i];
  248. for(int i = 0; i < 4 ; i++)betterdirection[i] = buffer[3 - i];
  249. }
  250. int end = 4;
  251. while(end > 0)
  252. {
  253. if(possibledirection[betterdirection[4 - end] - 1])
  254. {
  255. directionuser = betterdirection[4 - end];
  256. end = 0;
  257. }
  258. else end --;
  259. }
  260. couldown = rand_int(10);
  261. }
  262. }
  263. else couldown --;
  264. }
  265. void IAG::ScriptPink()
  266. {
  267. int px , py , bx , by;
  268. px = GetEngine()->GetListeObject()[0]->GetTransforms()->GetX();
  269. py = GetEngine()->GetListeObject()[0]->GetTransforms()->GetY();
  270. bx = px;
  271. by = py;
  272. bool end = false;
  273. while(end)
  274. {
  275. switch(*pacmandirection)
  276. {
  277. case 1: by ++; break;
  278. case 2: bx ++; break;
  279. case 3: by --; break;
  280. case 4: bx --; break;
  281. }
  282. if(GetEngine()->GetListeObject()[0]->GetRigibody()->CollisionDecor(bx , by))end = true;
  283. }
  284. int difx = bx - GetX();
  285. int dify = by - GetY();
  286. if(couldown <= 0)
  287. {
  288. if(!GetObject()->GetRigibody()->CollisionDecor(GetX() + (direction%2), GetY() + (!(direction%2))) || !GetObject()->GetRigibody()->CollisionDecor(GetX() - (direction%2), GetY() - (!(direction%2))))
  289. {
  290. bool possibledirection[4];
  291. for(int i = 0; i < 4 ; i++)possibledirection[i] = !(GetObject()->GetRigibody()->CollisionDecor(GetX() + (i == 1) - (i == 3), GetY() + (i == 0) - (i == 2)));
  292. if(GetObject()->CollisionTag("Jail",0,-1))possibledirection[2] = false;
  293. int betterdirection[4]; int best;
  294. if(ABS(difx) > ABS(dify))best = 1;
  295. else best = 2;
  296. if(difx == 0)best = 2;
  297. if(dify == 0)best = 1;
  298. if(difx > 0)
  299. {
  300. betterdirection[ (best == 2)] = 2;
  301. betterdirection[ 3 -(best == 2)] = 4;
  302. }
  303. else
  304. {
  305. betterdirection[ (best == 2)] = 4;
  306. betterdirection[ 3 -(best == 2)] = 2;
  307. }
  308. if(dify > 0)
  309. {
  310. betterdirection[ (best == 1)] = 1;
  311. betterdirection[ 3 -(best == 1)] = 3;
  312. }
  313. else
  314. {
  315. betterdirection[ (best == 1)] = 3;
  316. betterdirection[ 3 -(best == 1)] = 1;
  317. }
  318. if(SGUI->GetVulnerability() > 0)
  319. {
  320. bool buffer[4];
  321. for(int i = 0; i < 4 ; i++)buffer[i] = betterdirection[i];
  322. for(int i = 0; i < 4 ; i++)betterdirection[i] = buffer[3 - i];
  323. }
  324. int end = 4;
  325. while(end > 0)
  326. {
  327. if(possibledirection[betterdirection[4 - end] - 1])
  328. {
  329. directionuser = betterdirection[4 - end];
  330. end = 0;
  331. }
  332. else end --;
  333. }
  334. couldown = rand_int(10);
  335. }
  336. }
  337. else couldown --;
  338. }