PageRenderTime 27ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/creathemup_sdl/src/CBoatEnemyBehaviour.cpp

http://creathemup.googlecode.com/
C++ | 652 lines | 453 code | 156 blank | 43 comment | 2 complexity | b448555efeed745fbf4ca4257ac10b34 MD5 | raw file
  1. #include "../include/CBoatEnemyBehaviour.h"
  2. #include "../include/CDefaultShotBehaviour.h"
  3. #include "../include/CBombShotBehaviour.h"
  4. #include "../include/CSprite.h"
  5. #include "../include/CSpriteAnimation.h"
  6. #include <SDL/SDL.h>
  7. #include <math.h>
  8. //**************************
  9. //Description : Constructor
  10. //Parameters : Value for m_iX, m_iY,
  11. // m_iSpeedX, m_iSpeedY,
  12. // m_iWidth, m_iHeight,
  13. // the ratio for the collision box
  14. // the type of colision
  15. // m_iLives
  16. // m_iNbPoints
  17. //Note : None
  18. //**************************
  19. CBoatEnemyBehaviour::CBoatEnemyBehaviour(int _iX, int _iY,
  20. int _iSpeedX, int _iSpeedY,
  21. int _iWidth, int _iHeight,
  22. float _fRatioW, float _fRatioH,
  23. TYPE_COLLISION _iTypeCollision,
  24. int _iLives,
  25. int _iNbPoints)
  26. : CDefaultEnemyBehaviour(_iX, _iY,
  27. _iSpeedX, _iSpeedY,
  28. _iWidth, _iHeight,
  29. _fRatioW, _fRatioW,
  30. _iTypeCollision,
  31. _iLives,
  32. _iNbPoints)
  33. {
  34. //this enemy only have a primary shot
  35. m_iPrimaryShot = DEFAULTSHOT;
  36. m_iSecondaryShot = -1;
  37. m_iTimeBetweenPrimaryShot = 800;
  38. m_iTimeBetweenSecondaryShot = -1;
  39. }
  40. //**************************
  41. //Description : Copy Constructor
  42. //Parameters : A CBoatEnemyBehaviour
  43. //Note : None
  44. //**************************
  45. CBoatEnemyBehaviour::CBoatEnemyBehaviour(CBoatEnemyBehaviour & _BoatEnemy)
  46. : CDefaultEnemyBehaviour(_BoatEnemy)
  47. {
  48. }
  49. //**************************
  50. //Description : Destructor
  51. //Parameters : None
  52. //Note : None
  53. //**************************
  54. CBoatEnemyBehaviour::~CBoatEnemyBehaviour()
  55. {
  56. }
  57. //**************************
  58. //Description : The entity shoot
  59. //Parameters : The tab of animations shot and the screen
  60. //Return Value : True if the enemy has shot, otherwise false
  61. //Note : Virtual because the way of shooting depends on the entity
  62. //**************************
  63. bool CBoatEnemyBehaviour::Shoot(CSpriteAnimation * _SpriteAnimationShot, SDL_Surface * _screen)
  64. {
  65. if ( ( (Uint32)(m_lLastPrimaryShoot + m_iTimeBetweenPrimaryShot) < SDL_GetTicks() ) )
  66. {
  67. CDefaultShotBehaviour * Shot;
  68. switch ( m_iPrimaryShot )
  69. {
  70. case DEFAULTSHOT:
  71. {
  72. //upper right side
  73. Shot = new CDefaultShotBehaviour(m_iX,
  74. m_iY,
  75. 0,
  76. -4,
  77. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  78. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  79. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  80. m_vShots->push_back( Shot );
  81. Shot = new CDefaultShotBehaviour(m_iX,
  82. m_iY,
  83. 1,
  84. -4,
  85. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  86. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  87. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  88. m_vShots->push_back( Shot );
  89. Shot = new CDefaultShotBehaviour(m_iX,
  90. m_iY,
  91. 2,
  92. -4,
  93. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  94. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  95. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  96. m_vShots->push_back( Shot );
  97. Shot = new CDefaultShotBehaviour(m_iX,
  98. m_iY,
  99. 3,
  100. -3,
  101. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  102. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  103. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  104. m_vShots->push_back( Shot );
  105. Shot = new CDefaultShotBehaviour(m_iX,
  106. m_iY,
  107. 4,
  108. -2,
  109. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  110. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  111. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  112. m_vShots->push_back( Shot );
  113. Shot = new CDefaultShotBehaviour(m_iX,
  114. m_iY,
  115. 4,
  116. -1,
  117. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  118. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  119. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  120. m_vShots->push_back( Shot );
  121. //lower right side
  122. Shot = new CDefaultShotBehaviour(m_iX,
  123. m_iY,
  124. 4,
  125. 0,
  126. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  127. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  128. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  129. m_vShots->push_back( Shot );
  130. Shot = new CDefaultShotBehaviour(m_iX,
  131. m_iY,
  132. 4,
  133. 1,
  134. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  135. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  136. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  137. m_vShots->push_back( Shot );
  138. Shot = new CDefaultShotBehaviour(m_iX,
  139. m_iY,
  140. 4,
  141. 2,
  142. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  143. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  144. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  145. m_vShots->push_back( Shot );
  146. Shot = new CDefaultShotBehaviour(m_iX,
  147. m_iY,
  148. 3,
  149. 3,
  150. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  151. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  152. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  153. m_vShots->push_back( Shot );
  154. Shot = new CDefaultShotBehaviour(m_iX,
  155. m_iY,
  156. 2,
  157. 4,
  158. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  159. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  160. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  161. m_vShots->push_back( Shot );
  162. Shot = new CDefaultShotBehaviour(m_iX,
  163. m_iY,
  164. 1,
  165. 4,
  166. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  167. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  168. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  169. m_vShots->push_back( Shot );
  170. //upper left side
  171. Shot = new CDefaultShotBehaviour(m_iX,
  172. m_iY,
  173. -1,
  174. -4,
  175. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  176. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  177. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  178. m_vShots->push_back( Shot );
  179. Shot = new CDefaultShotBehaviour(m_iX,
  180. m_iY,
  181. -2,
  182. -4,
  183. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  184. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  185. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  186. m_vShots->push_back( Shot );
  187. Shot = new CDefaultShotBehaviour(m_iX,
  188. m_iY,
  189. -3,
  190. -3,
  191. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  192. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  193. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  194. m_vShots->push_back( Shot );
  195. Shot = new CDefaultShotBehaviour(m_iX,
  196. m_iY,
  197. -4,
  198. -2,
  199. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  200. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  201. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  202. m_vShots->push_back( Shot );
  203. Shot = new CDefaultShotBehaviour(m_iX,
  204. m_iY,
  205. -4,
  206. -1,
  207. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  208. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  209. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  210. m_vShots->push_back( Shot );
  211. //lower left side
  212. Shot = new CDefaultShotBehaviour(m_iX,
  213. m_iY,
  214. -4,
  215. 0,
  216. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  217. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  218. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  219. m_vShots->push_back( Shot );
  220. Shot = new CDefaultShotBehaviour(m_iX,
  221. m_iY,
  222. -4,
  223. 1,
  224. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  225. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  226. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  227. m_vShots->push_back( Shot );
  228. Shot = new CDefaultShotBehaviour(m_iX,
  229. m_iY,
  230. -4,
  231. 2,
  232. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  233. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  234. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  235. m_vShots->push_back( Shot );
  236. Shot = new CDefaultShotBehaviour(m_iX,
  237. m_iY,
  238. -3,
  239. 3,
  240. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  241. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  242. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  243. m_vShots->push_back( Shot );
  244. Shot = new CDefaultShotBehaviour(m_iX,
  245. m_iY,
  246. -2,
  247. 4,
  248. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  249. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  250. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  251. m_vShots->push_back( Shot );
  252. Shot = new CDefaultShotBehaviour(m_iX,
  253. m_iY,
  254. -1,
  255. 4,
  256. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  257. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  258. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  259. m_vShots->push_back( Shot );
  260. Shot = new CDefaultShotBehaviour(m_iX,
  261. m_iY,
  262. 0,
  263. 4,
  264. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  265. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  266. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  267. m_vShots->push_back( Shot );
  268. break;
  269. }
  270. case BOMB:
  271. {
  272. //upper right side
  273. Shot = new CBombShotBehaviour(m_iX,
  274. m_iY,
  275. 0,
  276. -4,
  277. _SpriteAnimationShot[BOMB].GetWidth(),
  278. _SpriteAnimationShot[BOMB].GetHeight());
  279. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  280. m_vShots->push_back( Shot );
  281. Shot = new CBombShotBehaviour(m_iX,
  282. m_iY,
  283. 1,
  284. -4,
  285. _SpriteAnimationShot[BOMB].GetWidth(),
  286. _SpriteAnimationShot[BOMB].GetHeight());
  287. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  288. m_vShots->push_back( Shot );
  289. Shot = new CBombShotBehaviour(m_iX,
  290. m_iY,
  291. 2,
  292. -4,
  293. _SpriteAnimationShot[BOMB].GetWidth(),
  294. _SpriteAnimationShot[BOMB].GetHeight());
  295. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  296. m_vShots->push_back( Shot );
  297. Shot = new CBombShotBehaviour(m_iX,
  298. m_iY,
  299. 3,
  300. -3,
  301. _SpriteAnimationShot[BOMB].GetWidth(),
  302. _SpriteAnimationShot[BOMB].GetHeight());
  303. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  304. m_vShots->push_back( Shot );
  305. Shot = new CBombShotBehaviour(m_iX,
  306. m_iY,
  307. 4,
  308. -2,
  309. _SpriteAnimationShot[BOMB].GetWidth(),
  310. _SpriteAnimationShot[BOMB].GetHeight());
  311. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  312. m_vShots->push_back( Shot );
  313. Shot = new CBombShotBehaviour(m_iX,
  314. m_iY,
  315. 4,
  316. -1,
  317. _SpriteAnimationShot[BOMB].GetWidth(),
  318. _SpriteAnimationShot[BOMB].GetHeight());
  319. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  320. m_vShots->push_back( Shot );
  321. //lower right side
  322. Shot = new CBombShotBehaviour(m_iX,
  323. m_iY,
  324. 4,
  325. 0,
  326. _SpriteAnimationShot[BOMB].GetWidth(),
  327. _SpriteAnimationShot[BOMB].GetHeight());
  328. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  329. m_vShots->push_back( Shot );
  330. Shot = new CBombShotBehaviour(m_iX,
  331. m_iY,
  332. 4,
  333. 1,
  334. _SpriteAnimationShot[BOMB].GetWidth(),
  335. _SpriteAnimationShot[BOMB].GetHeight());
  336. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  337. m_vShots->push_back( Shot );
  338. Shot = new CBombShotBehaviour(m_iX,
  339. m_iY,
  340. 4,
  341. 2,
  342. _SpriteAnimationShot[BOMB].GetWidth(),
  343. _SpriteAnimationShot[BOMB].GetHeight());
  344. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  345. m_vShots->push_back( Shot );
  346. Shot = new CBombShotBehaviour(m_iX,
  347. m_iY,
  348. 3,
  349. 3,
  350. _SpriteAnimationShot[BOMB].GetWidth(),
  351. _SpriteAnimationShot[BOMB].GetHeight());
  352. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  353. m_vShots->push_back( Shot );
  354. Shot = new CBombShotBehaviour(m_iX,
  355. m_iY,
  356. 2,
  357. 4,
  358. _SpriteAnimationShot[BOMB].GetWidth(),
  359. _SpriteAnimationShot[BOMB].GetHeight());
  360. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  361. m_vShots->push_back( Shot );
  362. Shot = new CBombShotBehaviour(m_iX,
  363. m_iY,
  364. 1,
  365. 4,
  366. _SpriteAnimationShot[BOMB].GetWidth(),
  367. _SpriteAnimationShot[BOMB].GetHeight());
  368. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  369. m_vShots->push_back( Shot );
  370. //upper left side
  371. Shot = new CBombShotBehaviour(m_iX,
  372. m_iY,
  373. -1,
  374. -4,
  375. _SpriteAnimationShot[BOMB].GetWidth(),
  376. _SpriteAnimationShot[BOMB].GetHeight());
  377. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  378. m_vShots->push_back( Shot );
  379. Shot = new CBombShotBehaviour(m_iX,
  380. m_iY,
  381. -2,
  382. -4,
  383. _SpriteAnimationShot[BOMB].GetWidth(),
  384. _SpriteAnimationShot[BOMB].GetHeight());
  385. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  386. m_vShots->push_back( Shot );
  387. Shot = new CBombShotBehaviour(m_iX,
  388. m_iY,
  389. -3,
  390. -3,
  391. _SpriteAnimationShot[BOMB].GetWidth(),
  392. _SpriteAnimationShot[BOMB].GetHeight());
  393. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  394. m_vShots->push_back( Shot );
  395. Shot = new CBombShotBehaviour(m_iX,
  396. m_iY,
  397. -4,
  398. -2,
  399. _SpriteAnimationShot[BOMB].GetWidth(),
  400. _SpriteAnimationShot[BOMB].GetHeight());
  401. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  402. m_vShots->push_back( Shot );
  403. Shot = new CBombShotBehaviour(m_iX,
  404. m_iY,
  405. -4,
  406. -1,
  407. _SpriteAnimationShot[BOMB].GetWidth(),
  408. _SpriteAnimationShot[BOMB].GetHeight());
  409. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  410. m_vShots->push_back( Shot );
  411. //lower left side
  412. Shot = new CBombShotBehaviour(m_iX,
  413. m_iY,
  414. -4,
  415. 0,
  416. _SpriteAnimationShot[BOMB].GetWidth(),
  417. _SpriteAnimationShot[BOMB].GetHeight());
  418. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  419. m_vShots->push_back( Shot );
  420. Shot = new CBombShotBehaviour(m_iX,
  421. m_iY,
  422. -4,
  423. 1,
  424. _SpriteAnimationShot[BOMB].GetWidth(),
  425. _SpriteAnimationShot[BOMB].GetHeight());
  426. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  427. m_vShots->push_back( Shot );
  428. Shot = new CBombShotBehaviour(m_iX,
  429. m_iY,
  430. -4,
  431. 2,
  432. _SpriteAnimationShot[BOMB].GetWidth(),
  433. _SpriteAnimationShot[BOMB].GetHeight());
  434. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  435. m_vShots->push_back( Shot );
  436. Shot = new CBombShotBehaviour(m_iX,
  437. m_iY,
  438. -3,
  439. 3,
  440. _SpriteAnimationShot[BOMB].GetWidth(),
  441. _SpriteAnimationShot[BOMB].GetHeight());
  442. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  443. m_vShots->push_back( Shot );
  444. Shot = new CBombShotBehaviour(m_iX,
  445. m_iY,
  446. -2,
  447. 4,
  448. _SpriteAnimationShot[BOMB].GetWidth(),
  449. _SpriteAnimationShot[BOMB].GetHeight());
  450. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  451. m_vShots->push_back( Shot );
  452. Shot = new CBombShotBehaviour(m_iX,
  453. m_iY,
  454. -1,
  455. 4,
  456. _SpriteAnimationShot[BOMB].GetWidth(),
  457. _SpriteAnimationShot[BOMB].GetHeight());
  458. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  459. m_vShots->push_back( Shot );
  460. Shot = new CBombShotBehaviour(m_iX,
  461. m_iY,
  462. 0,
  463. 4,
  464. _SpriteAnimationShot[BOMB].GetWidth(),
  465. _SpriteAnimationShot[BOMB].GetHeight());
  466. Shot->SetSprite( &_SpriteAnimationShot[BOMB], _screen );
  467. m_vShots->push_back( Shot );
  468. break;
  469. }
  470. default:
  471. {
  472. Shot = new CDefaultShotBehaviour(m_iX, m_iY,
  473. 0, 4,
  474. _SpriteAnimationShot[DEFAULTSHOT].GetWidth(),
  475. _SpriteAnimationShot[DEFAULTSHOT].GetHeight());
  476. Shot->SetSprite( &_SpriteAnimationShot[DEFAULTSHOT], _screen );
  477. m_vShots->push_back( Shot );
  478. }
  479. }
  480. m_lLastPrimaryShoot = SDL_GetTicks();
  481. return true;
  482. }
  483. else
  484. return false;
  485. }
  486. //**************************
  487. //Description : Update the trajectory of the entity
  488. //Parameters : None
  489. //Return Value : None
  490. //Note : Virtual because actions depends on the entity type
  491. //**************************
  492. void CBoatEnemyBehaviour::EntityTrajectory()
  493. {
  494. //we move on Y
  495. m_iY += m_iSpeedY;
  496. }