-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimateObject.java
More file actions
358 lines (342 loc) · 11.8 KB
/
Copy pathAnimateObject.java
File metadata and controls
358 lines (342 loc) · 11.8 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
* All moving objects in the Maze inherit from this super class
*
* Methods:
* move(): changes the position of the object in given direction
* getGridX(): returns the horizontal block position
* getGridX(x): returns horizontal block position for given x-coord
* getGridY(): returns vertical block position
* getGridY(y); returns vertical block position for given y-coord
* blockTraversed: checks if object has changed block position
* canMove(): checks if character will ram into wall or can move
* in a given direction
*
*
* @author Amar and Avi Ruthen
* @version May 22, 2020
*/
public class AnimateObject {
int xBlock;
int yBlock;
int squareSize;
int sizeAdj;
int moves;
int gridHeight, gridWidth;
boolean inMotion = false;
// instance variables - replace the example below with your own
public AnimateObject(int squareSize, int gridHeight, int gridWidth) {
sizeAdj = squareSize / 4;
xBlock = sizeAdj;
yBlock = 600 - (3 * sizeAdj);
this.gridHeight = gridHeight;
this.gridWidth = gridWidth;
this.squareSize = squareSize;
}
/*public void move(int direction) {
int formerX = xBlock;
int formerY = yBlock;
switch (direction) {
case 0:
yBlock--;
break;
case 1:
xBlock++;
break;
case 2:
yBlock++;
break;
case 3:
xBlock--;
break;
default:
break;
}
if (blockTraversed(formerX, formerY))
moves++;
}*/
/*public boolean canMove(int[][][] squareMoves, int direction) {
//if ((yBlock + (3 * sizeAdj)) % squareSize != 0 || (xBlock - sizeAdj) % squareSize != 0)
//return true;
for (int i = 0; i < squareMoves[getGridX()][getGridY()].length; i++) {
if (squareMoves[getGridX()][getGridY()][i] == direction)
return true;
}
return false;
}*/
/**
* Returns the column the moving object is occupying
*
* @return the column value
*/
public int getGridX() {
return (xBlock + squareSize / 4) / squareSize;
}
/**
* Returns the column for the given x coordinate
*
* @param x : the x coordinate to test
*
* @return the column value
*/
public int getGridX(int x) {
return (x + squareSize / 4) / squareSize;
}
/**
* Returns the row the moving object is occupying
*
* @return the row value
*/
public int getGridY() {
return gridHeight - 1 - (yBlock + squareSize / 4) / squareSize;
}
/**
* Returns the row for the given y coordinate
*
* @return the row value
*/
public int getGridY(int y) {
return gridHeight - 1 - (y + squareSize / 4) / squareSize;
}
/**
* Determines if object has fully traversed a block
*
* @return true if the block the character now occupies
* Is different from before
*/
public boolean blockTraversed(int formerX, int formerY) {
if (getGridX() == getGridX(formerX) && getGridY() == getGridY(formerY))
return false;
return true;
}
/**
* Enables object to change position
*
* @param direction the direction to travel
* @param difficulty (needed for the size of the block)
* @param difCounter (needed to set the speed of the object)
*
* @return difCounter the integer that controls which ticks to move
*/
public int move(int direction, String difficulty, int difCounter) {
int formerX = xBlock;
int formerY = yBlock;
if (difCounter == 0 || difCounter == 1)
{
switch (direction) {
case 0:
yBlock--;
break;
case 1:
xBlock++;
break;
case 2:
yBlock++;
break;
case 3:
xBlock--;
break;
default:
break;
}
if (blockTraversed(formerX, formerY))
moves++;
}
//Based on the difficulty, the object will either move
//Every timer call or some fraction less
switch (difficulty)
{
case "Easy":
difCounter = 0;
break;
case "Medium":
difCounter += 2;
difCounter %= 2;
break;
case "Hard":
difCounter += 2;
difCounter %= 3;
default:
break;
}
return difCounter;
}
/**
* Determines if a moving object can move in the given direction
* @param squareMoves All possible move values at a given square
* @param direction Intended travel location
* @return
*/
public boolean canMove(int[][][] squareMoves, int direction) {
//object cannot move
if (!inMotion)
return false;
//central location of the object
int charCenterX = (xBlock + squareSize/4) % squareSize;
int charCenterY = (yBlock + squareSize/4) % squareSize;
boolean canMove = false;
//movement direction
switch (direction)
{
//no intended movement
case -1:
return false;
//NORTH
case 0:
//object is centered on the bottom portion of the square
if (charCenterY > squareSize/4)
{
//edge of the circle within the square
if (charCenterX >= squareSize/4 && charCenterX <= 3*squareSize/4)
return true;
canMove = true;
}
//edge of the object passes through top of the square
if (charCenterY <= squareSize/4)
{
//checks if object can move up in that direction
for (int i = 0; i < squareMoves[0][0].length; i++)
{
if (squareMoves[getGridX()][getGridY()][i] == 0)
{
return true;
}
}
}
//left edge of the circle is beyond square boundary
if (charCenterX < squareSize/4)
{
//opening on the left side
for (int i = 0; i < squareMoves[0][0].length; i++)
{
if (squareMoves[getGridX()][getGridY()][i] == 3 && canMove)
return true;
}
}
//right edge of the circle is beyond square boundary
if (charCenterX > 3*squareSize/4)
{
//opening on the right side
for (int i = 0; i < squareMoves[0][0].length; i++)
{
if (squareMoves[getGridX()][getGridY()][i] == 1 && canMove)
return true;
}
}
break;
//EAST
case 1:
//object is centered on the left portion of the square
if (charCenterX < 3*squareSize/4)
{
//edge of the circle within the square
if (charCenterY >= squareSize/4 && charCenterY <= 3*squareSize/4)
return true;
canMove = true;
}
//edge of the object passes through right of the square
if (charCenterX >= 3*squareSize/4)
{
//checks if object can move right in that direction
for (int i = 0; i < squareMoves[0][0].length; i++)
{
if (squareMoves[getGridX()][getGridY()][i] == 1)
{
return true;
}
}
}
//upper edge of circle is beyond boundary
if (charCenterY < squareSize/4)
{
for (int i = 0; i < squareMoves[0][0].length; i++)
{
if (squareMoves[getGridX()][getGridY()][i] == 0 && canMove)
return true;
}
}
//lower edge of circle is beyond boundary
if (charCenterY > 3*squareSize/4)
{
for (int i = 0; i < squareMoves[0][0].length; i++)
{
if (squareMoves[getGridX()][getGridY()][i] == 2 && canMove)
return true;
}
}
break;
//SOUTH (similar process as NORTH)
case 2:
if (charCenterY < 3*squareSize/4)
{
if (charCenterX >= squareSize/4 && charCenterX <= 3*squareSize/4)
return true;
canMove = true;
}
if (charCenterY >= 3*squareSize/4)
{
for (int i = 0; i < squareMoves[0][0].length; i++)
{
if (squareMoves[getGridX()][getGridY()][i] == 2)
{
return true;
}
}
}
if (charCenterX < squareSize/4)
{
for (int i = 0; i < squareMoves[0][0].length; i++)
{
if (squareMoves[getGridX()][getGridY()][i] == 3 && canMove)
return true;
}
}
if (charCenterX > 3*squareSize/4)
{
for (int i = 0; i < squareMoves[0][0].length; i++)
{
if (squareMoves[getGridX()][getGridY()][i] == 1 && canMove)
return true;
}
}
break;
//WEST (similar process as EAST)
case 3:
if (charCenterX > squareSize/4)
{
if (charCenterY >= squareSize/4 && charCenterY <= 3*squareSize/4)
return true;
canMove = true;
}
if (charCenterX <= squareSize/4)
{
for (int i = 0; i < squareMoves[0][0].length; i++)
{
if (squareMoves[getGridX()][getGridY()][i] == 3)
{
return true;
}
}
}
if (charCenterY < squareSize/4)
{
for (int i = 0; i < squareMoves[0][0].length; i++)
{
if (squareMoves[getGridX()][getGridY()][i] == 0 && canMove)
return true;
}
}
if (charCenterY > 3*squareSize/4)
{
for (int i = 0; i < squareMoves[0][0].length; i++)
{
if (squareMoves[getGridX()][getGridY()][i] == 2 && canMove)
return true;
}
}
break;
default:
break;
}
return false; //default is cannot move
}
}