This commit is contained in:
j.mei7
2022-03-20 00:25:18 +01:00
parent 45b1dc2404
commit 6279eb3adb
6 changed files with 22 additions and 13 deletions

View File

@@ -8,7 +8,7 @@ public class Grid<TGridObject>
int width, height;
float cellSize;
Vector3 originPosition;
TGridObject[,] gridArray;
public TGridObject[,] gridArray;
bool showDebug = true;

View File

@@ -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;
}

View File

@@ -1,5 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathfindingSystem : MonoBehaviour

View File

@@ -15,10 +15,12 @@ public class PathNode {
public bool isWalkable;
public PathNode cameFromNode;
public PathNode(Grid<PathNode> grid, int x, int y) {
this.grid = grid;
this.x = x;
this.y = y;
public List<PathNode> neighbourList = new List<PathNode>();
public PathNode(Grid<PathNode> _grid, int _x, int _y) {
grid = _grid;
x = _x;
y = _y;
isWalkable = false;
}

View File

@@ -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<PathNode>(width, height, cellSize, Vector3.zero, (Grid<PathNode> g, int x, int y) => new PathNode(g, x, y));
foreach (PathNode node in grid.gridArray) {
node.neighbourList = GetNeighbourList(node);
}
}
public Grid<PathNode> GetGrid() {
@@ -39,6 +44,8 @@ public class Pathfinding {
}
public List<PathNode> 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);

View File

@@ -6,7 +6,7 @@ public class Way : PlacedObject
{
public override void OnPlace()
{
Debug.Log("Placed Way");
//Debug.Log("Placed Way");
}
}