-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPathFinding2D.cs
More file actions
185 lines (169 loc) · 6.81 KB
/
PathFinding2D.cs
File metadata and controls
185 lines (169 loc) · 6.81 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using UnityEngine;
using System.Collections.Generic;
using System;
using System.Linq;
public static class PathFinding2D
{
/**
* find a path in grid tilemaps
*/
public static List<Vector2Int> find4(Vector2Int from, Vector2Int to, Dictionary<Vector2Int, int> map, List<int> passableValues)
{
Func<Vector2Int, Vector2Int, float> getDistance = delegate (Vector2Int a, Vector2Int b)
{
float xDistance = Mathf.Abs(a.x - b.x);
float yDistance = Mathf.Abs(a.y - b.y);
return xDistance * xDistance + yDistance * yDistance;
};
Func<Vector2Int, List<Vector2Int>> getNeighbors = delegate (Vector2Int pos)
{
var neighbors = new List<Vector2Int>();
neighbors.Add(new Vector2Int(pos.x, pos.y + 1));
neighbors.Add(new Vector2Int(pos.x, pos.y - 1));
neighbors.Add(new Vector2Int(pos.x + 1, pos.y));
neighbors.Add(new Vector2Int(pos.x - 1, pos.y));
return neighbors;
};
return astar(from, to, map, passableValues, getDistance, getNeighbors);
}
/**
* find a path in hexagonal grid tilemaps (when grid rows are staggered with each other)
*/
public static List<Vector2Int> find6X(Vector2Int from, Vector2Int to, Dictionary<Vector2Int, int> map, List<int> passableValues)
{
Func<Vector2Int, Vector2Int, float> getDistance = delegate (Vector2Int a, Vector2Int b)
{
float xDistance = Mathf.Abs(a.x - b.x);
float yDistance = Mathf.Abs(a.y - b.y) * Mathf.Sqrt(3);
return xDistance * xDistance + yDistance * yDistance;
};
Func<Vector2Int, List<Vector2Int>> getNeighbors = delegate (Vector2Int pos)
{
var neighbors = new List<Vector2Int>();
neighbors.Add(new Vector2Int(pos.x + 1, pos.y + 1));
neighbors.Add(new Vector2Int(pos.x - 1, pos.y + 1));
neighbors.Add(new Vector2Int(pos.x + 1, pos.y - 1));
neighbors.Add(new Vector2Int(pos.x - 1, pos.y - 1));
neighbors.Add(new Vector2Int(pos.x - 2, pos.y));
neighbors.Add(new Vector2Int(pos.x + 2, pos.y));
return neighbors;
};
return astar(from, to, map, passableValues, getDistance, getNeighbors);
}
/**
* find a path in hexagonal grid tilemaps (when grid columns are staggered with each other)
*/
public static List<Vector2Int> find6Y(Vector2Int from, Vector2Int to, Dictionary<Vector2Int, int> map, List<int> passableValues)
{
Func<Vector2Int, Vector2Int, float> getDistance = delegate (Vector2Int a, Vector2Int b)
{
float xDistance = Mathf.Abs(a.x - b.x) * Mathf.Sqrt(3);
float yDistance = Mathf.Abs(a.y - b.y);
return xDistance * xDistance + yDistance * yDistance;
};
Func<Vector2Int, List<Vector2Int>> getNeighbors = delegate (Vector2Int pos)
{
var neighbors = new List<Vector2Int>();
neighbors.Add(new Vector2Int(pos.x + 1, pos.y + 1));
neighbors.Add(new Vector2Int(pos.x - 1, pos.y + 1));
neighbors.Add(new Vector2Int(pos.x + 1, pos.y - 1));
neighbors.Add(new Vector2Int(pos.x - 1, pos.y - 1));
neighbors.Add(new Vector2Int(pos.x, pos.y - 2));
neighbors.Add(new Vector2Int(pos.x, pos.y + 2));
return neighbors;
};
return astar(from, to, map, passableValues, getDistance, getNeighbors);
}
static List<Vector2Int> astar(Vector2Int from, Vector2Int to, Dictionary<Vector2Int, int> map, List<int> passableValues,
Func<Vector2Int, Vector2Int, float> getDistance, Func<Vector2Int, List<Vector2Int>> getNeighbors)
{
var result = new List<Vector2Int>();
if (from == to)
{
result.Add(from);
return result;
}
Node finalNode;
List<Node> open = new List<Node>();
if (findDest(new Node(null, from, getDistance(from, to), 0), open, map, to, out finalNode, passableValues, getDistance, getNeighbors))
{
while (finalNode != null)
{
result.Add(finalNode.pos);
finalNode = finalNode.preNode;
}
}
result.Reverse();
return result;
}
static bool findDest(Node currentNode, List<Node> openList,
Dictionary<Vector2Int, int> map, Vector2Int to, out Node finalNode, List<int> passableValues,
Func<Vector2Int, Vector2Int, float> getDistance, Func<Vector2Int, List<Vector2Int>> getNeighbors)
{
if (currentNode == null) {
finalNode = null;
return false;
}
else if (currentNode.pos == to)
{
finalNode = currentNode;
return true;
}
currentNode.open = false;
openList.Add(currentNode);
foreach (var item in getNeighbors(currentNode.pos))
{
if (map.ContainsKey(item) && passableValues.Contains(map[item]))
{
findTemp(openList, currentNode, item, to, getDistance);
}
}
var next = openList.FindAll(obj => obj.open).Min();
return findDest(next, openList, map, to, out finalNode, passableValues, getDistance, getNeighbors);
}
static void findTemp(List<Node> openList, Node currentNode, Vector2Int from, Vector2Int to, Func<Vector2Int, Vector2Int, float> getDistance)
{
Node temp = openList.Find(obj => obj.pos == (from));
if (temp == null)
{
temp = new Node(currentNode, from, getDistance(from, to), currentNode.gScore + 1);
openList.Add(temp);
}
else if (temp.open && temp.gScore > currentNode.gScore + 1)
{
temp.gScore = currentNode.gScore + 1;
temp.fScore = temp.hScore + temp.gScore;
temp.preNode = currentNode;
}
}
class Node:IComparable
{
public Node preNode;
public Vector2Int pos;
public float fScore;
public float hScore;
public float gScore;
public bool open = true;
public Node(Node prePos, Vector2Int pos, float hScore, float gScore)
{
this.preNode = prePos;
this.pos = pos;
this.hScore = hScore;
this.gScore = gScore;
this.fScore = hScore + gScore;
}
public int CompareTo(object obj)
{
Node temp = obj as Node;
if (temp == null) return 1;
if (Mathf.Abs(this.fScore - temp.fScore) < 0.01f) {
return this.fScore > temp.fScore ? 1 : -1;
}
if (Mathf.Abs(this.hScore - temp.hScore) < 0.01f)
{
return this.hScore > temp.hScore ? 1 : -1;
}
return 0;
}
}
}