diff --git a/Assets/Scripts/Grid/Grid.cs b/Assets/Scripts/Grid/Grid.cs index ab1738e..1347238 100644 --- a/Assets/Scripts/Grid/Grid.cs +++ b/Assets/Scripts/Grid/Grid.cs @@ -8,7 +8,7 @@ public class Grid int width, height; float cellSize; Vector3 originPosition; - TGridObject[,] gridArray; + public TGridObject[,] gridArray; bool showDebug = true; diff --git a/Assets/Scripts/Grid/GridBuildingSystem.cs b/Assets/Scripts/Grid/GridBuildingSystem.cs index 84d7459..77414ec 100644 --- a/Assets/Scripts/Grid/GridBuildingSystem.cs +++ b/Assets/Scripts/Grid/GridBuildingSystem.cs @@ -194,7 +194,6 @@ public class GridBuildingSystem : MonoBehaviour public GameObject PlaceBuilding(Vector3 position) { - Debug.Log("HALLO"); position = new Vector3(position.x, position.y); buildingGrid.GetXY(position, out int x, out int y); @@ -214,7 +213,7 @@ public class GridBuildingSystem : MonoBehaviour } else { - Debug.Log("Cannot build here!" + " " + position); + //Debug.Log("Cannot build here!" + " " + position); } return null; } diff --git a/Assets/Scripts/Grid/PathfindingSystem.cs b/Assets/Scripts/Grid/PathfindingSystem.cs index c789c17..9be6f3a 100644 --- a/Assets/Scripts/Grid/PathfindingSystem.cs +++ b/Assets/Scripts/Grid/PathfindingSystem.cs @@ -1,5 +1,3 @@ -using System.Collections; -using System.Collections.Generic; using UnityEngine; public class PathfindingSystem : MonoBehaviour diff --git a/Assets/Scripts/Pathfinding/PathNode.cs b/Assets/Scripts/Pathfinding/PathNode.cs index b202b57..d4f44e0 100644 --- a/Assets/Scripts/Pathfinding/PathNode.cs +++ b/Assets/Scripts/Pathfinding/PathNode.cs @@ -15,10 +15,12 @@ public class PathNode { public bool isWalkable; public PathNode cameFromNode; - public PathNode(Grid grid, int x, int y) { - this.grid = grid; - this.x = x; - this.y = y; + public List neighbourList = new List(); + + public PathNode(Grid _grid, int _x, int _y) { + grid = _grid; + x = _x; + y = _y; isWalkable = false; } diff --git a/Assets/Scripts/Pathfinding/Pathfinding.cs b/Assets/Scripts/Pathfinding/Pathfinding.cs index d29a66a..fcfb0aa 100644 --- a/Assets/Scripts/Pathfinding/Pathfinding.cs +++ b/Assets/Scripts/Pathfinding/Pathfinding.cs @@ -1,7 +1,7 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using UnityEngine; - public class Pathfinding { private const int MOVE_STRAIGHT_COST = 10; @@ -16,6 +16,11 @@ public class Pathfinding { public Pathfinding(int width, int height, float cellSize) { Instance = this; grid = new Grid(width, height, cellSize, Vector3.zero, (Grid g, int x, int y) => new PathNode(g, x, y)); + + foreach (PathNode node in grid.gridArray) { + node.neighbourList = GetNeighbourList(node); + } + } public Grid GetGrid() { @@ -39,6 +44,8 @@ public class Pathfinding { } public List FindPath(int startX, int startY, int endX, int endY, bool ignoreIsWalkable = false) { + var timer = new System.Diagnostics.Stopwatch(); + timer.Start(); PathNode startNode = grid.GetGridObject(startX, startY); PathNode endNode = grid.GetGridObject(endX, endY); @@ -67,13 +74,16 @@ public class Pathfinding { PathNode currentNode = GetLowestFCostNode(openList); if (currentNode == endNode) { // Reached final node + timer.Stop(); + TimeSpan timeTaken = timer.Elapsed; + Debug.Log("Time taken: " + timeTaken.ToString(@"m\:ss\.fff")); return CalculatePath(endNode); } openList.Remove(currentNode); closedList.Add(currentNode); - foreach (PathNode neighbourNode in GetNeighbourList(currentNode)) { + foreach (PathNode neighbourNode in currentNode.neighbourList) { if (closedList.Contains(neighbourNode)) continue; if (!neighbourNode.isWalkable && !ignoreIsWalkable) { // If neighbouring node is not walkable instantly add them to closed closedList.Add(neighbourNode); diff --git a/Assets/Scripts/PlacedObjects/Way.cs b/Assets/Scripts/PlacedObjects/Way.cs index 466f41e..eb860d5 100644 --- a/Assets/Scripts/PlacedObjects/Way.cs +++ b/Assets/Scripts/PlacedObjects/Way.cs @@ -6,7 +6,7 @@ public class Way : PlacedObject { public override void OnPlace() { - Debug.Log("Placed Way"); + //Debug.Log("Placed Way"); } }