4
4
#include < SFML/Graphics.hpp>
5
5
#include " GridCellStates.hpp"
6
6
#include " math.h"
7
+ #include < stdio.h>
7
8
9
+ using namespace std ;
8
10
using namespace sf ;
9
11
10
12
class Grid
@@ -16,6 +18,9 @@ class Grid
16
18
int cellSize; // in pixels
17
19
int outlineThickness; // in pixels
18
20
21
+ Vector2i *startPos;
22
+ Vector2i *destPos;
23
+
19
24
public:
20
25
Grid (int width, int height, int cellSize);
21
26
@@ -87,6 +92,9 @@ Grid::Grid(int width, int height, int cellSize)
87
92
gridHeight = height;
88
93
this ->cellSize = cellSize;
89
94
outlineThickness = 1 ;
95
+
96
+ startPos = NULL ;
97
+ destPos = NULL ;
90
98
}
91
99
92
100
// / <summary>
@@ -110,6 +118,9 @@ Grid::Grid(int width, int height, int cellSize, int outlineThickness)
110
118
gridHeight = height;
111
119
this ->cellSize = cellSize;
112
120
this ->outlineThickness = outlineThickness;
121
+
122
+ startPos = NULL ;
123
+ destPos = NULL ;
113
124
}
114
125
115
126
// / <summary>
@@ -196,7 +207,62 @@ bool Grid::setValAt(int x, int y, GridValue val)
196
207
{
197
208
if (validCoords (x, y))
198
209
{
210
+ // only allow one start and dest cell
211
+ if (val == GridValue::START)
212
+ {
213
+ // set old start to unoccupied
214
+ if (startPos == NULL )
215
+ {
216
+ startPos = new Vector2i (x, y);
217
+ }
218
+ else
219
+ {
220
+ // unoccupy old start pos
221
+ gridArr[startPos->x ][startPos->y ] = GridValue::UNOCCUPIED;
222
+
223
+ // set new start pos
224
+ startPos->x = x;
225
+ startPos->y = y;
226
+ }
227
+ }
228
+ else if (val == GridValue::DESTINATION)
229
+ {
230
+ // set old dest to unoccupied
231
+ if (destPos == NULL )
232
+ {
233
+ destPos = new Vector2i (x, y);
234
+ }
235
+ else
236
+ {
237
+ // unoccupy old dest pos
238
+ gridArr[destPos->x ][destPos->y ] = GridValue::UNOCCUPIED;
239
+
240
+ // set new dest pos
241
+ destPos->x = x;
242
+ destPos->y = y;
243
+ }
244
+ }
245
+ else
246
+ {
247
+ // make sure that dest/start are removed if the cell is overwritten
248
+ GridValue currVal = getValueAt (x, y);
249
+ if (startPos != NULL && currVal == GridValue::START)
250
+ {
251
+ // remove start pos
252
+ delete (startPos);
253
+ startPos = NULL ;
254
+ }
255
+ else if (destPos != NULL && currVal == GridValue::DESTINATION)
256
+ {
257
+ // remove destination pos
258
+ delete (destPos);
259
+ destPos = NULL ;
260
+ }
261
+ }
262
+
263
+ // set the value at the cell
199
264
gridArr[x][y] = val;
265
+
200
266
return true ;
201
267
}
202
268
@@ -213,25 +279,34 @@ bool Grid::setValAt(int x, int y, GridValue val)
213
279
bool Grid::setValAt (Vector2i pos, GridValue val)
214
280
{
215
281
Vector2i gridCoords (screenToGrid (pos));
216
- if (validCoords (gridCoords.x , gridCoords.y ))
217
- {
218
- gridArr[gridCoords.x ][gridCoords.y ] = val;
219
- return true ;
220
- }
221
- return false ;
282
+ return setValAt (gridCoords.x , gridCoords.y , val);
222
283
}
223
284
224
285
// / <summary>
225
286
// / Destructor for a grid object
226
287
// / </summary>
227
288
Grid::~Grid ()
228
289
{
290
+ cout << " startPos == null: " << (startPos == NULL ) << endl;
291
+ cout << " destPos == null: " << (destPos == NULL ) << endl;
292
+
293
+ // free the grid array
229
294
for (int i = 0 ; i < gridWidth; i++)
230
295
{
231
296
delete gridArr[i];
232
297
}
233
298
234
299
delete gridArr;
300
+
301
+ // free the start and dest positions
302
+ if (startPos != NULL )
303
+ {
304
+ delete (startPos);
305
+ }
306
+ if (destPos != NULL )
307
+ {
308
+ delete (destPos);
309
+ }
235
310
}
236
311
237
312
#endif
0 commit comments