-
Notifications
You must be signed in to change notification settings - Fork 0
/
GridBase.cs
198 lines (161 loc) · 5.76 KB
/
GridBase.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
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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace GridMaster
{
public class GridBase : MonoBehaviour
{
//Setting up the grid
public int maxX = 10;
public int maxY = 3;
public int maxZ = 10;
//Offset relates to the world positions only
public float offsetX = 1;
public float offsetY = 1;
public float offsetZ = 1;
public Node[, ,] grid; // our grid
public GameObject gridFloorPrefab;
public Vector3 startNodePosition;
public Vector3 endNodePosition;
public int agents;
void Start()
{
//The typical way to create a grid
grid = new Node[maxX, maxY, maxZ];
for (int x = 0; x < maxX; x++)
{
for (int y = 0; y < maxY; y++)
{
for (int z = 0; z < maxZ; z++)
{
//Apply the offsets and create the world object for each node
float posX = x * offsetX;
float posY = y * offsetY;
float posZ = z * offsetZ;
GameObject go = Instantiate(gridFloorPrefab, new Vector3(posX, posY, posZ),
Quaternion.identity) as GameObject;
//Rename it
go.transform.name = x.ToString() + " " + y.ToString() + " " + z.ToString();
//and parent it under this transform to be more organized
go.transform.parent = transform;
//Create a new node and update it's values
Node node = new Node();
node.x = x;
node.y = y;
node.z = z;
node.worldObject = go;
//BoxCastAll is only Unity 5.3+ remove this and it will play on all versions 5+
//in theory it should play with every Unity version, but i haven't tested it
RaycastHit[] hits = Physics.BoxCastAll(new Vector3(posX, posY, posZ), new Vector3(1,0,1), Vector3.forward);
for (int i = 0; i < hits.Length; i++)
{
node.isWalkable = false;
}
//then place it to the grid
grid[x, y, z] = node;
}
}
}
}
//Just a quick and dirty way to visualize the path
public bool start;
void Update()
{
if(start)
{
start = false;
//Create the new pathfinder class
// Pathfinding.Pathfinder path = new Pathfinding.Pathfinder();
//to test the avoidance, just make a node unwalkable
grid[1, 0, 1].isWalkable = false;
//pass the target nodes
Node startNode = GetNodeFromVector3(startNodePosition);
Node end = GetNodeFromVector3(endNodePosition);
//path.startPosition = startNode;
//path.endPosition = end;
//find the path
//List<Node> p = path.FindPath();
startNode.worldObject.SetActive(false);
for (int i = 0; i < agents; i++)
{
//Pathfinding.PathfindMaster.GetInstance().RequestPathfind(startNode, end, ShowPath);
}
}
}
public void ShowPath(List<Node> path)
{
foreach (Node n in path)
{
n.worldObject.SetActive(false);
}
//Debug.Log("agent complete");
}
public Node GetNode(int x, int y, int z)
{
//Used to get a node from a grid,
//If it's greater than all the maximum values we have
//then it's going to return null
Node retVal = null;
if (x < maxX && x >= 0 &&
y >= 0 && y < maxY &&
z >= 0 && z < maxZ)
{
retVal = grid[x, y, z];
}
return retVal;
}
public Node GetNodeFromVector3(Vector3 pos)
{
int x = Mathf.RoundToInt(pos.x);
int y = Mathf.RoundToInt(pos.y);
int z = Mathf.RoundToInt(pos.z);
Node retVal = GetNode(x, y, z);
return retVal;
}
//Singleton
public static GridBase instance;
public static GridBase GetInstance()
{
return instance;
}
void Awake()
{
instance = this;
}
}
}
######################
Node.cs
using UnityEngine;
using System.Collections;
namespace GridMaster
{
public class Node
{
//Node's position in the grid
public int x;
public int y;
public int z;
//Node's costs for pathfinding purposes
public float hCost;
public float gCost;
public float fCost
{
get //the fCost is the gCost+hCost so we can get it directly this way
{
return gCost + hCost;
}
}
public Node parentNode;
public bool isWalkable = true;
//Reference to the world object so we can have the world position of the node among other things
public GameObject worldObject;
//Types of nodes we can have, we will use this later on a case by case examples
public NodeType nodeType;
public enum NodeType
{
ground,
air
}
}
}