Invader.cpp 7.5 KB

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