-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.cs
162 lines (102 loc) · 4.17 KB
/
grid.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class grid : MonoBehaviour
{
[HideInInspector]
public Node[,] Grid;
public Vector2 GimzoSize;
public float radius;
private float diameter;
public LayerMask unwalkable;
int NodeNumberX;
int NodeNumberY;
// Start is called before the first frame update
void Start()
{
diameter = 2 * radius;
NodeNumberX = (int)(GimzoSize.x / diameter);
NodeNumberY = (int)(GimzoSize.y / diameter);
Grid = new Node[NodeNumberX, NodeNumberY];
Vector3 BottomLeft = transform.position - (Vector3.right * GimzoSize.x / 2 + Vector3.forward * GimzoSize.y / 2);
for (int x = 0; x < NodeNumberX; x++)
{
for (int y = 0; y < NodeNumberY; y++)
{
bool isWalkable = false;
Vector3 nodePos = BottomLeft + Vector3.right * (x * diameter + radius) + Vector3.forward * (y * diameter + radius);
if (Physics.CheckSphere(nodePos, radius, unwalkable)) isWalkable = true;
Grid[x, y] = new Node(nodePos, isWalkable);
}
}
}
public Node getNodeposition(Vector3 currentNode)
{
float percentX = Mathf.Clamp01((GimzoSize.x / 2 + currentNode.x) / GimzoSize.x);
float percentY = Mathf.Clamp01((GimzoSize.y / 2 + currentNode.z) / GimzoSize.y);
int indexX = Mathf.RoundToInt((NodeNumberX - 1) * percentX);
int indexY = Mathf.RoundToInt((NodeNumberY - 1) * percentY);
return Grid[indexX, indexY];
}
private bool isUnwalkable(Node[,] getNode, Vector3 getPosition)
{
foreach (var n in getNode)
{
if (n.pos == getPosition)
{
return n.unwalkable;
}
}
return false;
}
public List<Node> Neighbours(Node currentNode)
{
List<Node> Direction = new List<Node>();
float getX = currentNode.pos.x;
float getY = currentNode.pos.z;
float maxGridX = Grid[NodeNumberX - 1, NodeNumberY - 1].pos.x;
float minGridX = Grid[NodeNumberX - NodeNumberX, NodeNumberY - NodeNumberY].pos.x;
float maxGridY = Grid[NodeNumberX - 1, NodeNumberY - 1].pos.z;
float minGridY = Grid[NodeNumberX - NodeNumberX, NodeNumberY - NodeNumberY].pos.z;
Vector3[] neighbours = neighbourArray(currentNode);
for (int i = 0; i < neighbours.Length; i++)
{
if (getX > minGridX && getX < maxGridX && getY > minGridY && getY < maxGridY)
{
Direction.Add(new Node(neighbours[i], isUnwalkable(Grid, neighbours[i])));
}
}
return Direction;
}
private Vector3[] neighbourArray(Node current)
{
Vector3[] neighbours = new Vector3[8];
neighbours[0] = new Vector3(current.pos.x, current.pos.y, current.pos.z + 1f);
neighbours[1] = new Vector3(current.pos.x + 1f, current.pos.y, current.pos.z);
neighbours[2] = new Vector3(current.pos.x, current.pos.y, current.pos.z - 1f);
neighbours[3] = new Vector3(current.pos.x - 1f, current.pos.y, current.pos.z);
neighbours[4] = new Vector3(current.pos.x - 1f, current.pos.y, current.pos.z + 1f);
neighbours[5] = new Vector3(current.pos.x + 1f, current.pos.y, current.pos.z + 1f);
neighbours[6] = new Vector3(current.pos.x + 1f, current.pos.y, current.pos.z - 1f);
neighbours[7] = new Vector3(current.pos.x - 1f, current.pos.y, current.pos.z - 1f);
return neighbours;
}
void OnDrawGizmos()
{
Gizmos.DrawWireCube(transform.position, new Vector3(GimzoSize.x, 1, GimzoSize.y));
if (Grid != null)
{
foreach (var n in Grid)
{
Gizmos.color = Color.white;
if (n.unwalkable)
{
Gizmos.color = Color.red;
}
Gizmos.DrawCube(n.pos, Vector3.one * (diameter - 0.1f));
}
List<Node> getNodePos = new List<Node>();
;
}
}
}