mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-29 12:32:10 +01:00
a
This commit is contained in:
@@ -8,7 +8,7 @@ public class Grid<TGridObject>
|
|||||||
int width, height;
|
int width, height;
|
||||||
float cellSize;
|
float cellSize;
|
||||||
Vector3 originPosition;
|
Vector3 originPosition;
|
||||||
TGridObject[,] gridArray;
|
public TGridObject[,] gridArray;
|
||||||
|
|
||||||
bool showDebug = true;
|
bool showDebug = true;
|
||||||
|
|
||||||
|
|||||||
@@ -194,7 +194,6 @@ public class GridBuildingSystem : MonoBehaviour
|
|||||||
|
|
||||||
public GameObject PlaceBuilding(Vector3 position)
|
public GameObject PlaceBuilding(Vector3 position)
|
||||||
{
|
{
|
||||||
Debug.Log("HALLO");
|
|
||||||
position = new Vector3(position.x, position.y);
|
position = new Vector3(position.x, position.y);
|
||||||
buildingGrid.GetXY(position, out int x, out int y);
|
buildingGrid.GetXY(position, out int x, out int y);
|
||||||
|
|
||||||
@@ -214,7 +213,7 @@ public class GridBuildingSystem : MonoBehaviour
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.Log("Cannot build here!" + " " + position);
|
//Debug.Log("Cannot build here!" + " " + position);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class PathfindingSystem : MonoBehaviour
|
public class PathfindingSystem : MonoBehaviour
|
||||||
|
|||||||
@@ -15,10 +15,12 @@ public class PathNode {
|
|||||||
public bool isWalkable;
|
public bool isWalkable;
|
||||||
public PathNode cameFromNode;
|
public PathNode cameFromNode;
|
||||||
|
|
||||||
public PathNode(Grid<PathNode> grid, int x, int y) {
|
public List<PathNode> neighbourList = new List<PathNode>();
|
||||||
this.grid = grid;
|
|
||||||
this.x = x;
|
public PathNode(Grid<PathNode> _grid, int _x, int _y) {
|
||||||
this.y = y;
|
grid = _grid;
|
||||||
|
x = _x;
|
||||||
|
y = _y;
|
||||||
isWalkable = false;
|
isWalkable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using System.Collections;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class Pathfinding {
|
public class Pathfinding {
|
||||||
|
|
||||||
private const int MOVE_STRAIGHT_COST = 10;
|
private const int MOVE_STRAIGHT_COST = 10;
|
||||||
@@ -16,6 +16,11 @@ public class Pathfinding {
|
|||||||
public Pathfinding(int width, int height, float cellSize) {
|
public Pathfinding(int width, int height, float cellSize) {
|
||||||
Instance = this;
|
Instance = this;
|
||||||
grid = new Grid<PathNode>(width, height, cellSize, Vector3.zero, (Grid<PathNode> g, int x, int y) => new PathNode(g, x, y));
|
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() {
|
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) {
|
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 startNode = grid.GetGridObject(startX, startY);
|
||||||
PathNode endNode = grid.GetGridObject(endX, endY);
|
PathNode endNode = grid.GetGridObject(endX, endY);
|
||||||
|
|
||||||
@@ -67,13 +74,16 @@ public class Pathfinding {
|
|||||||
PathNode currentNode = GetLowestFCostNode(openList);
|
PathNode currentNode = GetLowestFCostNode(openList);
|
||||||
if (currentNode == endNode) {
|
if (currentNode == endNode) {
|
||||||
// Reached final node
|
// Reached final node
|
||||||
|
timer.Stop();
|
||||||
|
TimeSpan timeTaken = timer.Elapsed;
|
||||||
|
Debug.Log("Time taken: " + timeTaken.ToString(@"m\:ss\.fff"));
|
||||||
return CalculatePath(endNode);
|
return CalculatePath(endNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
openList.Remove(currentNode);
|
openList.Remove(currentNode);
|
||||||
closedList.Add(currentNode);
|
closedList.Add(currentNode);
|
||||||
|
|
||||||
foreach (PathNode neighbourNode in GetNeighbourList(currentNode)) {
|
foreach (PathNode neighbourNode in currentNode.neighbourList) {
|
||||||
if (closedList.Contains(neighbourNode)) continue;
|
if (closedList.Contains(neighbourNode)) continue;
|
||||||
if (!neighbourNode.isWalkable && !ignoreIsWalkable) { // If neighbouring node is not walkable instantly add them to closed
|
if (!neighbourNode.isWalkable && !ignoreIsWalkable) { // If neighbouring node is not walkable instantly add them to closed
|
||||||
closedList.Add(neighbourNode);
|
closedList.Add(neighbourNode);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ public class Way : PlacedObject
|
|||||||
{
|
{
|
||||||
public override void OnPlace()
|
public override void OnPlace()
|
||||||
{
|
{
|
||||||
Debug.Log("Placed Way");
|
//Debug.Log("Placed Way");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user