-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScreenGameplay.pde
676 lines (522 loc) · 16.5 KB
/
ScreenGameplay.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
This screen is the main gameplay screen.
*/
public class ScreenGameplay extends IScreen implements Subject{
ArrayList<LayerObserver> layerObserver;
PImage bk;
PImage bk2;
PImage shaderTexture;
PShader backgroundShader;
// time it takes for the tokens above the ones that were destroyed to start falling down.
private float DELAY_PAUSE = 2.5f; //0.035f;
// Only for debugging to see which token would be selected
// by the player.
public boolean drawBoxUnderCursor;
Debugger debug;
Ticker levelCountDownTimer;
Ticker timer;
private int tokensDestroyed = 0;
private boolean isPaused = false;
private int gemsWonByPlayer = 0;
private int gemsRequiredForLevel;
private boolean allowInputWhenTokensFalling;
private BoardModel boardModel;
// This is immediately incremented in the ctor by calling goToNextLevel().
int currLevel = 0;
// User can only be in the process of swapping two tokens
// at any given time.
Token swapToken1 = null;
Token swapToken2 = null;
Token currToken1 = null;
Token currToken2 = null;
int score = 0;
private float opacity = 0.0;
/**
*/
public void addObserver(LayerObserver o){
layerObserver.add(o);
// recalculate indices
}
/*
*/
public void removeObserver(LayerObserver o){
// recalc?
}
/*
*/
public void notifyObservers(){
for(int i = 0; i < layerObserver.size(); i++){
layerObserver.get(i).notifyObserver();
}
}
/*
*/
public void setAllowInputWhenTokensFalling(boolean b){
allowInputWhenTokensFalling = b;
}
/*
*/
ScreenGameplay(){
timer = new Ticker();
allowInputWhenTokensFalling = true;
LayerObserver hudLayer = new HUDLayer(this);
gemsRequiredForLevel = gemsRequired[0];
//PImage tex1 = loadImage("../ref/arch.png");
shaderTexture = loadImage("data/images/background/space1.png");
backgroundShader = loadShader("data/shaders/plane_deformation.frag","data/shaders/plane_deformation.vert");
bk = loadImage("data/images/boards/board.png");
//bk2 = loadImage("data/images/background/background.png");
bk2 = loadImage("data/images/boards/board_tightlypacked.png");
//
layerObserver = new ArrayList<LayerObserver>();
addObserver(hudLayer);
/*Layer bkLayer = new BackgroundLayer();
layers.add(bkLayer);
Layer hudLayer = new HUDLayer();
observers.add(hudLayer);*/
Keyboard.lockKeys(new int[]{KEY_M});
boardModel = new BoardModel();
boardModel.setNumGemsAllowedAtOnce(2);
debug = new Debugger();
// lock P for pause
Keyboard.lockKeys(new int[]{KEY_P});
drawBoxUnderCursor = false;
deselectCurrentTokens();
// levelCountDownTimer is set in this method.
goToNextLevel();
}
/*
*/
public void draw(){
background(0);
//
if(using3DMode){
shader(backgroundShader);
backgroundShader.set("width", (float)CANVAS_WIDTH);
backgroundShader.set("height", (float)CANVAS_HEIGHT);
backgroundShader.set("iChannel0", shaderTexture);
backgroundShader.set("time", frameCount/100.0);
//textureMode(NORMAL);
//noStroke();
beginShape();
vertex(0, 0);
vertex(width, 0);
vertex(width, height);
vertex(0, height);
endShape();
resetShader();
}
// Breaks HUD
/*tint(opacity);
opacity += 1.0f;
if(opacity > 255){
opacity = 255;
}*/
pushMatrix();
translate(START_X, START_Y);
//rect(0,0, BOARD_W_IN_PX, BOARD_H_IN_PX);
// Draw the board background image
pushStyle();
imageMode(CORNER);
// Offset the image slighly so that it lines up with the grid of tokens.
//image(bk, -13, -16);//for diablo board
image(bk2, 0, 0);
popStyle();
// Draw the debug board with gridlines
//pushMatrix();
//translate(0, 300);
//fill(33,66,99,100);
//strokeWeight(1);
//rect(START_X, START_Y, BOARD_W_IN_PX, BOARD_H_IN_PX);
//rect(0, 0, BOARD_W_IN_PX, BOARD_H_IN_PX);
//popMatrix();
ArrayList<Token> dyingTokens = boardModel.getDyingTokens();
// The dying tokens shrink and the falling tokens get rendered on top of them.
for(int i = 0; i < dyingTokens.size(); i++){
dyingTokens.get(i).draw();
}
if(swapToken1 != null){
swapToken1.draw();
}
if(swapToken2 != null){
swapToken2.draw();
}
BoardModel.Iterator iter = boardModel.getIterator();
while(iter.next()){
iter.item().draw();
}
// In some cases it is necessary to see the non-visible tokens
// above the visible board. Other cases, I want that part covered.
// for example, when tokens are falling.
if(DEBUG_ON == false){
pushStyle();
rectMode(CORNER);
fill(0);
noStroke();
rect(0, -100, 260, 100);
//rect(START_X-150, -237, 250, 222);
popStyle();
}
popMatrix();
// HACK: This line is here as a workaround a bug in Processing.js
// If removed, the board would translate diagonally on the canvas.
// when tokens are removed.
if(usingPjs){
resetMatrix();
}
if(layerObserver != null){
for(int i = 0; i < layerObserver.size(); i++){
layerObserver.get(i).draw();
}
}
//debug.addString("FPS: " + frameRate);
debug.draw();
}
/**
*/
public void update(){
//}
//public void fuckyou(){
// TODO: fix
// Need to notify that we are paused so the HUD can draw an overlay
if(isPaused){
notifyObservers();
return;
}
// INSTANT DEATH - for some reason, can't set it to 1 second
if(Keyboard.isKeyDown(KEY_I)){
//levelCountDownTimer.setTime(0, 3);
//gemsWonByPlayer = 50;
}
// NEW BOARD
if(Keyboard.isKeyDown(KEY_N)){
// Generating a new board while swapping is unlikely, but prevent it anyway, just in case.
if(boardModel.getDyingTokens().size() == 0 && swapToken1 == null && swapToken2 == null){
boardModel.generateNewBoardWithDyingAnimation(true);
}
}
timer.tick();
float td = timer.getDeltaSec();
// Once the player meets their quota...
if(gemsWonByPlayer >= gemsRequiredForLevel && boardModel.hasMovement() == false){
gemsWonByPlayer = 0;
if(currLevel < NUM_LEVELS){
goToNextLevel();
}
else{
screens.transitionTo("win");
}
}
debug.clear();
if(levelCountDownTimer != null){
levelCountDownTimer.tick();
}
// Now, update the two tokens that the user has swapped
if(swapToken1 != null){
swapToken1.update(td);
swapToken2.update(td);
if(swapToken1.arrivedAtDest() && swapToken1.isReturning() == false && swapToken1.isMoving()){
//
// Need to drop into the cells before check if it was indeed a valid swap
swapToken1.dropIntoCell();
boardModel.setToken(swapToken1.getRow(), swapToken1.getColumn(), swapToken1);
swapToken2.dropIntoCell();
boardModel.setToken(swapToken2.getRow(), swapToken2.getColumn(), swapToken2);
int matches = boardModel.getNumCosecutiveMatches(swapToken1, swapToken2);
// If it was not a valid swap, animate it back from where it came.
if(matches < 3){
int r1 = swapToken1.getRow();
int c1 = swapToken1.getColumn();
int r2 = swapToken2.getRow();
int c2 = swapToken2.getColumn();
swapToken1.swapTo(r2, c2);
swapToken2.swapTo(r1, c1);
swapToken1.setReturning(true);
swapToken2.setReturning(true);
soundManager.playFailSwapSound();
}
// Swap was valid
else{
swapToken1 = swapToken2 = null;
int test = boardModel.markTokensForRemoval(8, 15);
boardModel.removeMarkedTokens(true);
soundManager.playMatchSound();
boardModel.dropTokens();
deselectCurrentTokens();
//soundManager.playSuccessSwapSound();
}
}
//
else if(swapToken1.arrivedAtDest() && swapToken1.isReturning()){
swapToken1.dropIntoCell();
boardModel.setToken(swapToken1.getRow(), swapToken1.getColumn(), swapToken1);
swapToken2.dropIntoCell();
boardModel.setToken(swapToken2.getRow(), swapToken2.getColumn(), swapToken2);
swapToken1.setReturning(false);
swapToken2.setReturning(false);
swapToken1 = swapToken2 = null;
}
}
ArrayList<Token> dyingTokens = boardModel.getDyingTokens();
// TODO: refactor?
// Iterate over all the tokens that are dying and increase the score.
for(int i = 0; i < dyingTokens.size(); i++){
Token dyingToken = dyingTokens.get(i);
dyingToken.update(td);
if(dyingToken.isAlive() == false){
if(dyingToken.hasGem()){
gemsWonByPlayer++;
boardModel.reduceGemCount(1);
}
addToScore(dyingToken.getScore());
dyingTokens.remove(i);
tokensDestroyed++;
}
}
boardModel.update(td);
// Probably the most logical place to ensure the number of gems on the board is right here
// Doing it on level start is actually tricker, since the only tokens that exist are the ones at the top
// that are faling down.
if(tokensDestroyed > 0){
//addGemsToQueuedTokens();
boardModel.ensureGemCount();
}
if(usingPjs){
resetMatrix();
}
// Add a leading zero if seconds is a single digit
String secStr = "";
int seconds = (int)levelCountDownTimer.getTotalTime() % 60;
//
if((int)levelCountDownTimer.getTotalTime() == 0){
screens.transitionTo("gameover");
}
if(seconds <= 9){
secStr = Utils.prependStringWithString( "" + seconds, "0", 2);
}
else{
secStr = "" + seconds;
}
notifyObservers();
}
public String getName(){
return "gameplay";
}
public int getRowIndex(){
return (int)map(mouseY, START_Y, START_Y + BOARD_H_IN_PX, START_ROW_INDEX, BOARD_ROWS);
}
public int getColumnIndex(){
return (int)map(mouseX, START_X, START_X + BOARD_W_IN_PX, 0, BOARD_COLS);
}
/*
*
*/
public void mousePressed(){
if(isPaused){
return;
}
//
if(allowInputWhenTokensFalling == false && boardModel.hasMovement()){
return;
}
// convert the mouse coords to grid coordinates
int r = getRowIndex();
int c = getColumnIndex();
// We can get some wacky values when clicking outside of the
// board. If the player does that, just ignore the click.
if( r >= BOARD_ROWS || c >= BOARD_COLS || r < 0 || c < 0){
return;
}
// Prevent the user from trying to swap with anything else until
// the last swap animation is done. This prevents the user from
// trying to swap with a token that's already being swapped.
if(swapToken1 != null && swapToken2 != null){
return;
}
// Player hasn't selected the first token yet.
if(currToken1 == null){
currToken1 = boardModel.getToken(r, c);
// If the token the player selected is actually null (an empty cell) or is
// actually falling down, then back out.
if(currToken1.canBeSwapped() == false){
currToken1 = null;
return;
}
currToken1.setSelect(true);
return;
}
// The real work is done once we know what to swap with.
if(currToken2 == null){
currToken2 = boardModel.getToken(r, c);
// Same as a few lines above.
if(currToken2.canBeSwapped() == false){
currToken2 = null;
return;
}
// User clicked on a token that's too far to swap with the one already selected
// In that case, what they are probably doing is starting the 'swap process' over.
if( boardModel.isCloseEnoughForSwap(currToken1, currToken2) == false){
currToken1.setSelect(false);
currToken1 = currToken2;
currToken1.setSelect(true);
currToken2 = null;
}
else{
animateSwapTokens(currToken1, currToken2);
}
}
}
/**
* To swap tokens, players will click/tap a token then drag to the token
* they want to swap with.
*/
public void mouseDragged(){
// convert the mouse coords to grid coordinates
int r = getRowIndex();
int c = getColumnIndex();
if( r >= BOARD_ROWS || c >= BOARD_COLS || r < 0 || c < 0){
return;
}
// Player have had to select the first token before dragging.
if(currToken1 != null && currToken2 == null){
//
if(c != currToken1.getColumn() || r != currToken1.getRow()){
currToken2 = boardModel.getToken(r, c);
// If they dragged to an empty cell, we have to back out.
if(currToken2.canBeSwapped() == false){
currToken2 = null;
return;
}
if(boardModel.isCloseEnoughForSwap(currToken1, currToken2) == false){
currToken2 = null;
}
else{
animateSwapTokens(currToken1, currToken2);
}
}
}
}
public int getScore(){
return score;
}
public int getLevelTimeLeft(){
return (int)levelCountDownTimer.getTotalTime();
}
public int getLevel(){
return currLevel;
}
/*
As soon as a token is removed, we add to the score.
*/
public void addToScore(int offset){
score += offset;
notifyObservers();
}
/*
*
*/
void animateSwapTokens(Token t1, Token t2){
int t1Row = t1.getRow();
int t1Col = t1.getColumn();
int t2Row = t2.getRow();
int t2Col = t2.getColumn();
swapToken1 = t1;
swapToken2 = t2;
swapToken1.swapTo(t2Row, t2Col);
swapToken2.swapTo(t1Row, t1Col);
deselectCurrentTokens();
}
/*
*/
public void deselectCurrentTokens(){
if(currToken1 != null) currToken1.setSelect(false);
if(currToken2 != null) currToken2.setSelect(false);
currToken1 = null;
currToken2 = null;
}
/*
*/
void keyPressed(){
Keyboard.setKeyDown(keyCode, true);
// P key is locked
isPaused = Keyboard.isKeyDown(KEY_P);
if(isPaused){
timer.pause();
levelCountDownTimer.pause();
}
if(Keyboard.isKeyDown(KEY_L)){
//gemsWonByPlayer = 50;
soundManager.playMatchSound();
//goToNextLevel();
}
if(Keyboard.isKeyDown(KEY_M)){
soundManager.setMute(!soundManager.isMuted());
}
if(Keyboard.isKeyDown(KEY_S)){
//if(currToken1 != null){
// currToken1.setHasGem(true);
//setType(6);
//}
soundManager.playFailSwapSound();
}
}
/**
*/
void keyReleased(){
Keyboard.setKeyDown(keyCode, false);
isPaused = Keyboard.isKeyDown(KEY_P);
if(isPaused == false){
timer.resume();
levelCountDownTimer.resume();
}
}
public int getNumGems(){
return gemsWonByPlayer;
}
/**
*/
public boolean Paused(){
return isPaused;
}
/**
*/
public int getNumGemsForNextLevel(){
return gemsRequiredForLevel;
}
/*
The user can only go to the next level if they have
destroyed the number stored in gemsRequiredForLevel.
When the player goes to the next level, the user is given:
- A bit more time than the previous level
- Greater number of gems on board at a given time
- Sometimes the number of gem types increase
*/
void goToNextLevel(){
screenStory.nextLevel();
screens.transitionTo("story");
}
/*
Need to clear off all the crap that was happeneing in the last level.
*/
public void OnTransitionTo(){
currLevel++;
tokensDestroyed = 0;
//dyingTokens.clear();
// Should the score be reset?
// score = 0;
gemsWonByPlayer = 0;
gemsRequiredForLevel = gemsRequired[currLevel-1];
// Still playing around with this to make later levels challenging.
levelCountDownTimer = new Ticker();
levelCountDownTimer.setTime(timePermitted[currLevel-1]);
levelCountDownTimer.setDirection(-1);
timer = new Ticker();
if(currLevel == 4){
numTokenTypesOnBoard++;
}
// Easy way to clear the dying tokens which we don't want animating in the next level.
boardModel = new BoardModel();
boardModel.setNumGemsAllowedAtOnce(2);
boardModel.generateNewBoardWithDyingAnimation(false);
}
}