-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cs
More file actions
271 lines (265 loc) · 8.04 KB
/
Grid.cs
File metadata and controls
271 lines (265 loc) · 8.04 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/*it would be wise to create grids whos dimensions (-2) are factors of the blocksize*/
public class Grid
{
private Vector2 size, position = new Vector2 (0.0f, 0.0f);
private Vector2 xyMin = new Vector2 (1.0f, 1.0f);
private Vector2 xyMax;
private GPoint[,] gPoints;
private GPoint.PType gridType;
public Grid (Grid g)
{//Vector2 setSize, Vector2 setPosition, GPoint.PType fillType
xyMax = g.xyMax;
gridType = g.gridType;
//bump up the size to create room for the buffer;
size = g.size;
//translate
position = g.position;
//Debug.Log ("creating a grid FROM a grid " + g.GetSize ().x + "by" + g.GetSize ().y + " grid, filled with " + g.gridType);
//Debug.Log ("actually a " + size.x + "by" + size.y + " grid");
//create [,] of empty points
Initialize ();
//add a buffer
MarkBuffer ();
//fill the center
//Fill (gridType);
UpdatePoints (g.GetPoints ());
}
public Grid (Vector2 setSize, Vector2 setPosition, GPoint.PType fillType)
{
xyMax = setSize + new Vector2 (1.0f, 1.0f);
gridType = fillType;
//bump up the size to create room for the buffer;
SetSize (setSize + new Vector2 (2.0f, 2.0f));
//translate
SetPosition (setPosition);
Debug.Log ("creating a " + setSize.x + "by" + setSize.y + " grid, filled with " + fillType);
//Debug.Log ("actually a " + size.x + "by" + size.y + " grid");
//create [,] of empty points
Initialize ();
//add a buffer
MarkBuffer ();
//fill the center
Fill (gridType);
}
public GPoint.PType GetGridType ()
{
return gridType;
}
private void Initialize ()
{
gPoints = new GPoint[(int)size.x, (int)size.y];
//Debug.Log ("initializing an empty grid");
for (int ix = 0; ix < (int)size.x; ix+=1) {
for (int iy = 0; iy < (int)size.y; iy+=1) {
Vector2 pos = new Vector2 ((float)ix, (float)iy);
gPoints [ix, iy] = new GPoint (pos, GPoint.PType.EMPTY);
//Debug.Log ("tile added at" + ix + "," + iy + "\nwith data" + GetPoint (pos).GetPosition ().x + GetPoint (pos).GetPosition ().x + GPoint.PType.EMPTY);
}
}
}
private void MarkBuffer ()
{
//Debug.Log ("creating the 1 tile buffer");
//make the x lines
for (int ix = 0; ix < (int)size.x; ix++) {
//where to add a point
Vector2 pos = new Vector2 ((float)ix, 0.0f);
//bottom row
gPoints [(int)pos.x, (int)pos.y].UpdateType (GPoint.PType.BUFFER);
//move up
pos.y = size.y - 1.0f;
//move up
gPoints [(int)pos.x, (int)pos.y].UpdateType (GPoint.PType.BUFFER);
}
//make the y lines
for (int iy = 0; iy < (int)size.y; iy++) {
//where to add a point
Vector2 pos = new Vector2 (0.0f, (float)iy);
//left collumn
gPoints [(int)pos.x, (int)pos.y].UpdateType (GPoint.PType.BUFFER);
//move right
pos.x = size.x - 1.0f;
//right collumn
gPoints [(int)pos.x, (int)pos.y].UpdateType (GPoint.PType.BUFFER);
}
}
public void Fill (GPoint.PType fillType)
{
//Debug.Log ("filling the grid with" + fillType);
//this method DOES NOT start at the 0,0 origin but rather at xymin and ends at xymax;
for (float ix = xyMin.x; ix < xyMax.x; ix+=1.0f) {
for (float iy = xyMin.y; iy <= xyMax.y; iy+=1.0f) {
Vector2 pos = new Vector2 (ix, iy);
gPoints [(int)pos.x, (int)pos.y].UpdateType (fillType);
}
}
}
public void Reset ()
{
Fill (gridType);
}
public void Clear ()
{
Fill (GPoint.PType.EMPTY);
}
public List<GPoint> GetPoints ()
{
List<GPoint> points = new List<GPoint> ();
for (float ix = xyMin.x; ix < xyMax.x; ix+=1.0f) {
for (float iy = xyMin.y; iy < xyMax.y; iy+=1.0f) {
Vector2 pos = new Vector2 (ix, iy);
GPoint gpAtIndex = GetPoint (pos);
if (gpAtIndex.GetPType () != GPoint.PType.EMPTY)
//Debug.Log ("adding point val to list: x" + gpAtIndex.GetPosition ().x + "y" + gpAtIndex.GetPosition ().x);
points.Add (gpAtIndex);
/*gPoints [(int)pos.x, (int)pos.y].UpdateType (fillType);*/
}
}
return points;
}
public List<GPoint> GetPoints (GPoint.PType pointType)
{
List<GPoint> points = new List<GPoint> ();
for (float ix = xyMin.x; ix < xyMax.x; ix+=1.0f) {
for (float iy = xyMin.y; iy < xyMax.y; iy+=1.0f) {
Vector2 pos = new Vector2 (ix, iy);
GPoint gpAtIndex = GetPoint (pos);
if (gpAtIndex.GetPType () != GPoint.PType.EMPTY && gpAtIndex.GetPType () == pointType) {
points.Add (gpAtIndex);
}
}
}
return points;
}
public GPoint GetPoint (Vector2 position)
{
if (PositionInBounds (position)) {
return gPoints [(int)position.x, (int)position.y];
}
return new GPoint (position, GPoint.PType.EMPTY);
}
public GPoint GetPoint (float x, float y)
{
if (PositionInBounds (new Vector2 (x, y)))
return gPoints [(int)x, (int)y];
return new GPoint (position, GPoint.PType.EMPTY);
}
public GPoint GetPoint (int x, int y)
{
if (PositionInBounds (new Vector2 ((float)x, (float)y)))
return gPoints [x, y];
return new GPoint (position, GPoint.PType.EMPTY);
}
public void UpdatePoint (Vector2 pos, GPoint.PType newType)
{
if (PositionInBounds (pos))
gPoints [(int)pos.x, (int)pos.y].UpdateType (newType);
}
public void UpdatePoints (List<GPoint> pointsToUpdate)
{
foreach (GPoint gp in pointsToUpdate) {
UpdatePoint (gp.GetPosition (), gp.GetPType ());
}
}
public void UpdatePointDirection (Vector2 pos, GPoint.Direction dir)
{
gPoints [(int)pos.x, (int)pos.y].SetDirection (dir);
}
public void UpdatePoints (Grid g)
{
foreach (GPoint gp in g.GetPoints()) {
UpdatePoint ((gp.GetPosition () + g.GetPosition ()), gp.GetPType ());
}
}
public Vector2 GetXYMin ()
{
return xyMin;
}
public Vector2 GetXYMax ()
{
return xyMax;
}
public bool PositionInBounds (Vector2 pos)
{
if (pos.x >= xyMin.x && pos.x <= xyMax.x) {
if (pos.y >= xyMin.y && pos.y <= xyMax.y)
return true;
}
return false;
}
/* public bool ScanPositionInBounds (Vector2 pos)
{
if (pos.x >= xyMin.x && pos.x <= xyMax.x) {
if (pos.y >= xyMin.y && pos.y <= xyMax.y)
return true;
}
return false;
}*/
public void SetPosition (Vector2 pos)
{
position = RoundVector (pos);
}
public void SetSize (Vector2 newSize)
{
size = RoundVector (newSize);
}
public void Draw ()
{
//draw the buffer
Gizmos.color = Color.white;
Gizmos.DrawWireCube (GetWorldPosition (), new Vector3 (size.x, 1.0f, size.y));
//draw the useable space
Gizmos.color = Color.red;
Gizmos.DrawWireCube (GetWorldPosition (), new Vector3 (size.x - 2.0f, 1.0f, size.y - 2.0f));
Gizmos.DrawIcon (GetWorldPosition (), "grid_icon.png", false);
}
public Vector2 RoundVector (Vector2 vecToRound)
{
Vector2 roundedVector;
int x, y;
x = (int)vecToRound.x;
y = (int)vecToRound.y;
roundedVector = new Vector2 (x, y);
return roundedVector;
}
public Vector2 GetPosition ()
{
return position;
}
public Vector3 GetWorldPosition ()
{
Vector3 worldPos = new Vector3 ((int)size.x / 2.0f, 0.0f, (int)size.y / 2.0f);
Vector3 offsetPos = new Vector3 ((int)position.x, 0.0f, (int)position.y);
offsetPos -= new Vector3 (1.0f, 0.0f, 1.0f);
worldPos += offsetPos;
return worldPos;
}
public void ShowGridDataInConsole ()
{
string gridString = "";
for (int ix = 0; ix < (int)size.x; ix+=1) {
for (int iy = 0; iy < (int)size.y; iy+=1) {
Vector2 pos = new Vector2 ((float)ix, (float)iy);
switch (GetPoint (pos).GetPType ()) {
case GPoint.PType.BUFFER:
gridString += "B";
break;
case GPoint.PType.DEBUG:
gridString += "G";
break;
default:
break;
}
}
Debug.Log (gridString);
gridString = "";
}
}
public Vector2 GetSize ()
{
return size - new Vector2 (2.0f, 2.0f);
}
}