mirror of
https://github.com/DerTyp7/industrialize-unity.git
synced 2025-10-30 21:07:11 +01:00
CameraMovement
This commit is contained in:
39
Assets/Scripts/Pathfinding/PathNode.cs
Normal file
39
Assets/Scripts/Pathfinding/PathNode.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PathNode {
|
||||
|
||||
private Grid<PathNode> grid;
|
||||
public int x;
|
||||
public int y;
|
||||
|
||||
public int gCost;
|
||||
public int hCost;
|
||||
public int fCost;
|
||||
|
||||
public bool isWalkable;
|
||||
public PathNode cameFromNode;
|
||||
|
||||
public List<PathNode> neighbourList = new List<PathNode>();
|
||||
|
||||
public PathNode(Grid<PathNode> _grid, int _x, int _y) {
|
||||
grid = _grid;
|
||||
x = _x;
|
||||
y = _y;
|
||||
isWalkable = false;
|
||||
}
|
||||
|
||||
public void CalculateFCost() {
|
||||
fCost = gCost + hCost;
|
||||
}
|
||||
|
||||
public void SetIsWalkable(bool isWalkable) {
|
||||
this.isWalkable = isWalkable;
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
return x + "," + y;
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Pathfinding/PathNode.cs.meta
Normal file
11
Assets/Scripts/Pathfinding/PathNode.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a762ee25504d4344b891ad21350450f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
171
Assets/Scripts/Pathfinding/Pathfinding.cs
Normal file
171
Assets/Scripts/Pathfinding/Pathfinding.cs
Normal file
@@ -0,0 +1,171 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
public class Pathfinding {
|
||||
|
||||
private const int MOVE_STRAIGHT_COST = 10;
|
||||
private const int MOVE_DIAGONAL_COST = 14;
|
||||
|
||||
public static Pathfinding Instance { get; private set; }
|
||||
|
||||
private Grid<PathNode> grid;
|
||||
private List<PathNode> openList;
|
||||
private List<PathNode> closedList;
|
||||
|
||||
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() {
|
||||
return grid;
|
||||
}
|
||||
|
||||
public List<Vector3> FindPath(Vector3 startWorldPosition, Vector3 endWorldPosition, bool ignoreIsWalkable = false) {
|
||||
grid.GetXY(startWorldPosition, out int startX, out int startY);
|
||||
grid.GetXY(endWorldPosition, out int endX, out int endY);
|
||||
|
||||
List<PathNode> path = FindPath(startX, startY, endX, endY, ignoreIsWalkable);
|
||||
if (path == null) {
|
||||
return null;
|
||||
} else {
|
||||
List<Vector3> vectorPath = new List<Vector3>();
|
||||
foreach (PathNode pathNode in path) {
|
||||
vectorPath.Add(new Vector3(pathNode.x, pathNode.y) * grid.GetCellSize() + Vector3.one * grid.GetCellSize() * .5f);
|
||||
}
|
||||
return vectorPath;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (startNode == null || endNode == null) {
|
||||
// Invalid Path
|
||||
return null;
|
||||
}
|
||||
|
||||
openList = new List<PathNode> { startNode };
|
||||
closedList = new List<PathNode>();
|
||||
|
||||
for (int x = 0; x < grid.GetWidth(); x++) {
|
||||
for (int y = 0; y < grid.GetHeight(); y++) {
|
||||
PathNode pathNode = grid.GetGridObject(x, y);
|
||||
pathNode.gCost = 99999999;
|
||||
pathNode.CalculateFCost();
|
||||
pathNode.cameFromNode = null;
|
||||
}
|
||||
}
|
||||
|
||||
startNode.gCost = 0;
|
||||
startNode.hCost = CalculateDistanceCost(startNode, endNode);
|
||||
startNode.CalculateFCost();
|
||||
|
||||
while (openList.Count > 0) {
|
||||
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 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);
|
||||
continue;
|
||||
}
|
||||
|
||||
int tentativeGCost = currentNode.gCost + CalculateDistanceCost(currentNode, neighbourNode);
|
||||
if (tentativeGCost < neighbourNode.gCost) {
|
||||
neighbourNode.cameFromNode = currentNode;
|
||||
neighbourNode.gCost = tentativeGCost;
|
||||
neighbourNode.hCost = CalculateDistanceCost(neighbourNode, endNode);
|
||||
neighbourNode.CalculateFCost();
|
||||
|
||||
if (!openList.Contains(neighbourNode)) {
|
||||
openList.Add(neighbourNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Out of nodes on the openList
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<PathNode> GetNeighbourList(PathNode currentNode) {
|
||||
List<PathNode> neighbourList = new List<PathNode>();
|
||||
|
||||
if (currentNode.x - 1 >= 0) {
|
||||
// Left
|
||||
neighbourList.Add(GetNode(currentNode.x - 1, currentNode.y));
|
||||
// Left Down
|
||||
// if (currentNode.y - 1 >= 0) neighbourList.Add(GetNode(currentNode.x - 1, currentNode.y - 1));
|
||||
// Left Up
|
||||
// if (currentNode.y + 1 < grid.GetHeight()) neighbourList.Add(GetNode(currentNode.x - 1, currentNode.y + 1));
|
||||
}
|
||||
if (currentNode.x + 1 < grid.GetWidth()) {
|
||||
// Right
|
||||
neighbourList.Add(GetNode(currentNode.x + 1, currentNode.y));
|
||||
// Right Down
|
||||
// if (currentNode.y - 1 >= 0) neighbourList.Add(GetNode(currentNode.x + 1, currentNode.y - 1));
|
||||
// Right Up
|
||||
// if (currentNode.y + 1 < grid.GetHeight()) neighbourList.Add(GetNode(currentNode.x + 1, currentNode.y + 1));
|
||||
}
|
||||
// Down
|
||||
if (currentNode.y - 1 >= 0) neighbourList.Add(GetNode(currentNode.x, currentNode.y - 1));
|
||||
// Up
|
||||
if (currentNode.y + 1 < grid.GetHeight()) neighbourList.Add(GetNode(currentNode.x, currentNode.y + 1));
|
||||
|
||||
return neighbourList;
|
||||
}
|
||||
|
||||
public PathNode GetNode(int x, int y) {
|
||||
return grid.GetGridObject(x, y);
|
||||
}
|
||||
|
||||
private List<PathNode> CalculatePath(PathNode endNode) {
|
||||
List<PathNode> path = new List<PathNode>();
|
||||
path.Add(endNode);
|
||||
PathNode currentNode = endNode;
|
||||
while (currentNode.cameFromNode != null) {
|
||||
path.Add(currentNode.cameFromNode);
|
||||
currentNode = currentNode.cameFromNode;
|
||||
}
|
||||
path.Reverse();
|
||||
return path;
|
||||
}
|
||||
|
||||
private int CalculateDistanceCost(PathNode a, PathNode b) {
|
||||
int xDistance = Mathf.Abs(a.x - b.x);
|
||||
int yDistance = Mathf.Abs(a.y - b.y);
|
||||
int remaining = Mathf.Abs(xDistance - yDistance);
|
||||
return MOVE_DIAGONAL_COST * Mathf.Min(xDistance, yDistance) + MOVE_STRAIGHT_COST * remaining;
|
||||
}
|
||||
|
||||
private PathNode GetLowestFCostNode(List<PathNode> pathNodeList) {
|
||||
PathNode lowestFCostNode = pathNodeList[0];
|
||||
for (int i = 1; i < pathNodeList.Count; i++) {
|
||||
if (pathNodeList[i].fCost < lowestFCostNode.fCost) {
|
||||
lowestFCostNode = pathNodeList[i];
|
||||
}
|
||||
}
|
||||
return lowestFCostNode;
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Pathfinding/Pathfinding.cs.meta
Normal file
11
Assets/Scripts/Pathfinding/Pathfinding.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e5530caa2a712744aa8742d15eb47ac9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
9
Assets/Scripts/Pathfinding/Testing.cs
Normal file
9
Assets/Scripts/Pathfinding/Testing.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Testing : MonoBehaviour {
|
||||
|
||||
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Pathfinding/Testing.cs.meta
Normal file
11
Assets/Scripts/Pathfinding/Testing.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4a9e1a4cbcd629e43960027a99f45196
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user