forked from RonenNess/UnityUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cs
More file actions
executable file
·180 lines (156 loc) · 5.13 KB
/
Copy pathGrid.cs
File metadata and controls
executable file
·180 lines (156 loc) · 5.13 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
/**
* 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 UnityEngine;
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="width">Grid width.</param>
/// <param name="height">Grid height.</param>
/// <param name="tiles_costs">A 2d array, matching width and height, of tile prices.
/// 0.0f = Unwalkable tile.
/// 1.0f = Normal tile.
/// > 1.0f = costy tile.
/// < 1.0f = cheap tile.
/// </param>
public Grid(int width, int height, float[,] tiles_costs)
{
gridSizeX = width;
gridSizeY = height;
nodes = new Node[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; 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="width">Grid width.</param>
/// <param name="height">Grid height.</param>
/// <param name="walkable_tiles">A 2d array, matching width and height, which tiles are walkable and which are not.</param>
public Grid(int width, int height, bool[,] walkable_tiles)
{
gridSizeX = width;
gridSizeY = height;
nodes = new Node[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
nodes[x, y] = new Node(walkable_tiles[x, y] ? 1.0f : 0.0f, x, y);
}
}
}
/// <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 bool UpdateGrid (float[,] tiles_costs) {
if (nodes != null) {
for (int x = 0; x < gridSizeX; x++)
{
for (int y = 0; y < gridSizeY; y++)
{
nodes[x, y].UpdateNode(tiles_costs[x, y], x, y);
}
}
return true;
} else {
Debug.LogError ("Nodes have not been initialized. Create a new Grid before using this method.");
return false;
}
}
/// <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 bool UpdateGrid (bool[,] walkable_tiles) {
if (nodes != null) {
for (int x = 0; x < gridSizeX; x++)
{
for (int y = 0; y < gridSizeY; y++)
{
nodes[x, y].UpdateNode(walkable_tiles[x, y] ? 1.0f : 0.0f, x, y);
}
}
return true;
} else {
Debug.LogError ("Nodes have not been initialized. Create a new Grid before using this method.");
return false;
}
}
/// <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 List<Node> GetNeighbours(Node node, Pathfinding.DistanceType distanceType)
{
List<Node> neighbours = new List<Node>();
int x = 0, y = 0;
switch (distanceType) {
case Pathfinding.DistanceType.Manhattan:
y = 0;
for (x = -1; x <= 1; ++x) {
AddNodeNeighbour (x, y, node, neighbours);
}
x = 0;
for (y = -1; y <= 1; ++y) {
AddNodeNeighbour (x, y, node, neighbours);
}
break;
case Pathfinding.DistanceType.Euclidean:
for (x = -1; x <= 1; x++) {
for (y = -1; y <= 1; y++) {
AddNodeNeighbour (x, y, node, neighbours);
}
}
break;
}
return neighbours;
}
/// <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>
private bool AddNodeNeighbour (int x, int y, Node node, List<Node> neighbours) {
if (x == 0 && y == 0)
return false;
int checkX = node.gridX + x;
int checkY = node.gridY + y;
if (checkX >= 0 && checkX < gridSizeX && checkY >= 0 && checkY < gridSizeY)
{
neighbours.Add(nodes[checkX, checkY]);
return true;
}
return false;
}
}
}