-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameBoard.pde
422 lines (403 loc) · 14.1 KB
/
GameBoard.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
class GameBoard {
PVector offset;
int xOffset;
int yOffset;
Block[][] tiles;
Shape player;
Shape nextPlayer;
GraphicRegion nextPlayerRegion;
PImage img;
PVector imgSize;
float timer;
float timer2;
int fillProgress;
float fillTimer;
private PImage borderImage;
GameBoard(int xOffset, int yOffset) {
// The offset to draw the game board
this.xOffset = xOffset;
this.yOffset = yOffset;
offset = toWorldCoords(new PVector(xOffset, yOffset));
// The array which stores the tiles
tiles = new Block[config.getInt("xTiles")][config.getInt("yTiles")];
// Start of with a balnk board
for (int x=0; x<config.getInt("xTiles"); x++) {
for (int y=0; y<config.getInt("yTiles"); y++) {
setBlock(x, y, "blank");
}
}
borderImage = getImage("bg_border_tile");
int w = int(getMaximumShapeSize().x * TILESIZE);
int h = int(getMaximumShapeSize().y * TILESIZE);
nextPlayerRegion = new GraphicRegion(this.getVisibleWidth(), borderImage.height, w, h, 2);
generateBackground();
// Timer to keep track of when to next update
timer = 0;
timer2 = 0;
fillProgress = 0;
fillTimer = 0;
}
public void drawBorder(PGraphics g, int offsetX, int offsetY, int width, int height) {
// Width and height border inclusive
int w = width + (borderImage.width * 2);
int h = height + (borderImage.height * 2);
// Create new graphics object to crop output
PGraphics cropped = createGraphics(w,h);
cropped.beginDraw();
cropped.pushMatrix();
// Draw the border tiles at the top and bottom of the screen
for (int x=0;x<w/(borderImage.width-1);x++) {
for (int y=0;y<2;y++) {
cropped.image(borderImage,x*(borderImage.width-1),y*(height+borderImage.height));
}
}
// Draw the border tiles on the left and right of the screen
for (int y=0;y<h/(borderImage.height-1);y++) {
for (int x=0;x<2;x++) {
cropped.image(borderImage,x*(width+borderImage.width),y*(borderImage.height-1));
}
}
cropped.popMatrix();
cropped.endDraw();
g.pushMatrix();
g.translate(offsetX, offsetY);
g.translate(-borderImage.width, -borderImage.height);
g.image(cropped,0,0);
g.popMatrix();
}
public void drawNextPlayerBackground(PGraphics g) {
g.noStroke();
g.fill(#ffffab);
g.rect(
nextPlayerRegion.x,
nextPlayerRegion.y,
nextPlayerRegion.width,
nextPlayerRegion.height);
g.fill(255,255,171);
drawBorder(g,
nextPlayerRegion.x,
nextPlayerRegion.y,
nextPlayerRegion.width,
nextPlayerRegion.height);
}
public void generateBackground() {
int h = getVisibleHeight();
int w = getVisibleWidth()+nextPlayerRegion.width+borderImage.width;
if (h < nextPlayerRegion.height) {
h = nextPlayerRegion.height;
}
PGraphics g = createGraphics(w,h);
g.beginDraw();
drawBorder(g, borderImage.width, borderImage.height, getWidth(), getHeight());
drawNextPlayerBackground(g);
// Draw the actual game background tiles
g.translate(borderImage.width, borderImage.height);
PImage i = getImage("bg_tile");
for (int x=0; x<config.getInt("xTiles"); x++) {
for (int y=0; y<config.getInt("yTiles"); y++) {
g.image(i, x*TILESIZE, y*TILESIZE);
}
}
g.endDraw();
img = g;
imgSize = imgToWorldCoords(img);
}
public int getWidth() {
return config.getInt("xTiles") * TILESIZE;
}
public int getHeight() {
return config.getInt("yTiles") * TILESIZE;
}
public int getVisibleWidth() {
return getWidth() + borderImage.width*2;
}
public int getVisibleHeight() {
return getHeight() + borderImage.height*2;
}
public int getLeftEdge() {
return this.xOffset;
}
public int getRightEdge() {
return this.getLeftEdge()+this.getVisibleWidth();
}
public int getTopEdge() {
return this.yOffset;
}
public int getBottomEdge() {
return this.getTopEdge()+this.getVisibleHeight();
}
public PVector getVisibleCenter() {
return new PVector(board.getLeftEdge()+board.getVisibleWidth()/2f,
board.getTopEdge()+board.getVisibleHeight()/2f);
}
// Reset the board
// called when game is over
public void reset() {
for (int x=0; x<config.getInt("xTiles"); x++) {
for (int y=0; y<config.getInt("yTiles"); y++) {
setBlock(x, y, "blank");
}
}
}
public void setBlock(int x, int y, String name) {
tiles[x][y] = new Block(x, y, name);
}
public void setPlayer(Shape s) {
player = s;
}
public void setNextPlayer(Shape s) {
nextPlayer = s;
}
public void drawNextPlayer() {
if (nextPlayer!=null) {
pushMatrix();
PVector nextPlayerCenter = nextPlayer.getCenter().mult(TILESIZE);
float x1 = nextPlayerRegion.x + (nextPlayerRegion.width / 2) - nextPlayerCenter.x;
float y1 = nextPlayerRegion.y + (nextPlayerRegion.height / 2) - nextPlayerCenter.y;
translate(toWorldX(int(x1)), toWorldY(int(y1)));
nextPlayer.display(false);
popMatrix();
}
}
public void display() {
pushMatrix();
translate(offset.x, offset.y);
// The offset of the image
image(img, 0, 0, imgSize.x, imgSize.y);
drawNextPlayer();
g.translate(toWorldX(borderImage.width), toWorldY(borderImage.height));
// Draw all the tiles on the board
for (int x=0; x<config.getInt("xTiles"); x++) {
for (int y=0; y<config.getInt("yTiles"); y++) {
tiles[x][y].display();
}
}
// Draw the player shape
if (player!=null) {
player.display(true);
}
popMatrix();
}
public void fillUp(float delta) {
String[] rainbow = new String[]{"red","yellow","green","blue","purple"};
fillTimer += delta;
if (fillTimer>100) {
fillProgress++;
fillTimer = 0;
boolean filled = false;
outerloop:
for (int y=config.getInt("yTiles")-1; y>=0; y--) {
for (int x=0; x<config.getInt("xTiles"); x++) {
if (!filled) {
if ("blank".equals(tiles[x][y].getType().name)) {
setBlock(x,y,rainbow[y%5]);
filled = true;
}
}
else {
break outerloop;
}
}
}
if (!filled) {
gameOver();
}
}
}
public void update(float delta) {
if (!gameover && !newGame) {
boolean stable = true;
boolean appliedGravity = true;
while (appliedGravity) {
appliedGravity = false;
for (int x=0; x<config.getInt("xTiles"); x++) {
for (int y=config.getInt("yTiles")-1; y>=0; y--) {
// Apply gravity if there is no block underneath
if (blockExists(x, y+1, "blank") && !"blank".equals(tiles[x][y].type)) {
tiles[x][y+1] = makeBlock(x, y+1, tiles[x][y].type);
tiles[x][y] = makeBlock(x, y, "blank");
appliedGravity = true;
}
}
}
}
// Add delay between each explosion
timer += delta;
if (timer>speed/2) {
int avgX = 0;
int avgY = 0;
int total = 0;
for (int x=0; x<config.getInt("xTiles"); x++) {
for (int y=config.getInt("yTiles")-1; y>=0; y--) {
String type = tiles[x][y].getType().name;
if (!"blank".equals(type)) {
if (!"lightning".equals(type) && !"star".equals(type) && !"n".equals(type) && !"stone".equals(type)) {
if ( (blockExists(x+1, y, type) && blockExists(x-1, y, type) && !tiles[x+1][y].explode && !tiles[x-1][y].explode) ||
(blockExists(x, y+1, type) && blockExists(x, y-1, type) && !tiles[x][y+1].explode && !tiles[x][y+1].explode) ||
(blockExists(x+1, y+1, type) && blockExists(x-1, y-1, type) && !tiles[x+1][y+1].explode && !tiles[x-1][y-1].explode) ||
(blockExists(x-1, y+1, type) && blockExists(x+1, y-1, type) && !tiles[x-1][y+1].explode && !tiles[x+1][y-1].explode)) {
explode(x, y);
avgX+=x;
avgY+=y;
total+=1;
//stable = false;
ArrayList<PVector> explodePositions = new ArrayList<PVector>();
// Horizontal
if (blockExists(x+1, y, type) && blockExists(x-1, y, type)) {
explodePositions.add(new PVector(x+1, y));
explodePositions.add(new PVector(x-1, y));
explodePositions.add(new PVector(x+2, y));
explodePositions.add(new PVector(x-2, y));
}
// Vertical
if (blockExists(x, y+1, type) && blockExists(x, y-1, type)) {
explodePositions.add(new PVector(x, y+1));
explodePositions.add(new PVector(x, y-1));
explodePositions.add(new PVector(x, y+2));
explodePositions.add(new PVector(x, y-2));
}
// Diagonal 1
if (blockExists(x+1, y+1, type) && blockExists(x-1, y-1, type)) {
explodePositions.add(new PVector(x+1, y+1));
explodePositions.add(new PVector(x-1, y-1));
explodePositions.add(new PVector(x+2, y+2));
explodePositions.add(new PVector(x-2, y-2));
}
// Diagonal 2
if (blockExists(x-1, y+1, type) && blockExists(x+1, y-1, type)) {
explodePositions.add(new PVector(x-1, y+1));
explodePositions.add(new PVector(x+1, y-1));
explodePositions.add(new PVector(x-2, y+2));
explodePositions.add(new PVector(x+2, y-2));
}
for (PVector p : explodePositions) {
int px = int(p.x);
int py = int(p.y);
if (blockExists(px, py, type)) {
explode(px, py);
avgX+=px;
avgY+=py;
total+=1;
}
}
}
} else if ("n".equals(type)) {
explode(x, y);
avgX+=x;
avgY+=y;
total+=1;
//stable = false;
explode(x+1, y);
explode(x-1, y);
explode(x, y+1);
explode(x, y-1);
explode(x+1, y-1);
explode(x+1, y+1);
explode(x-1, y-1);
explode(x-1, y+1);
} else if ("lightning".equals(type)) {
avgX+=x;
avgY+=y;
total+=1;
//stable = false;
// Explode all blocks below
for (int j=y; j<config.getInt("yTiles"); j++) {
explode(x, j);
}
}
// Explode all the same color
else if ("star".equals(type) && blockExists(x, y+1)) {
explode(x, y);
String typeCheck = tiles[x][y+1].getType().name;
for (int x2=0; x2<config.getInt("xTiles"); x2++) {
for (int y2=config.getInt("yTiles")-1; y2>=0; y2--) {
if (typeCheck != null && typeCheck.equals(tiles[x2][y2].getType().name)) {
explode(x2, y2);
}
}
}
}
}
}
}
// Broken -/-
if (isExploding()) {
combos++;
}
if (combos>1 && total>0) {
int xc = avgX/total;
int yc = avgY/total;
drawCombos(xc, yc);
sim.explode((xc*TILESIZE)+board.xOffset, (yc*TILESIZE)+board.yOffset);
}
timer = 0;
}
// If stable add new player
if (isStable() && player.blocks.length==0) {
timer2+=time;
// if (timer2>nextPlayerDelay) {
if (timer2>speed) {
combos = 1;
timer2 = 0;
nextPlayer();
}
}
player.update(delta);
}
for (int x=0; x<config.getInt("xTiles"); x++) {
for (int y=config.getInt("yTiles")-1; y>=0; y--) {
tiles[x][y].update(time);
}
}
}
public void explode(int x, int y) {
if (blockExists(x, y)) {
tiles[x][y].explode = true;
}
}
public boolean isStable() {
boolean stable = true;
loop:
for (int x=0; x<config.getInt("xTiles"); x++) {
for (int y=config.getInt("yTiles")-1; y>=0; y--) {
if (!"blank".equals(tiles[x][y].getType().name)) {
// If this will fall or disappear then it's unstable
if (blockExists(x, y+1, "blank") || tiles[x][y].explode || (blockExists(x, y+1) && tiles[x][y+1].explode)) {
stable = false;
break loop;
}
}
}
}
return stable;
}
public boolean isExploding() {
boolean exploding = false;
loop:
for (int x=0; x<config.getInt("xTiles"); x++) {
for (int y=config.getInt("yTiles")-1; y>=0; y--) {
if (!"blank".equals(tiles[x][y].getType().name)) {
if (tiles[x][y].exploding) {
exploding = true;
break loop;
}
}
}
}
return exploding;
}
}
public void drawCombos(int x, int y) {
comboFont.writeDelay(str(combos), 1000, (x*TILESIZE)+board.xOffset+TILESIZE, (y*TILESIZE)+board.yOffset);
//comboFont.writeDelay(str(combos), 1000, 34, 56);
}
public boolean blockExists(int x, int y, String name) {
if (x<config.getInt("xTiles") && x>=0 && y<config.getInt("yTiles") && y>=0 && ((board.tiles[x][y].getType().name.equals(name) || ("any".equals(name) && !"blank".equals(board.tiles[x][y].getType().name))) || name == null)) {
return true;
} else {
return false;
}
}
public boolean blockExists(int x, int y) {
return blockExists(x, y, null);
}