-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathGrid.cs
More file actions
executable file
·198 lines (179 loc) · 6.32 KB
/
Grid.cs
File metadata and controls
executable file
·198 lines (179 loc) · 6.32 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
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Represent a grid of nodes we can search paths on.
* Based on code and tutorial by Sebastian Lague (https://www.youtube.com/channel/UCmtyQOKKmrMVaKuRXz02jbQ).
*
* Author: Ronen Ness.
* Since: 2016.
*/
using System.Collections.Generic;
namespace NesScripts.Controls.PathFind
{
/// <summary>
/// A 2D grid of nodes we use to find path.
/// The grid mark which tiles are walkable and which are not.
/// </summary>
public class Grid
{
// nodes in grid
public Node[,] nodes;
// grid size
int gridSizeX, gridSizeY;
/// <summary>
/// Create a new grid with tile prices.
/// </summary>
/// <param name="tiles_costs">A 2d array of tile prices.
/// 0.0f = Unwalkable tile.
/// 1.0f = Normal tile.
/// > 1.0f = costy tile.
/// < 1.0f = cheap tile.
/// </param>
public Grid(float[,] tiles_costs)
{
// create nodes
CreateNodes(tiles_costs.GetLength(0), tiles_costs.GetLength(1));
// init nodes
for (int x = 0; x < gridSizeX; x++)
{
for (int y = 0; y < gridSizeY; y++)
{
nodes[x, y] = new Node(tiles_costs[x, y], x, y);
}
}
}
/// <summary>
/// Create a new grid without tile prices, eg with just walkable / unwalkable tiles.
/// </summary>
/// <param name="walkable_tiles">A 2d array representing which tiles are walkable and which are not.</param>
public Grid(bool[,] walkable_tiles)
{
// create nodes
CreateNodes(walkable_tiles.GetLength(0), walkable_tiles.GetLength(1));
// init nodes
for (int x = 0; x < gridSizeX; x++)
{
for (int y = 0; y < gridSizeY; y++)
{
nodes[x, y] = new Node(walkable_tiles[x, y] ? 1.0f : 0.0f, x, y);
}
}
}
/// <summary>
/// Create the nodes grid and set size.
/// </summary>
/// <param name="width">Nodes grid width.</param>
/// <param name="height">Nodes grid height.</param>
private void CreateNodes(int width, int height)
{
gridSizeX = width;
gridSizeY = height;
nodes = new Node[gridSizeX, gridSizeY];
}
/// <summary>
/// Updates the already created grid with new tile prices.
/// </summary>
/// <returns><c>true</c>, if grid was updated, <c>false</c> otherwise.</returns>
/// <param name="tiles_costs">Tiles costs.</param>
public void UpdateGrid (float[,] tiles_costs)
{
// check if need to re-create grid
if (nodes == null ||
gridSizeX != tiles_costs.GetLength(0) ||
gridSizeY != tiles_costs.GetLength(1))
{
CreateNodes(tiles_costs.GetLength(0), tiles_costs.GetLength(1));
}
// update nodes
for (int x = 0; x < gridSizeX; x++)
{
for (int y = 0; y < gridSizeY; y++)
{
nodes[x, y].Update(tiles_costs[x, y], x, y);
}
}
}
/// <summary>
/// Updates the already created grid without new tile prices, eg with just walkable / unwalkable tiles.
/// </summary>
/// <returns><c>true</c>, if grid was updated, <c>false</c> otherwise.</returns>
/// <param name="walkable_tiles">Walkable tiles.</param>
public void UpdateGrid (bool[,] walkable_tiles)
{
// check if need to re-create grid
if (nodes == null ||
gridSizeX != walkable_tiles.GetLength(0) ||
gridSizeY != walkable_tiles.GetLength(1))
{
CreateNodes(walkable_tiles.GetLength(0), walkable_tiles.GetLength(1));
}
// update grid
for (int x = 0; x < gridSizeX; x++)
{
for (int y = 0; y < gridSizeY; y++)
{
nodes[x, y].Update(walkable_tiles[x, y] ? 1.0f : 0.0f, x, y);
}
}
}
/// <summary>
/// Get all the neighbors of a given tile in the grid.
/// </summary>
/// <param name="node">Node to get neighbors for.</param>
/// <returns>List of node neighbors.</returns>
public System.Collections.IEnumerable GetNeighbours(Node node, Pathfinding.DistanceType distanceType)
{
int x = 0, y = 0;
switch (distanceType)
{
case Pathfinding.DistanceType.Manhattan:
y = 0;
for (x = -1; x <= 1; ++x)
{
var neighbor = AddNodeNeighbour(x, y, node);
if (neighbor != null)
yield return neighbor;
}
x = 0;
for (y = -1; y <= 1; ++y)
{
var neighbor = AddNodeNeighbour(x, y, node);
if (neighbor != null)
yield return neighbor;
}
break;
case Pathfinding.DistanceType.Euclidean:
for (x = -1; x <= 1; x++)
{
for (y = -1; y <= 1; y++)
{
var neighbor = AddNodeNeighbour(x, y, node);
if (neighbor != null)
yield return neighbor;
}
}
break;
}
}
/// <summary>
/// Adds the node neighbour.
/// </summary>
/// <returns><c>true</c>, if node neighbour was added, <c>false</c> otherwise.</returns>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <param name="node">Node.</param>
/// <param name="neighbours">Neighbours.</param>
Node AddNodeNeighbour(int x, int y, Node node)
{
if (x == 0 && y == 0)
{
return null;
}
int checkX = node.gridX + x;
int checkY = node.gridY + y;
if (checkX >= 0 && checkX < gridSizeX && checkY >= 0 && checkY < gridSizeY)
{
return nodes[checkX, checkY];
}
return null;
}
}
}