Shield.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "Shield.h"
  28. #include <Rocket/Core/Math.h>
  29. #include <ShellOpenGL.h>
  30. #include "Game.h"
  31. #include "GameDetails.h"
  32. #include "Sprite.h"
  33. const int MAX_HEALTH = 4;
  34. Shield::Shield(Game* _game, ShieldType _type) : position(0,0)
  35. {
  36. game = _game;
  37. type = _type;
  38. health = MAX_HEALTH;
  39. InitialiseCells();
  40. }
  41. Shield::~Shield()
  42. {
  43. }
  44. void Shield::InitialiseCells()
  45. {
  46. if (type == REGULAR || type == TOP_LEFT || type == TOP_RIGHT)
  47. {
  48. for (int i = 0; i < NUM_SHIELD_CELLS; i++)
  49. {
  50. for (int j = 0; j < NUM_SHIELD_CELLS; j++)
  51. {
  52. shield_cells[i][j] = ON;
  53. }
  54. }
  55. // Take the bites out of the cells if they're a corner cell:
  56. if (type == TOP_LEFT)
  57. {
  58. for (int x = 0; x < NUM_SHIELD_CELLS - 1; x++)
  59. {
  60. for (int y = 0; y < (NUM_SHIELD_CELLS - 1) - x; y++)
  61. shield_cells[x][y] = OFF;
  62. }
  63. }
  64. else if (type == TOP_RIGHT)
  65. {
  66. for (int x = 0; x < NUM_SHIELD_CELLS - 1; x++)
  67. {
  68. for (int y = 0; y < (NUM_SHIELD_CELLS - 1) - x; y++)
  69. shield_cells[(NUM_SHIELD_CELLS - 1) - x][y] = OFF;
  70. }
  71. }
  72. }
  73. else
  74. {
  75. for (int i = 0; i < NUM_SHIELD_CELLS; i++)
  76. {
  77. for (int j = 0; j < NUM_SHIELD_CELLS; j++)
  78. {
  79. shield_cells[i][j] = OFF;
  80. }
  81. }
  82. if (type == BOTTOM_LEFT)
  83. {
  84. for (int x = 0; x < NUM_SHIELD_CELLS - 1; x++)
  85. {
  86. for (int y = 0; y < (NUM_SHIELD_CELLS - 1) - x; y++)
  87. shield_cells[(NUM_SHIELD_CELLS - 1) - x][y] = ON;
  88. }
  89. }
  90. else if (type == BOTTOM_RIGHT)
  91. {
  92. for (int x = 0; x < NUM_SHIELD_CELLS - 1; x++)
  93. {
  94. for (int y = 0; y < (NUM_SHIELD_CELLS - 1) - x; y++)
  95. shield_cells[x][y] = ON;
  96. }
  97. }
  98. }
  99. }
  100. void Shield::SetPosition(const Rocket::Core::Vector2f& _position)
  101. {
  102. position = _position;
  103. }
  104. const Rocket::Core::Vector2f& Shield::GetPosition() const
  105. {
  106. return position;
  107. }
  108. void Shield::Render()
  109. {
  110. if (health > 0)
  111. {
  112. glPointSize((GLfloat) PIXEL_SIZE);
  113. glDisable(GL_TEXTURE_2D);
  114. glColor4ubv(GameDetails::GetDefenderColour());
  115. glBegin(GL_POINTS);
  116. for (int i = 0; i < NUM_SHIELD_CELLS; i++)
  117. {
  118. for (int j = 0; j < NUM_SHIELD_CELLS; j++)
  119. {
  120. if (shield_cells[i][j] == ON)
  121. {
  122. Rocket::Core::Vector2f cell_position = position + Rocket::Core::Vector2f((float) (PIXEL_SIZE * i), (float) (PIXEL_SIZE * j));
  123. glVertex2f(cell_position.x, cell_position.y);
  124. }
  125. }
  126. }
  127. glEnd();
  128. glEnable(GL_TEXTURE_2D);
  129. }
  130. }
  131. bool Shield::CheckHit(const Rocket::Core::Vector2f& check_position)
  132. {
  133. float sprite_size = PIXEL_SIZE * NUM_SHIELD_CELLS;
  134. // If we're alive and the position is within our bounds, set ourselves
  135. // as exploding and return a valid hit
  136. if (health > 0
  137. && check_position.x >= position.x
  138. && check_position.x <= position.x + sprite_size
  139. && check_position.y >= position.y
  140. && check_position.y <= position.y + sprite_size)
  141. {
  142. // Take damage.
  143. SustainDamage();
  144. return true;
  145. }
  146. return false;
  147. }
  148. void Shield::SustainDamage()
  149. {
  150. health--;
  151. if (health > 0)
  152. {
  153. int num_shields_to_lose = (NUM_SHIELD_CELLS * NUM_SHIELD_CELLS) / MAX_HEALTH;
  154. while (num_shields_to_lose > 0)
  155. {
  156. int x = Rocket::Core::Math::RandomInteger(NUM_SHIELD_CELLS);
  157. int y = Rocket::Core::Math::RandomInteger(NUM_SHIELD_CELLS);
  158. if (shield_cells[x][y] != DESTROYED)
  159. {
  160. shield_cells[x][y] = DESTROYED;
  161. num_shields_to_lose--;
  162. }
  163. }
  164. }
  165. }