-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShape.pde
339 lines (318 loc) · 8.5 KB
/
Shape.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
class Shape {
int x;
int y;
Block[] blocks;
float timer;
int left;
int right;
int top;
int bottom;
boolean toBoard;
Shape(int x, int y, Block[] blocks) {
this.x = x;
this.y = y;
this.blocks = blocks;
timer = 0;
left = -1;
right = -1;
top = -1;
bottom = -1;
toBoard = false;
}
public void updateBounds() {
left = -1;
right = -1;
top = -1;
bottom = -1;
for (Block b : blocks) {
if (b.x>right || right==-1) {
right = b.x;
}
if (b.x<left || left==-1) {
left = b.x;
}
if (b.y<top || top==-1) {
top = b.y;
}
if (b.y>bottom || bottom==-1) {
bottom = b.y;
}
}
right += x;
left += x;
top += y;
bottom += y;
}
public int getWidth() {
return right - left;
}
public PVector getCenter() {
return new PVector(
(right - left + 1) / 2.0f,
(bottom - top + 1) / 2.0f
);
}
public void toBoard() {
while (bottom<config.getInt("yTiles")-1 && !colliding(x, y+1)) {
this.y++;
updateBounds();
}
for (Block b : blocks) {
if (b.y+y>=0) {
board.tiles[b.x+x][b.y+y] = makeBlock(b.x+x, b.y+y, b.type);
} else {
gameover = true;
}
}
blocks = new Block[]{};
}
public boolean colliding(int x1, int y1) {
for (Block b : blocks) {
if (blockExists(b.x+x1, b.y+y1) && !"blank".equals(board.tiles[b.x+x1][b.y+y1].getType().name)) {
return true;
}
}
return false;
}
public void checkCollisions() {
updateBounds();
if (left<0) {
x = 0;
}
if (right>config.getInt("xTiles")-1) {
x -= right-(config.getInt("xTiles")-1);
}
updateBounds();
}
public void move(int x1, int y1) {
if (!colliding(x+x1, y+y1)) {
this.x += x1;
this.y += y1;
}
}
public void update(float delta) {
updateBounds();
timer+=delta;
if (timer>speed) {
// Move the shape down if it's not hitting the bottom
if (bottom<config.getInt("yTiles")-1 && !colliding(x, y+1)) {
this.y++;
}
// If it hits the bottom add this to the board
else {
toBoard = true;
}
timer = 0;
}
updateBounds();
// Game over || gameover
if (colliding(x, y) && top<=0) {
gameover = true;
}
if (toBoard) {
toBoard();
}
}
public void display(boolean myLoc) {
pushMatrix();
if (myLoc) {
PVector loc = toWorldCoords(new PVector(x*TILESIZE, y*TILESIZE));
translate(loc.x, loc.y);
}
for (Block b : blocks) {
// Don't render the block if it's off the screen
if (b.y+y>=0 || !myLoc) {
b.display();
}
}
popMatrix();
}
// Rotate the blocks positions
public void shift () {
int firstX = blocks[0].x;
int firstY = blocks[0].y;
for (int i=0; i<blocks.length; i++) {
if (i+1<blocks.length) {
blocks[i].x = blocks[i+1].x;
blocks[i].y = blocks[i+1].y;
} else {
blocks[i].x = firstX;
blocks[i].y = firstY;
}
blocks[i].updateLoc();
}
}
}
class ShapeType {
PVector[][] blocks;
boolean canBeSpecial;
ShapeType(PVector[][] blocks, boolean canBeSpecial) {
this.blocks = blocks;
this.canBeSpecial = canBeSpecial;
}
ShapeType(PVector[][] blocks) {
this(blocks, false);
}
}
ArrayList<ShapeType> shapeTypes = new ArrayList<ShapeType>();
ArrayList<ShapeType> possibleShapeTypes = new ArrayList<ShapeType>();
public void addShapeType(ShapeType b, float chance) {
possibleShapeTypes.add(b);
for (int i=0; i<chance*100; i++) {
shapeTypes.add(b);
}
}
public PVector getShapeSize(PVector[] blocks) {
float left = -1;
float right = -1;
float top = -1;
float bottom = -1;
for (PVector v : blocks) {
if (v.x>right || right==-1) {
right = v.x;
}
if (v.x<left || left==-1) {
left = v.x;
}
if (v.y<top || top==-1) {
top = v.y;
}
if (v.y>bottom || bottom==-1) {
bottom = v.y;
}
}
return new PVector((abs(right-left)+1), (abs(bottom-top)+1));
}
public PVector getMaximumShapeSize() {
PVector maxSize = new PVector();
for (ShapeType shapeType : possibleShapeTypes) {
for (int rotation=0;rotation<shapeType.blocks.length;rotation++) {
PVector size = getShapeSize(shapeType.blocks[rotation]);
if (size.x > maxSize.x) {
maxSize.x = size.x;
}
if (size.y > maxSize.y) {
maxSize.y = size.y;
}
}
}
return maxSize;
}
ArrayList<ArrayList<String>> colorTypes = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> specialColorTypes = new ArrayList<ArrayList<String>>();
public void initColorTypes() {
for (int i=0;i<numLevels;i++) {
colorTypes.add(i,new ArrayList<String>());
specialColorTypes.add(i,new ArrayList<String>());
}
}
public void addColorType(String c, float chance, int lvl, boolean special) {
for (int i=0; i<chance*100; i++) {
for (int j=lvl;j<numLevels;j++) {
if (!special) {
colorTypes.get(j).add(c);
}
specialColorTypes.get(j).add(c);
}
}
}
public void addColorType(String c, float chance, int lvl) {
addColorType(c, chance, lvl, false);
}
public String randomType(boolean special) {
// Select a random type based on the current level
if (special) {
return specialColorTypes.get(level).get(int(random(specialColorTypes.get(level).size())));
} else {
return colorTypes.get(level).get(int(random(colorTypes.get(level).size())));
}
}
public String randomType() {
return randomType(false);
}
public void initRandomShapeGen() {
initColorTypes();
addColorType("blue", 0.5, 0);
addColorType("yellow", 0.5, 0);
addColorType("red", 0.5, 0);
addColorType("green", 0.5, 0);
addColorType("purple", 0.5, 1);
addColorType("stone", 0.2, 3);
// 0.15 for specials
addColorType("n", 0.3, 2, true);
addColorType("lightning", 0.3, 2, true);
addColorType("star", 0.3, 2, true);
// 1
addShapeType(new ShapeType(new PVector[][]{
// O
new PVector[]{new PVector(0, 0)},
}, true // Can be a speical block
), 0.1); // Probability
// 2
addShapeType(new ShapeType(new PVector[][]{
// O O
new PVector[]{new PVector(0, 0), new PVector(1, 0)},
// O
// O
new PVector[]{new PVector(0, 0), new PVector(0, 1)},
}), 0.2);
// 3
addShapeType(new ShapeType(new PVector[][]{
// O O
// O
new PVector[]{new PVector(0, 0), new PVector(1, 0), new PVector(1, 1)},
// O
// O O
new PVector[]{new PVector(0, 0), new PVector(1, 1), new PVector(0, 1)},
// O O
// O
new PVector[]{new PVector(0, 0), new PVector(1, 0), new PVector(0, 1)},
// O
// O O
new PVector[]{new PVector(1, 0), new PVector(1, 1), new PVector(0, 1)},
}), 0.4);
// 3.5
addShapeType(new ShapeType(new PVector[][]{
// O O O
new PVector[]{new PVector(0, 0), new PVector(1, 0), new PVector(2, 0)},
// O
// O
// O
new PVector[]{new PVector(0, 0), new PVector(0, 1), new PVector(0, 2)},
}), 0.3);
}
public Shape randomShape() {
int x = 2;
int y = 0;
// Choose a random shape type
int rand = int(random(shapeTypes.size()));
ShapeType st = shapeTypes.get(rand);
// Choose a random orientation
int rotation = int(random(st.blocks.length));
Block[] blocks = new Block[st.blocks[rotation].length];
// For each position vector generate a new random block
for (int i=0; i<st.blocks[rotation].length; i++) {
PVector v = st.blocks[rotation][i];
String type = randomType();
if (st.canBeSpecial) {
type = randomType(true);
}
blocks[i] = makeBlock(int(v.x), int(v.y), type);
}
// Make the new shape
Shape rShape = new Shape(x, y, blocks);
// Ensure it's on not off the screen on the x-axis
rShape.updateBounds();
rShape.y -= rShape.bottom;
rShape.checkCollisions();
return rShape;
}
public void nextPlayer() {
board.setPlayer(board.nextPlayer);
board.player.x = int(config.getInt("xTiles")/2.0 - board.player.getWidth()/2.0);
board.setNextPlayer(randomShape());
if (speed>fastestSpeed) {
speed-=((initspeed-fastestSpeed)/config.getInt("numToFastest"));
level = (initspeed-speed) / ((initspeed-fastestSpeed)/numLevels);
}
}