Parcourir la source

Updated tile/grid

Sanjay Madhav il y a 8 ans
Parent
commit
999716a508
4 fichiers modifiés avec 42 ajouts et 20 suppressions
  1. 12 14
      Chapter05/Grid.cpp
  2. 24 2
      Chapter05/Grid.h
  3. 2 2
      Chapter05/Tile.cpp
  4. 4 2
      Chapter05/Tile.h

+ 12 - 14
Chapter05/Grid.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Grid.h"
@@ -106,16 +106,16 @@ bool Grid::FindPath(Tile* start, Tile* goal)
 		for (size_t j = 0; j < NumCols; j++)
 		{
 			mTiles[i][j]->g = 0.0f;
+			mTiles[i][j]->mInOpenSet = false;
+			mTiles[i][j]->mInClosedSet = false;
 		}
 	}
 	
-	// Open/closed sets
 	std::vector<Tile*> openSet;
-	std::vector<Tile*> closedSet;
 	
 	// Set current node to start, and add to closed set
 	Tile* current = start;
-	closedSet.emplace_back(current);
+	current->mInClosedSet = true;
 	
 	do
 	{
@@ -128,27 +128,24 @@ bool Grid::FindPath(Tile* start, Tile* goal)
 			}
 			
 			// Only check nodes that aren't in the closed set
-			auto iter = std::find(closedSet.begin(), closedSet.end(),
-								  neighbor);
-			if (iter == closedSet.end())
+			if (!neighbor->mInClosedSet)
 			{
-				iter = std::find(openSet.begin(), openSet.end(), neighbor);
-				if (iter == openSet.end())
+				if (!neighbor->mInOpenSet)
 				{
 					// Not in the open set, so set parent
 					neighbor->mParent = current;
-					neighbor->h = Math::Abs(neighbor->GetPosition().x - goal->GetPosition().x)
-						+ Math::Abs(neighbor->GetPosition().y - goal->GetPosition().y);
+					neighbor->h = (neighbor->GetPosition() - goal->GetPosition()).Length();
 					// g(x) is the parent's g plus cost of traversing edge
 					neighbor->g = current->g + TileSize;
 					neighbor->f = neighbor->g + neighbor->h;
 					openSet.emplace_back(neighbor);
+					neighbor->mInOpenSet = true;
 				}
 				else
 				{
 					// Compute g(x) cost if current becomes the parent
 					float newG = current->g + TileSize;
-					if (newG < current->g)
+					if (newG < neighbor->g)
 					{
 						// Adopt this node
 						neighbor->mParent = current;
@@ -174,7 +171,8 @@ bool Grid::FindPath(Tile* start, Tile* goal)
 		// Set to current and move from open to closed
 		current = *iter;
 		openSet.erase(iter);
-		closedSet.emplace_back(current);
+		current->mInOpenSet = false;
+		current->mInClosedSet = true;
 	}
 	while (current != goal);
 	

+ 24 - 2
Chapter05/Grid.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -14,23 +14,45 @@ class Grid : public Actor
 {
 public:
 	Grid(class Game* game);
+	
+	// Handle a mouse click at the x/y screen locations
 	void ProcessClick(int x, int y);
+	
+	// Use A* to find a path
 	bool FindPath(class Tile* start, class Tile* goal);
+	
+	// Try to build a tower
 	void BuildTower();
+	
+	// Get start/end tile
 	class Tile* GetStartTile();
 	class Tile* GetEndTile();
+
 	void UpdateActor(float deltaTime) override;
 private:
+	// Select a specific tile
 	void SelectTile(size_t row, size_t col);
+	
+	// Update textures for tiles on path
 	void UpdatePathTiles(class Tile* start);
+	
+	// Currently selected tile
 	class Tile* mSelectedTile;
+	
+	// 2D vector of tiles in grid
 	std::vector<std::vector<class Tile*>> mTiles;
+	
+	// Time until next enemy
 	float mNextEnemy;
 	
+	// Rows/columns in grid
 	const size_t NumRows = 7;
 	const size_t NumCols = 16;
+	// Start y position of top left corner
 	const float StartY = 192.0f;
 	const float StartX = -512.0f;
+	// Width/height of each tile
 	const float TileSize = 64.0f;
+	// Time between enemies
 	const float EnemyTime = 1.5f;
 };

+ 2 - 2
Chapter05/Tile.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Tile.h"

+ 4 - 2
Chapter05/Tile.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -35,6 +35,8 @@ private:
 	float f;
 	float g;
 	float h;
+	bool mInOpenSet;
+	bool mInClosedSet;
 	bool mBlocked;
 	
 	void UpdateTexture();