Shield.cpp 4.5 KB

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