-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSandworm.pde
305 lines (261 loc) · 9.21 KB
/
Sandworm.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
class Sandworm {
ArrayList<Body> body;
float speed;
int dir = 1; // represents the most recent dir change (0 up, 1 right, 2 down, 3 left)
int add = 0; // "Queue" of body objects to be added
ArrayList<DirChange> dirQueue; // Queue for direction changes
boolean isWurm2 = false;
// Initializes relevant arrays and adds a head and tail to the wurm (changes position and direction based on if it the 1P/2P wurm)
Sandworm(boolean isWurm2) {
body = new ArrayList<Body>();
dirQueue = new ArrayList<DirChange>();
// Set the speed so that the snake travels one box every frameRate/3 frames
speed = board.getBoxSize() / (60/gc.gameDifficulty);
this.isWurm2 = isWurm2;
float bodyPosX1, bodyPosX2, bodyPosY, dx, dy;
if (isWurm2) {
bodyPosX1 = board.numOfBoxes - 3;
bodyPosX2 = board.numOfBoxes - 2;
bodyPosY = 3;
bodyPosY = 5;
dx = -speed;
dy = 0;
} else {
bodyPosX1 = 2;
bodyPosX2 = 1;
bodyPosY = 8;
dx = speed;
dy = 0;
}
body.add(
new Body(
board.getXPos() - (board.getBoxSize()/2) + board.getBoxSize() * bodyPosX1,
board.getYPos() - (board.getBoxSize()/2) + board.getBoxSize() * bodyPosY,
dx,
dy
)
);
body.add(
new Body(
board.getXPos() - (board.getBoxSize()/2) + board.getBoxSize() * bodyPosX2,
board.getYPos() - (board.getBoxSize()/2) + board.getBoxSize() * bodyPosY,
dx,
dy
)
);
}
// Draws the various pieces of the wurm's body
void display() {
// Stroke settings
strokeWeight(1);
stroke(0);
imageMode(CENTER);
int imageSize = board.getBoxSize();
// Draw head
fill(200, 0, 0);
image(getWormImage("head", body.get(0).dx, body.get(0).dy), body.get(0).x, body.get(0).y, imageSize, imageSize);
resetMatrix();
// Draw body
for (int i = 1; i < body.size()-1; i++) {
fill(255);
image(getWormImage("body", body.get(i).dx, body.get(i).dy), body.get(i).x, body.get(i).y, imageSize, imageSize);
}
// Draw tail
fill(0, 200, 0);
image(getWormImage("tail", body.get(body.size()-1).dx, body.get(body.size()-1).dy), body.get(body.size()-1).x, body.get(body.size()-1).y, imageSize, imageSize);
}
// Moves the position of each piece of the wurm's body based on the body piece's current dx, dy
// If a certain amount of frames have based since the game's start, also checks the dirQueue
// and performs a direction change where appropriate
// Lastly, adds a piece to the body if an add has been queued
void update() {
// Increment the position of all body parts
for (int i = 0; i < body.size(); i++) {
Body curr = body.get(i);
curr.x += curr.dx;
curr.y += curr.dy;
}
// Check if the box square size divided by speed divides into the frameCount
// If so, the snake is aligned with the center of a box
if ((frameCount - gc.startFrame) % int(board.getBoxSize()/speed) == 0) {
int recentIndex = -1;
for (int i = 0; i < dirQueue.size(); i++) {
DirChange currDir = dirQueue.get(i);
if (currDir.index != recentIndex) {
Body currBody = body.get(currDir.index);
currBody.dx = currDir.dx;
currBody.dy = currDir.dy;
recentIndex = currDir.index;
currDir.index += 1;
}
}
for (int i = 0; i < dirQueue.size(); i++) {
if (dirQueue.get(i).index == body.size()) {
dirQueue.remove(i);
i--;
}
}
// Add a body piece if necessary
if (add > 0) {
this.add();
add -= 1;
}
}
}
// Adds one body piece to the queue
void queueAdd() {
add += 1;
}
// Appends a new Body object to the list of body pieces
// Also checks if enough body pieces have been added to meet the game's win condition
void add() {
Body last = body.get(body.size()-1);
Body newBody;
newBody = new Body(
last.x + ((board.getBoxSize()/speed) * last.dx * -1),
last.y + ((board.getBoxSize()/speed) * last.dy * -1),
last.dx,
last.dy
);
body.add(newBody);
if (body.size() >= ((board.numOfBoxes*board.numOfBoxes)-2)) {
gc.endGame(GameState.WIN);
}
}
// Queues a direction change to be applied to wurm based on the newDir input
void queueDir(int newDir) {
boolean invalidDirChange = (newDir == 0 && (dir == 0 || dir == 2));
invalidDirChange = invalidDirChange || (newDir == 2 && (dir == 0 || dir == 2));
invalidDirChange = invalidDirChange || (newDir == 1 && (dir == 1 || dir == 3));
invalidDirChange = invalidDirChange || (newDir == 3 && (dir == 1 || dir == 3));
if (invalidDirChange) return;
dir = newDir;
float newDx, newDy;
if (dir == 0) { // Up
newDx = 0;
newDy = -speed;
} else if (dir == 1) { // Left
newDx = speed;
newDy = 0;
} else if (dir == 2) { // Down
newDx = 0;
newDy = speed;
} else { // Right
newDx = -speed;
newDy = 0;
}
dirQueue.add(new DirChange(0, newDx, newDy));
}
// Checks for the wurm's head's collision with the bounds of the board, its own body,
// a harvester, and if applicable, the other wurm
void checkCollision() {
Body head = body.get(0);
//check edges
if (head.getLeftBound() < board.getLeftBound() ||
head.getRightBound() > board.getRightBound() ||
head.getTopBound() < board.getTopBound() ||
head.getBottomBound() > board.getBottomBound()) {
if (gc.is2P) {
if (isWurm2) gc.endGame(GameState.P1_WIN);
else gc.endGame(GameState.P2_WIN);
} else {
gc.endGame(GameState.LOSS);
}
}
//check body
for (int i = 1; i < body.size(); i++) {
if (head.x < body.get(i).getRightBound() &&
head.x > body.get(i).getLeftBound() &&
head.y < body.get(i).getBottomBound() &&
head.y > body.get(i).getTopBound()) {
gc.endGame(GameState.LOSS);
}
}
if (gc.is2P) {
Sandworm wormToCheck = isWurm2 ? wurm : wurm2;
// Check for head to head contact first
Body otherHead = wormToCheck.body.get(0);
if (head.x < otherHead.getRightBound() &&
head.x > otherHead.getLeftBound() &&
head.y < otherHead.getBottomBound() &&
head.y > otherHead.getTopBound()) {
if ((head.dx < 0 && otherHead.dx > 0) ||
(head.dx > 0 && otherHead.dx < 0) ||
(head.dy > 0 && otherHead.dy < 0) ||
(head.dy < 0 && otherHead.dy > 0)) {
gc.endGame(GameState.DRAW);
return;
}
}
for (int i = 0; i < wormToCheck.body.size(); i++) {;
if (head.x < wormToCheck.body.get(i).getRightBound() &&
head.x > wormToCheck.body.get(i).getLeftBound() &&
head.y < wormToCheck.body.get(i).getBottomBound() &&
head.y > wormToCheck.body.get(i).getTopBound()) {
gc.endGame(isWurm2 ? GameState.P1_WIN : GameState.P2_WIN);
}
}
}
//check "food"
if (head.x > harvester.getBound("left") && head.x < harvester.getBound("right") && head.y < harvester.getBound("bottom") && head.y > harvester.getBound("top")) {
this.add();
if (this.isWurm2) score.increaseScore2();
else score.increase();
wormEat.play();
harvester.generate(this);
}
}
// Returns the appropriate image for the worm based on the desired body part (head, tail, body) and direction
PImage getWormImage(String bodyPart, float dx, float dy) {
if (dx < 0) {
if (bodyPart == "head") return isWurm2 ? headLeft2 : headLeft;
else if (bodyPart == "body") return isWurm2 ? bodyLeft2 : bodyLeft;
else if (bodyPart == "tail") return isWurm2 ? tailLeft2 : tailLeft;
} else if (dx > 0) {
if (bodyPart == "head") return isWurm2 ? headRight2 : headRight;
else if (bodyPart == "body") return isWurm2 ? bodyRight2 : bodyRight;
else if (bodyPart == "tail") return isWurm2 ? tailRight2 : tailRight;
} else if (dy > 0) {
if (bodyPart == "head") return isWurm2 ? headDown2 : headDown;
else if (bodyPart == "body") return isWurm2 ? bodyDown2 : bodyDown;
else if (bodyPart == "tail") return isWurm2 ? tailDown2 : tailDown;
} else {
if (bodyPart == "head") return isWurm2 ? headUp2 : headUp;
else if (bodyPart == "body") return isWurm2 ? bodyUp2 : bodyUp;
else if (bodyPart == "tail") return isWurm2 ? tailUp2 : tailUp;
}
return null;
}
}
// Class to represent direction changes that will be applied to each body piece of the wurm
class DirChange {
int index;
float dx, dy;
DirChange(int index, float dx, float dy) {
this.index = index;
this.dx = dx;
this.dy = dy;
}
}
// Class to represent pieces of the wurm's body (includes head and tail)
class Body {
float x, y, dx, dy;
Body(float x, float y, float dx, float dy) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
}
float getTopBound() {
return y - (board.getBoxSize()/2 - 5);
}
float getBottomBound() {
return y + (board.getBoxSize()/2 - 5);
}
float getLeftBound() {
return x - (board.getBoxSize()/2 - 5);
}
float getRightBound() {
return x + (board.getBoxSize()/2 - 5);
}
}