-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.pde
253 lines (203 loc) · 4.88 KB
/
events.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
//color textC = #F6FF00; // yellow
color textC = #FF035F; // pink
int pauseTimer;
boolean dyingAnimation = false;
int dyingTimer;
float dyingState, dyingAngle;
boolean levelCompleteAnimation = false;
float levelCompleteAngle = 0;
boolean gameOver = false;
boolean deadGhostAnimation = false;
int deadGhostTimer, ghostScore;
PVector ghostScoreLoc;
Ghost deadGhost;
int readyTimer;
void addScore(int d) {
int lastScore = score;
score += d;
if ((score % 50000) < (lastScore % 50000)) {
pac.lives++;
}
}
void levelCompleteSequence() {
levelStarted = false;
fill(0);
rectMode(CENTER);
rect(width/2, height/2, 40, 30);
fill(textC); // yellow
textFont(font,20);
textAlign(CENTER);
text("Level Complete!", width/2, height/2);
setupLevel();
}
void startLevel() {
levelStarted = true;
timer = millis();
phaseTimer = timer;
pac.moving = true;
blinky.moving = true;
}
void reset() {
// when new level starts or pacman dies
pac.initialize();
blinky.initialize();
pinky.initialize();
inky.initialize();
clyde.initialize();
ghostMult = 1;
readyTimer = millis();
levelStarted = false;
}
void pacDie() {
if (!invincible) {
// deathSound.trigger();
pac.lives--;
dyingState = pac.mouthState;
dyingAngle = pac.mouthAngle;
dyingTimer = millis();
animation = true;
dyingAnimation = true;
}
}
int ghostMult = 1;
void killGhost(Ghost g) {
int bonus = ghostMult * 200;
ghostMult *= 2;
ghostScore = bonus;
addScore(bonus);
deadGhost = g;
PVector drawLoc = getLoc(pac.loc.y,pac.loc.x);
float ran = random(0,1);
int offSetx = round(ran);
if (offSetx == 0) { offSetx = -1; }
ran = random(0,1);
int offSety = round(ran);
if (offSety == 0) { offSety = -1; }
ghostScoreLoc = new PVector(drawLoc.x + 20*offSetx, drawLoc.y + 20*offSety);
deadGhostAnimation = true;
animation = true;
deadGhostTimer = millis();
}
//************//
// ANIMATIONS //
//************//
void runAnimation() {
if (!gameStarted) {
runTitleScreen();
} else if (levelCompleteAnimation) {
runLevelCompleteAnimation();
} else if (dyingAnimation) {
runDyingAnimation();
} else if (deadGhostAnimation) {
killGhostAnimation();
} else if (gameOver) {
runGameOver();
}
}
void killGhostAnimation() {
pac.drawPac();
if (deadGhost != blinky)
blinky.drawGhost();
if (deadGhost != pinky)
pinky.drawGhost();
if (deadGhost != inky)
inky.drawGhost();
if (deadGhost != clyde)
clyde.drawGhost();
fill(255);
stroke(0);
textFont(font, 16);
text(ghostScore, ghostScoreLoc.x, ghostScoreLoc.y);
if (millis() - deadGhostTimer > 1000) {
// end animation
frightenedTimer += 1000; // must add a second back to the timer
animation = false;
deadGhostAnimation = false;
deadGhost.dead = true;
deadGhost.frightened = false;
// reset multiplier if we've killed all possible ghosts
if (!blinky.frightened && !pinky.frightened && !inky.frightened && !clyde.frightened) {
ghostMult = 1;
}
}
}
void runTitleScreen() {
strokeWeight(1);
if (keyPressed && key==' ') {
animation = false;
gameStarted = true;
setupGame();
} else if (startGame == true) { // test: start game with proximity
animation = false;
gameStarted = true;
setupGame();
}
paused = 1;
}
void runLevelCompleteAnimation() {
pac.drawPac();
blinky.drawGhost();
pinky.drawGhost();
inky.drawGhost();
clyde.drawGhost();
levelCompleteAngle += PI/12;
pushMatrix();
float angle = levelCompleteAngle;
if (angle > 4*2*PI) {
angle = 4*2*PI;
}
int tSize = int(angle*50/(8*PI));
translate(width/2, height/2);
rotate(angle);
textFont(font2, tSize);
fill(#FC0505);
stroke(255);
text("Level Complete", 0, 0);
popMatrix();
if (levelCompleteAngle > 6*2*PI) {
animation = false;
levelCompleteAnimation = false;
setupLevel();
}
}
void runDyingAnimation() {
blinky.drawGhost();
pinky.drawGhost();
inky.drawGhost();
clyde.drawGhost();
// draw dying pacman
PVector drawLoc = getLoc(pac.loc.y, pac.loc.x);
ellipseMode(CENTER);
stroke(0);
fill(yellow);
arc(drawLoc.x, drawLoc.y, pacDiameter, pacDiameter, dyingAngle+PI*dyingState/4, dyingAngle+2*PI-PI*dyingState/4);
if (millis() - dyingTimer > 600)
dyingState += 0.04;
if (dyingState >= 4) {
// animation complete
dyingAnimation = false;
animation = false;
if (pac.lives >= 0) {
reset();
} else {
// GAME OVER :(
gameOver = true;
animation = true;
startGame = false;
}
}
}
void runGameOver() {
/*rectMode(CENTER);
fill(0,150);
stroke(255);
strokeWeight(4);
rect(width/2, height/2, 400, 60);
strokeWeight(1);
textFont(font2, 50);
fill(#FC0505);
text("GAME OVER", width/2, height/2+17);*/
runTitleScreen();
if (animation == false)
gameOver = false;
}