forked from nhathuy7996/ReUseScript_Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pathfinding.cs
72 lines (61 loc) · 2.96 KB
/
Pathfinding.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Linq;
using _Scripts.Tiles;
using UnityEngine;
namespace Tarodev_Pathfinding._Scripts {
/// <summary>
/// This algorithm is written for readability. Although it would be perfectly fine in 80% of games, please
/// don't use this in an RTS without first applying some optimization mentioned in the video: https://youtu.be/i0x5fj4PqP4
/// If you enjoyed the explanation, be sure to subscribe!
///
/// Also, setting colors and text on each hex affects performance, so removing that will also improve it marginally.
/// </summary>
public static class Pathfinding {
private static readonly Color PathColor = new Color(0.65f, 0.35f, 0.35f);
private static readonly Color OpenColor = new Color(.4f, .6f, .4f);
private static readonly Color ClosedColor = new Color(0.35f, 0.4f, 0.5f);
public static List<NodeBase> FindPath(NodeBase startNode, NodeBase targetNode) {
var toSearch = new List<NodeBase>() { startNode };
var processed = new List<NodeBase>();
while (toSearch.Any()) {
var current = toSearch[0];
foreach (var t in toSearch)
if (t.F < current.F || t.F == current.F && t.H < current.H) current = t;
processed.Add(current);
toSearch.Remove(current);
current.SetColor(ClosedColor);
if (current == targetNode) {
var currentPathTile = targetNode;
var path = new List<NodeBase>();
var count = 100;
while (currentPathTile != startNode) {
path.Add(currentPathTile);
currentPathTile = currentPathTile.Connection;
count--;
if (count < 0) throw new Exception();
Debug.Log("sdfsdf");
}
foreach (var tile in path) tile.SetColor(PathColor);
startNode.SetColor(PathColor);
Debug.Log(path.Count);
return path;
}
foreach (var neighbor in current.Neighbors.Where(t => t.Walkable && !processed.Contains(t))) {
var inSearch = toSearch.Contains(neighbor);
var costToNeighbor = current.G + current.GetDistance(neighbor);
if (!inSearch || costToNeighbor < neighbor.G) {
neighbor.SetG(costToNeighbor);
neighbor.SetConnection(current);
if (!inSearch) {
neighbor.SetH(neighbor.GetDistance(targetNode));
toSearch.Add(neighbor);
neighbor.SetColor(OpenColor);
}
}
}
}
return null;
}
}
}