Invader.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "Invader.h"
  28. #include <Rocket/Core/Math.h>
  29. #include <Shell.h>
  30. #include <ShellOpenGL.h>
  31. #include "Defender.h"
  32. #include "Game.h"
  33. #include "GameDetails.h"
  34. #include "Shield.h"
  35. #include "Sprite.h"
  36. const float BOMB_UPDATE_FREQ = 0.04f;
  37. const float BOMB_RAY_SPEED = 10;
  38. const float BOMB_MISSILE_SPEED = 7;
  39. const float BOMB_PROBABILITY_EASY = 0.002f;
  40. const float BOMB_PROBABILITY_HARD = 0.005f;
  41. const float EXPLOSION_TIME = 0.25f;
  42. const Rocket::Core::Colourb MOTHERSHIP_COLOUR = Rocket::Core::Colourb(255, 0, 0, 255);
  43. Sprite invader_sprites[] =
  44. {
  45. // Rank 1
  46. Sprite(Rocket::Core::Vector2f(48, 32), Rocket::Core::Vector2f(0.609375f, 0), Rocket::Core::Vector2f(0.796875f, 0.5f)),
  47. Sprite(Rocket::Core::Vector2f(48, 32), Rocket::Core::Vector2f(0.80078125f, 0), Rocket::Core::Vector2f(0.98828125f, 0.5f)),
  48. // Rank 2
  49. Sprite(Rocket::Core::Vector2f(44, 32), Rocket::Core::Vector2f(0.2578125f, 0), Rocket::Core::Vector2f(0.4296875f, 0.5f)),
  50. Sprite(Rocket::Core::Vector2f(44, 32), Rocket::Core::Vector2f(0.43359375f, 0), Rocket::Core::Vector2f(0.60546875f, 0.5f)),
  51. // Rank 3
  52. Sprite(Rocket::Core::Vector2f(32, 32), Rocket::Core::Vector2f(0, 0), Rocket::Core::Vector2f(0.125f, 0.5f)),
  53. Sprite(Rocket::Core::Vector2f(32, 32), Rocket::Core::Vector2f(0.12890625f, 0), Rocket::Core::Vector2f(0.25390625f, 0.5f)),
  54. // Mothership
  55. Sprite(Rocket::Core::Vector2f(64, 28), Rocket::Core::Vector2f(0.23828125f, 0.515625f), Rocket::Core::Vector2f(0.48828125f, 0.953125f)),
  56. // Explosion
  57. Sprite(Rocket::Core::Vector2f(52, 28), Rocket::Core::Vector2f(0.71484375f, 0.51562500f), Rocket::Core::Vector2f(0.91796875f, 0.95312500f))
  58. };
  59. Sprite bomb_sprites[] =
  60. {
  61. // Ray
  62. Sprite(Rocket::Core::Vector2f(12, 20), Rocket::Core::Vector2f(0.51171875f, 0.51562500f), Rocket::Core::Vector2f(0.55859375f, 0.82812500f)),
  63. Sprite(Rocket::Core::Vector2f(12, 20), Rocket::Core::Vector2f(0.56250000, 0.51562500), Rocket::Core::Vector2f(0.60937500, 0.82812500)),
  64. Sprite(Rocket::Core::Vector2f(12, 20), Rocket::Core::Vector2f(0.61328125, 0.51562500), Rocket::Core::Vector2f(0.66015625, 0.82812500)),
  65. Sprite(Rocket::Core::Vector2f(12, 20), Rocket::Core::Vector2f(0.66406250, 0.51562500), Rocket::Core::Vector2f(0.71093750, 0.82812500)),
  66. // Missile
  67. Sprite(Rocket::Core::Vector2f(12, 20), Rocket::Core::Vector2f(0.92578125, 0.51562500), Rocket::Core::Vector2f(0.97265625, 0.82812500))
  68. };
  69. Invader::Invader(Game* _game, InvaderType _type, int _index) : position(0,0)
  70. {
  71. type = UNKNOWN;
  72. animation_frame = 0;
  73. bomb_animation_frame = 0;
  74. bomb_frame_start = 0;
  75. death_time = 0;
  76. state = ALIVE;
  77. bomb = NONE;
  78. game = _game;
  79. type = _type;
  80. death_time = 0;
  81. invader_index = _index;
  82. bomb_probability = GameDetails::GetDifficulty() == GameDetails::EASY ? BOMB_PROBABILITY_EASY : BOMB_PROBABILITY_HARD;
  83. bomb_probability *= type;
  84. }
  85. Invader::~Invader()
  86. {
  87. }
  88. void Invader::SetPosition(const Rocket::Core::Vector2f& _position)
  89. {
  90. position = _position;
  91. }
  92. const Rocket::Core::Vector2f& Invader::GetPosition() const
  93. {
  94. return position;
  95. }
  96. void Invader::Update()
  97. {
  98. // Update the bombs
  99. if (Shell::GetElapsedTime() - bomb_frame_start > BOMB_UPDATE_FREQ)
  100. {
  101. // Update the bomb position if its in flight, or check if we should drop one
  102. if (bomb != NONE)
  103. {
  104. if (bomb == RAY)
  105. {
  106. bomb_animation_frame++;
  107. if (bomb_animation_frame > 3)
  108. bomb_animation_frame = 0;
  109. bomb_position.y += BOMB_RAY_SPEED;
  110. }
  111. else
  112. {
  113. bomb_position.y += BOMB_MISSILE_SPEED;
  114. }
  115. if (bomb_position.y > game->GetWindowDimensions().y)
  116. bomb = NONE;
  117. // Check if we hit the shields
  118. for (int i = 0; i < game->GetNumShields(); i++)
  119. {
  120. if (game->GetShield(i)->CheckHit(bomb_position))
  121. {
  122. bomb = NONE;
  123. break;
  124. }
  125. }
  126. // Check if we hit the defender
  127. if (bomb != NONE)
  128. {
  129. if (game->GetDefender()->CheckHit(bomb_position))
  130. bomb = NONE;
  131. }
  132. }
  133. else if (state == ALIVE &&
  134. Rocket::Core::Math::RandomReal(1.0f) < bomb_probability &&
  135. game->CanDropBomb(invader_index))
  136. {
  137. bomb = Rocket::Core::Math::RandomInteger(2) == 0 ? RAY : MISSILE;
  138. bomb_position = position;
  139. bomb_position.x += invader_sprites[GetSpriteIndex()].dimensions.x / 2;
  140. if (bomb == RAY)
  141. bomb_animation_frame = 0;
  142. else
  143. bomb_animation_frame = 4;
  144. }
  145. bomb_frame_start = Shell::GetElapsedTime();
  146. }
  147. if (state == EXPLODING && Shell::GetElapsedTime() > death_time)
  148. state = DEAD;
  149. }
  150. void Invader::UpdateAnimation()
  151. {
  152. switch (state)
  153. {
  154. case ALIVE:
  155. animation_frame++;
  156. if (animation_frame > 1)
  157. animation_frame = 0;
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. void Invader::Render()
  164. {
  165. if (type == MOTHERSHIP)
  166. {
  167. glColor4ubv(MOTHERSHIP_COLOUR);
  168. }
  169. int sprite_index = GetSpriteIndex();
  170. int sprite_offset = Rocket::Core::Math::RealToInteger((invader_sprites[sprite_index].dimensions.x - 48) / 2);
  171. if (state != DEAD)
  172. invader_sprites[sprite_index].Render(Rocket::Core::Vector2f(position.x - sprite_offset, position.y));
  173. if (bomb != NONE)
  174. {
  175. bomb_sprites[bomb_animation_frame].Render(bomb_position);
  176. }
  177. if (type == MOTHERSHIP)
  178. {
  179. glColor4ub(255, 255, 255, 255);
  180. }
  181. }
  182. Invader::InvaderState Invader::GetState()
  183. {
  184. return state;
  185. }
  186. bool Invader::CheckHit(const Rocket::Core::Vector2f& check_position)
  187. {
  188. // Get the sprite index we're currently using for collision detection
  189. int sprite_index = GetSpriteIndex();
  190. int sprite_offset = Rocket::Core::Math::RealToInteger((invader_sprites[sprite_index].dimensions.x - 48) / 2);
  191. float sprite_width = invader_sprites[sprite_index].dimensions.x;
  192. float sprite_height = invader_sprites[sprite_index].dimensions.y;
  193. // If we're alive and the position is within our bounds, set ourselves
  194. // as exploding and return a valid hit
  195. if (state == ALIVE
  196. && check_position.x >= position.x - sprite_offset
  197. && check_position.x <= position.x - sprite_offset + sprite_width
  198. && check_position.y >= position.y
  199. && check_position.y <= position.y + sprite_height)
  200. {
  201. int score = 0;
  202. switch (type)
  203. {
  204. case MOTHERSHIP: score = (Rocket::Core::Math::RandomInteger(6) + 1) * 50; break; // 50 -> 300
  205. case RANK3: score = 40; break;
  206. case RANK2: score = 20; break;
  207. case RANK1: score = 10; break;
  208. }
  209. // Add the number of points
  210. game->AddScore(score);
  211. // Set our state to exploding and start the timer to our doom
  212. state = EXPLODING;
  213. death_time = Shell::GetElapsedTime() + EXPLOSION_TIME;
  214. return true;
  215. }
  216. return false;
  217. }
  218. int Invader::GetSpriteIndex() const
  219. {
  220. // Calculate our sprite index based on animation and type
  221. int index = animation_frame;
  222. switch (type)
  223. {
  224. case RANK2: index += 2; break;
  225. case RANK3: index += 4; break;
  226. case MOTHERSHIP: index = 6; break;
  227. }
  228. // If we're in exploding state, use the exploding sprite
  229. if (state == EXPLODING)
  230. index = 7;
  231. return index;
  232. }