Grid.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. }
  100. }
  101. // Open/closed sets
  102. std::vector<Tile*> openSet;
  103. std::vector<Tile*> closedSet;
  104. // Set current node to start, and add to closed set
  105. Tile* current = start;
  106. closedSet.emplace_back(current);
  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. auto iter = std::find(closedSet.begin(), closedSet.end(),
  118. neighbor);
  119. if (iter == closedSet.end())
  120. {
  121. iter = std::find(openSet.begin(), openSet.end(), neighbor);
  122. if (iter == openSet.end())
  123. {
  124. // Not in the open set, so set parent
  125. neighbor->mParent = current;
  126. neighbor->h = Math::Abs(neighbor->GetPosition().x - goal->GetPosition().x)
  127. + Math::Abs(neighbor->GetPosition().y - goal->GetPosition().y);
  128. // g(x) is the parent's g plus cost of traversing edge
  129. neighbor->g = current->g + TileSize;
  130. neighbor->f = neighbor->g + neighbor->h;
  131. openSet.emplace_back(neighbor);
  132. }
  133. else
  134. {
  135. // Compute g(x) cost if current becomes the parent
  136. float newG = current->g + TileSize;
  137. if (newG < current->g)
  138. {
  139. // Adopt this node
  140. neighbor->mParent = current;
  141. neighbor->g = newG;
  142. // f(x) changes because g(x) changes
  143. neighbor->f = neighbor->g + neighbor->h;
  144. }
  145. }
  146. }
  147. }
  148. // If open set is empty, all possible paths are exhausted
  149. if (openSet.empty())
  150. {
  151. break;
  152. }
  153. // Find lowest cost node in open set
  154. auto iter = std::min_element(openSet.begin(), openSet.end(),
  155. [](Tile* a, Tile* b) {
  156. return a->f < b->f;
  157. });
  158. // Set to current and move from open to closed
  159. current = *iter;
  160. openSet.erase(iter);
  161. closedSet.emplace_back(current);
  162. }
  163. while (current != goal);
  164. // Did we find a path?
  165. return (current == goal) ? true : false;
  166. }
  167. void Grid::UpdatePathTiles(class Tile* start)
  168. {
  169. // Reset all tiles to normal (except for start/end)
  170. for (size_t i = 0; i < NumRows; i++)
  171. {
  172. for (size_t j = 0; j < NumCols; j++)
  173. {
  174. if (!(i == 3 && j == 0) && !(i == 3 && j == 15))
  175. {
  176. mTiles[i][j]->SetTileState(Tile::EDefault);
  177. }
  178. }
  179. }
  180. Tile* t = start->mParent;
  181. while (t != GetEndTile())
  182. {
  183. t->SetTileState(Tile::EPath);
  184. t = t->mParent;
  185. }
  186. }
  187. void Grid::BuildTower()
  188. {
  189. if (mSelectedTile && !mSelectedTile->mBlocked)
  190. {
  191. mSelectedTile->mBlocked = true;
  192. if (FindPath(GetEndTile(), GetStartTile()))
  193. {
  194. Tower* t = new Tower(GetGame());
  195. t->SetPosition(mSelectedTile->GetPosition());
  196. }
  197. else
  198. {
  199. // This tower would block the path, so don't allow build
  200. mSelectedTile->mBlocked = false;
  201. FindPath(GetEndTile(), GetStartTile());
  202. }
  203. UpdatePathTiles(GetStartTile());
  204. }
  205. }
  206. Tile* Grid::GetStartTile()
  207. {
  208. return mTiles[3][0];
  209. }
  210. Tile* Grid::GetEndTile()
  211. {
  212. return mTiles[3][15];
  213. }
  214. void Grid::UpdateActor(float deltaTime)
  215. {
  216. Actor::UpdateActor(deltaTime);
  217. // Is it time to spawn a new enemy?
  218. mNextEnemy -= deltaTime;
  219. if (mNextEnemy <= 0.0f)
  220. {
  221. new Enemy(GetGame());
  222. mNextEnemy += EnemyTime;
  223. }
  224. }