forked from asalga/Horadrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Token.pde
491 lines (396 loc) · 11.4 KB
/
Token.pde
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
A token is an object the player moves in order to make matches.
A token may have a gem within it. If the player destroys a token that
has a gem inside it, we add that to the count in ScreenGameplay. After
getting enough gems, the player can progress to the next level.
*/
public class Token{
// States the token can be in
private final int IDLE = 0;
private final int MARKED_FOR_DEATH = 1;
private final int SWAPPING = 2;
private final int FALLING = 3;
private final int DYING = 4;
private final int DEAD = 5;
// Types
public static final int TYPE_NULL = 0;
/*public static final int TYPE_RED = 1;
public static final int TYPE_GREEN = 2;
public static final int TYPE_BLUE = 3;
public static final int TYPE_WHITE = 4;
public static final int TYPE_YELLOW = 5;
public static final int TYPE_SKULL = 6;
public static final int TYPE_PURPLE = 7;*/
private final float MOVE_SPEED = TOKEN_SIZE * 5.25f; // token size per second
private final float DROP_SPEED = TOKEN_SIZE * 4;
private int state;
// TODO: find better way of doing this?
// When the token is falling, we need it to be a float
// so just define it as a float to begin with.
private int row;
private int column;
// When swapping or falling, a token is given a cell to go to.
private int rowToMoveTo;
private int colToMoveTo;
private boolean hasArrivedAtDest;
private PVector detachedPos;
// Set this and decrement until we reach zero.
private float distanceToMove;
// !!! We can refactor this
private int moveDirection;
private boolean isFillCellMarker;
private int type;
private boolean doesHaveGem;
private boolean returning;
private float scaleSize;
// Use can select up to 2 tokens before they get swapped.
private boolean isSelected;
private int score;
/*
*/
public Token(){
state = IDLE;
row = 0;
column = 0;
// TODO: need to really need to set these?
rowToMoveTo = -1;
colToMoveTo = 0;
hasArrivedAtDest = false;
detachedPos = new PVector();
moveDirection = 0;
setType(TYPE_NULL);
doesHaveGem = false;
returning = false;
scaleSize = 1.0f;
isSelected = false;
setScore(100);
}
public void setScore(int s){
if(s >= 0){
score = s;
}
}
public int getScore(){
return score;
}
/*
*/
public void setRowColumn(int row, int column){
this.row = row;
this.column = column;
}
public int getRow(){
return row;
}
public int getColumn(){
return column;
}
public int getType(){
return type;
}
/*
TODO: add check?
*/
public void setType(int t){
type = t;
}
public void setFillCellMarker(boolean b){
isFillCellMarker = b;
}
public boolean getFillCellMarker(){
return isFillCellMarker;
}
/*
When a token is selected, it is somehow outlined to show the user
it is the 'current' token.
*/
public void setSelect(boolean select){
isSelected = select;
}
/*
Immediately swap the position (row, column) of this token with another.
Used to help testing if swapping will result in a match 3.
*/
public void swap(Token other){
int tempRow = row;
int tempCol = column;
this.setRowColumn(other.row, other.column);
other.setRowColumn(tempRow, tempCol);
}
/*
Gameplay doesn't keep track if it has already killed a token, so we have
to keep track of it ourselves to make sure the ticker doesn't get reset.
It only makes sense that falling or swapping tokens cannot be killed.
*/
public void kill(){
if(state == IDLE || state == MARKED_FOR_DEATH){
state = DYING;
}
}
/*
*/
public void markForDeath(){
if(state == IDLE){
state = MARKED_FOR_DEATH;
}
}
public boolean isMarkedForDeath(){
return state == MARKED_FOR_DEATH;
}
/*
If the token is dying it is in the process of animating its
death, which might take a second or so. Once the death animation
is finished, the token is considered dead.
*/
public boolean isDying(){
return state == DYING;
}
public boolean isFalling(){
return state == FALLING;
}
public boolean isIdle(){
return state == IDLE;
}
public boolean isReturning(){
return returning;
}
public void setReturning(boolean r){
returning = r;
}
public boolean arrivedAtDest(){
return hasArrivedAtDest;
}
/*
*/
public boolean isMoving(){
return moveDirection != 0;
}
/*
TODO: fix this
*/
public void dropIntoCell(){
row = rowToMoveTo;
column = colToMoveTo;
hasArrivedAtDest = false;
distanceToMove = 0;
moveDirection = 0;
state = IDLE;
}
/*
*/
public void update(float td){
if(state == FALLING || state == SWAPPING){
float amtToMove = td * DROP_SPEED * moveDirection;
if(row == rowToMoveTo){
detachedPos.x += amtToMove;
}
else{
detachedPos.y += amtToMove;
}
distanceToMove -= abs(amtToMove);
// Don't set the state yet,
if(distanceToMove <= 0){
hasArrivedAtDest = true;
}
}
else if(state == DYING){
// Shrink the token if it is dying.
scaleSize -= td * 1.25f;
if(scaleSize <= 0){
//scaleSize = 0.0f;
state = DEAD;
}
}
}
/*
Don't use isAlive for variable name because Processing.js gets confused
with method and variable names that share the same name.
Once the sprite completes its death animation, its state gets set to dead.
*/
public boolean isAlive(){
return state != DEAD;
}
public void setHasGem(boolean hasGem){
doesHaveGem = hasGem;
}
/*
Once the token is destroyed, the game screen will increment
the number of gems the player has if this token had a gem.
*/
public boolean hasGem(){
return doesHaveGem;
}
/*
Token needs to be valid and idle for it to be swapped.
*/
public boolean canBeSwapped(){
if(type == TYPE_NULL || state != IDLE || row < START_ROW_INDEX){
return false;
}
return true;
}
/*
* The token can only be matched if its just sitting there. If it is dying/moving
* then we can't have that token get matched.
*/
public boolean canBeMatched(){
if(type == TYPE_NULL){
return false;
}
if(state == MARKED_FOR_DEATH || state == IDLE){
return true;
}
//if(state != IDLE || type == TYPE_NULL){
// return false;
//}
return false;
}
public void swapTo(int r, int c){
state = SWAPPING;
animateTo(r, c);
}
public void fallTo(int r, int c){
state = FALLING;
animateTo(r, c);
}
/*
When the tokens fall, they are given new locations
If they are already falling, they can fall to a new 'lower' location.
*/
private void animateTo(int r, int c){
// If the token was already moving to another destination,
// we need to assign it a new place to go to.
// If we call animateTo, but the token is already falling, it means
// the player made a match below this falling token. We are given the new
// row and column, but need to recalculate the distance it needs to travel.
if(state == FALLING && distanceToMove > 0){
int oldRowToMoveTo = rowToMoveTo;
int newRowToMoveTo = r;
int distBetweenDsts = (newRowToMoveTo - oldRowToMoveTo) * TOKEN_SIZE;
// Find the distance between the src and dst in the original movement
float dst = (oldRowToMoveTo - r) * TOKEN_SIZE;
rowToMoveTo = r;
distanceToMove = distBetweenDsts + distanceToMove;
}
else{
// column row swapped here!
// TODO: fix !!! why are we dividing by 2? literal 8?
int detachedX = (int)(column * (BOARD_W_IN_PX / 8.0f) + ((BOARD_W_IN_PX / 8.0f)/2.0f ));
int detachedY = (int)((row-8) * (BOARD_H_IN_PX / 8.0f) + ((BOARD_H_IN_PX / 8.0f)/2.0f ));
detachedPos = new PVector(detachedX, detachedY);
rowToMoveTo = r;
colToMoveTo = c;
// Either we are moving the token vertically or horizontally
if(c == column){
int rowDiff = rowToMoveTo - row;
distanceToMove = abs(rowDiff) * TOKEN_SIZE;
moveDirection = rowDiff / abs(rowDiff);
}
else if(r == row){
int columnDiff = colToMoveTo - column;
distanceToMove = abs(columnDiff) * TOKEN_SIZE;
moveDirection = columnDiff / abs(columnDiff);
}
}
}
/*
Calculates the air speed velocity of an unladen swallow.
*/
public void draw(){
int x = 0;
int y = 0;
if(state == FALLING || state == SWAPPING){
x = (int)detachedPos.x;
y = (int)detachedPos.y;
}
else{
x = (int)(column * (BOARD_W_IN_PX / 8.0f) + ((BOARD_W_IN_PX / 8.0f)/2.0 ));
// 8 here is the number of visible rows. We need to essentially move the visible tokens up
// where the invisible ones would be drawn.
y = (int)((row-8) * (BOARD_H_IN_PX / 8.0f) + ((BOARD_H_IN_PX / 8.0f)/2.0 ));
}
// Draw a rectangle around the selected tokens
if(isSelected){
pushStyle();
fill(33, 66, 99);
rectMode(CENTER);
fill(255, 0, 0, 255);
strokeWeight(2);
stroke(255);
rect(x, y, TOKEN_SIZE, TOKEN_SIZE);
popStyle();
}
/*if(canBeMatched()){
pushStyle();
fill(99, 66, 33,33);
rectMode(CENTER);
//fill(255, 0, 0, 255);
strokeWeight(0);
stroke(255);
rect(x, y, TOKEN_SIZE, TOKEN_SIZE);
popStyle();
}*/
// pjs giving issues here
pushMatrix();
if(usingPjs){
resetMatrix();
}
//translate(START_X, START_Y);
translate(x, y);
rectMode(CENTER);
// draw a grey box to easily identify dead or null tokens
if(DEBUG_ON){//} && (state == DEAD || state == DYING || type == TYPE_NULL)){
pushStyle();
fill(128,128);
rect(0, 0, TOKEN_SIZE, TOKEN_SIZE);
popStyle();
}
// Draws an outline around all tokens
if(DEBUG_ON){
pushStyle();
noFill();
stroke(255,64);
rect(0, 0, TOKEN_SIZE, TOKEN_SIZE);
popStyle();
}
/*if(row < START_ROW_INDEX && DEBUG_ON){
pushStyle();
rectMode(CENTER);
fill(255, 255, 255, 32);
rect(0, 0, TOKEN_SIZE, TOKEN_SIZE);
popStyle();
}*/
// We need to somehow distinguish tokens that have gems.
if(hasGem() && state != DYING){
pushStyle();
fill(255 * sin(frameCount/10.0), 60, 90, 128);
//rect(0, 0, TOKEN_SIZE, TOKEN_SIZE);
ellipseMode(CENTER);
ellipse(0,0, TOKEN_SIZE, TOKEN_SIZE);
popStyle();
}
//
if(state == DYING){
scale(scaleSize >= 0 ? scaleSize : 0);
}
if(state != DEAD && type != TYPE_NULL){
AssetStore store = AssetStore.Instance(globalApplet);
pushStyle();
imageMode(CENTER);
image(store.get(type), 0, 0);
popStyle();
}
popMatrix();
}
/*
Instead of directly checking the type between tokens, we
have a method that just asks if it can match with whatever. This
allows us to later on match tokens with wildcards.
*/
public boolean matchesWith(int other){
if(type == TYPE_NULL){
return false;
}
return type == other;
}
}