Grid.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "Grid.h"
  9. #include "Tile.h"
  10. #include "Tower.h"
  11. #include "Enemy.h"
  12. #include <algorithm>
  13. Grid::Grid(class Game* game)
  14. :Actor(game)
  15. ,mSelectedTile(nullptr)
  16. {
  17. // 7 rows, 16 columns
  18. mTiles.resize(NumRows);
  19. for (size_t i = 0; i < mTiles.size(); i++)
  20. {
  21. mTiles[i].resize(NumCols);
  22. }
  23. // Create tiles
  24. for (size_t i = 0; i < NumRows; i++)
  25. {
  26. for (size_t j = 0; j < NumCols; j++)
  27. {
  28. mTiles[i][j] = new Tile(GetGame());
  29. mTiles[i][j]->SetPosition(Vector2(TileSize/2.0f + j * TileSize, StartY + i * TileSize));
  30. }
  31. }
  32. // Set start/end tiles
  33. GetStartTile()->SetTileState(Tile::EStart);
  34. GetEndTile()->SetTileState(Tile::EBase);
  35. // Set up adjacency lists
  36. for (size_t i = 0; i < NumRows; i++)
  37. {
  38. for (size_t j = 0; j < NumCols; j++)
  39. {
  40. if (i > 0)
  41. {
  42. mTiles[i][j]->mAdjacent.push_back(mTiles[i-1][j]);
  43. }
  44. if (i < NumRows - 1)
  45. {
  46. mTiles[i][j]->mAdjacent.push_back(mTiles[i+1][j]);
  47. }
  48. if (j > 0)
  49. {
  50. mTiles[i][j]->mAdjacent.push_back(mTiles[i][j-1]);
  51. }
  52. if (j < NumCols - 1)
  53. {
  54. mTiles[i][j]->mAdjacent.push_back(mTiles[i][j+1]);
  55. }
  56. }
  57. }
  58. // Find path (in reverse)
  59. FindPath(GetEndTile(), GetStartTile());
  60. UpdatePathTiles(GetStartTile());
  61. mNextEnemy = EnemyTime;
  62. }
  63. void Grid::SelectTile(size_t row, size_t col)
  64. {
  65. // Make sure it's a valid selection
  66. Tile::TileState tstate = mTiles[row][col]->GetTileState();
  67. if (tstate != Tile::EStart && tstate != Tile::EBase)
  68. {
  69. // Deselect previous one
  70. if (mSelectedTile)
  71. {
  72. mSelectedTile->ToggleSelect();
  73. }
  74. mSelectedTile = mTiles[row][col];
  75. mSelectedTile->ToggleSelect();
  76. }
  77. }
  78. void Grid::ProcessClick(int x, int y)
  79. {
  80. y -= static_cast<int>(StartY - TileSize / 2);
  81. if (y >= 0)
  82. {
  83. x /= static_cast<int>(TileSize);
  84. y /= static_cast<int>(TileSize);
  85. if (x >= 0 && x < static_cast<int>(NumCols) && y >= 0 && y < static_cast<int>(NumRows))
  86. {
  87. SelectTile(y, x);
  88. }
  89. }
  90. }
  91. // Implements A* pathfinding
  92. bool Grid::FindPath(Tile* start, Tile* goal)
  93. {
  94. for (size_t i = 0; i < NumRows; i++)
  95. {
  96. for (size_t j = 0; j < NumCols; j++)
  97. {
  98. mTiles[i][j]->g = 0.0f;
  99. mTiles[i][j]->mInOpenSet = false;
  100. mTiles[i][j]->mInClosedSet = false;
  101. }
  102. }
  103. std::vector<Tile*> openSet;
  104. // Set current node to start, and add to closed set
  105. Tile* current = start;
  106. current->mInClosedSet = true;
  107. do
  108. {
  109. // Add adjacent nodes to open set
  110. for (Tile* neighbor : current->mAdjacent)
  111. {
  112. if (neighbor->mBlocked)
  113. {
  114. continue;
  115. }
  116. // Only check nodes that aren't in the closed set
  117. if (!neighbor->mInClosedSet)
  118. {
  119. if (!neighbor->mInOpenSet)
  120. {
  121. // Not in the open set, so set parent
  122. neighbor->mParent = current;
  123. neighbor->h = (neighbor->GetPosition() - goal->GetPosition()).Length();
  124. // g(x) is the parent's g plus cost of traversing edge
  125. neighbor->g = current->g + TileSize;
  126. neighbor->f = neighbor->g + neighbor->h;
  127. openSet.emplace_back(neighbor);
  128. neighbor->mInOpenSet = true;
  129. }
  130. else
  131. {
  132. // Compute g(x) cost if current becomes the parent
  133. float newG = current->g + TileSize;
  134. if (newG < neighbor->g)
  135. {
  136. // Adopt this node
  137. neighbor->mParent = current;
  138. neighbor->g = newG;
  139. // f(x) changes because g(x) changes
  140. neighbor->f = neighbor->g + neighbor->h;
  141. }
  142. }
  143. }
  144. }
  145. // If open set is empty, all possible paths are exhausted
  146. if (openSet.empty())
  147. {
  148. break;
  149. }
  150. // Find lowest cost node in open set
  151. auto iter = std::min_element(openSet.begin(), openSet.end(),
  152. [](Tile* a, Tile* b) {
  153. return a->f < b->f;
  154. });
  155. // Set to current and move from open to closed
  156. current = *iter;
  157. openSet.erase(iter);
  158. current->mInOpenSet = false;
  159. current->mInClosedSet = true;
  160. }
  161. while (current != goal);
  162. // Did we find a path?
  163. return (current == goal) ? true : false;
  164. }
  165. void Grid::UpdatePathTiles(class Tile* start)
  166. {
  167. // Reset all tiles to normal (except for start/end)
  168. for (size_t i = 0; i < NumRows; i++)
  169. {
  170. for (size_t j = 0; j < NumCols; j++)
  171. {
  172. if (!(i == 3 && j == 0) && !(i == 3 && j == 15))
  173. {
  174. mTiles[i][j]->SetTileState(Tile::EDefault);
  175. }
  176. }
  177. }
  178. Tile* t = start->mParent;
  179. while (t != GetEndTile())
  180. {
  181. t->SetTileState(Tile::EPath);
  182. t = t->mParent;
  183. }
  184. }
  185. void Grid::BuildTower()
  186. {
  187. if (mSelectedTile && !mSelectedTile->mBlocked)
  188. {
  189. mSelectedTile->mBlocked = true;
  190. if (FindPath(GetEndTile(), GetStartTile()))
  191. {
  192. Tower* t = new Tower(GetGame());
  193. t->SetPosition(mSelectedTile->GetPosition());
  194. }
  195. else
  196. {
  197. // This tower would block the path, so don't allow build
  198. mSelectedTile->mBlocked = false;
  199. FindPath(GetEndTile(), GetStartTile());
  200. }
  201. UpdatePathTiles(GetStartTile());
  202. }
  203. }
  204. Tile* Grid::GetStartTile()
  205. {
  206. return mTiles[3][0];
  207. }
  208. Tile* Grid::GetEndTile()
  209. {
  210. return mTiles[3][15];
  211. }
  212. void Grid::UpdateActor(float deltaTime)
  213. {
  214. Actor::UpdateActor(deltaTime);
  215. // Is it time to spawn a new enemy?
  216. mNextEnemy -= deltaTime;
  217. if (mNextEnemy <= 0.0f)
  218. {
  219. new Enemy(GetGame());
  220. mNextEnemy += EnemyTime;
  221. }
  222. }