Defender.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "Defender.h"
  28. #include <Shell.h>
  29. #include <ShellOpenGL.h>
  30. #include "Game.h"
  31. #include "GameDetails.h"
  32. #include "Invader.h"
  33. #include "Mothership.h"
  34. #include "Shield.h"
  35. #include "Sprite.h"
  36. const float UPDATE_FREQ = 0.01f;
  37. const float MOVEMENT_SPEED = 15;
  38. const float BULLET_SPEED = 15;
  39. const int SPRITE_WIDTH = 64;
  40. const float RESPAWN_TIME = 1.0f;
  41. Sprite defender_sprite(Rocket::Core::Vector2f(60, 31), Rocket::Core::Vector2f(0, 0.5), Rocket::Core::Vector2f(0.23437500, 0.98437500));
  42. Sprite bullet_sprite(Rocket::Core::Vector2f(4, 20), Rocket::Core::Vector2f(0.4921875, 0.515625), Rocket::Core::Vector2f(0.5078125, 0.828125));
  43. Sprite explosion_sprite(Rocket::Core::Vector2f(52, 28), Rocket::Core::Vector2f(0.71484375f, 0.51562500f), Rocket::Core::Vector2f(0.91796875f, 0.95312500f));
  44. Defender::Defender(Game* _game)
  45. {
  46. move_direction = 0;
  47. defender_frame_start = 0;
  48. bullet_in_flight = false;
  49. game = _game;
  50. position.x = game->GetWindowDimensions().x / 2;
  51. position.y = game->GetWindowDimensions().y - 50;
  52. state = ALIVE;
  53. render = true;
  54. }
  55. Defender::~Defender()
  56. {
  57. }
  58. void Defender::Update()
  59. {
  60. if (Shell::GetElapsedTime() - defender_frame_start < UPDATE_FREQ)
  61. return;
  62. defender_frame_start = Shell::GetElapsedTime();
  63. position.x += (move_direction * MOVEMENT_SPEED);
  64. if (position.x < 5)
  65. position.x = 5;
  66. else if (position.x > (game->GetWindowDimensions().x - SPRITE_WIDTH - 5))
  67. position.x = game->GetWindowDimensions().x - SPRITE_WIDTH - 5;
  68. // Update the bullet
  69. if (bullet_in_flight)
  70. {
  71. // Move it up and mark it dead if it flies off the top of the screen
  72. bullet_position.y -= BULLET_SPEED;
  73. if (bullet_position.y < 0)
  74. bullet_in_flight = false;
  75. }
  76. if (state == RESPAWN)
  77. {
  78. // Switch the render flag so the defender "flickers"
  79. render = !render;
  80. // Check if we should switch back to our alive state
  81. if (Shell::GetElapsedTime() - respawn_start > RESPAWN_TIME)
  82. {
  83. state = ALIVE;
  84. render = true;
  85. }
  86. }
  87. }
  88. void Defender::Render()
  89. {
  90. glColor4ubv(GameDetails::GetDefenderColour());
  91. // Render our sprite if rendering is enabled
  92. if (render)
  93. defender_sprite.Render(Rocket::Core::Vector2f(position.x, position.y));
  94. // Update the bullet, doing collision detection
  95. if (bullet_in_flight)
  96. {
  97. bullet_sprite.Render(Rocket::Core::Vector2f(bullet_position.x, bullet_position.y));
  98. // Check if we hit the shields
  99. for (int i = 0; i < game->GetNumShields(); i++)
  100. {
  101. if (game->GetShield(i)->CheckHit(bullet_position))
  102. {
  103. bullet_in_flight = false;
  104. break;
  105. }
  106. }
  107. if (bullet_in_flight)
  108. {
  109. for (int i = 0; i < game->GetNumInvaders(); i++)
  110. {
  111. if (game->GetInvader(i)->CheckHit(bullet_position))
  112. {
  113. bullet_in_flight = false;
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. glColor4ub(255, 255, 255, 255);
  120. }
  121. void Defender::StartMove(float direction)
  122. {
  123. move_direction = direction > 0.0f ? 1.0f : -1.0f;
  124. }
  125. void Defender::StopMove(float direction)
  126. {
  127. float stop_direction = direction > 0.0f ? 1.0f : -1.0f;
  128. if (stop_direction == move_direction)
  129. move_direction = 0.0f;
  130. }
  131. void Defender::Fire()
  132. {
  133. if (!bullet_in_flight)
  134. {
  135. bullet_position = position + Rocket::Core::Vector2f((SPRITE_WIDTH/2) - 4, 0);
  136. bullet_in_flight = true;
  137. }
  138. }
  139. bool Defender::CheckHit(const Rocket::Core::Vector2f& check_position)
  140. {
  141. float sprite_width = defender_sprite.dimensions.x;
  142. float sprite_height = defender_sprite.dimensions.y;
  143. // If the position is within our bounds, set ourselves
  144. // as exploding and return a valid hit.
  145. if (state == ALIVE
  146. && check_position.x >= position.x
  147. && check_position.x <= position.x + sprite_width
  148. && check_position.y >= position.y
  149. && check_position.y <= position.y + sprite_height)
  150. {
  151. game->RemoveLife();
  152. state = RESPAWN;
  153. respawn_start = Shell::GetElapsedTime();
  154. return true;
  155. }
  156. return false;
  157. }