-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
188 lines (161 loc) · 4.18 KB
/
main.js
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
const FPS = 60;
const SPACE_KEYCODE = 32;
const BG_COLOR = 50;
let worm;
let attractor;
let now;
let obsticles = [];
let shieldBar;
let isGameStarted = false;
let tempo = 160;
let startScoreTimer;
let scoreTimer = 0;
let scoreP;
let bestP;
const LIVES_AMOUNT = 3;
let lives = LIVES_AMOUNT;
let SHIELD_ACTIVE_TIME = 5 * FPS;
let SHIELD_COOLDOWN = 20 * FPS;
let shieldActivator = false;
let startShieldFrameCount = -1;
let startShieldCoolDownFrameCount = -1;
let fcCrashDeactivateBypass = 0;
let fcShieldDeactivate = SHIELD_COOLDOWN;
let fcCanUseShield = 0;
/*
One time setup.
Init objects once and call reset to init all the
dynamic variables.
*/
function setup() {
createCanvas(innerWidth, innerHeight);
noCursor();
scoreP = createP();
bestP = createP();
scoreP.html("Score: 0");
bestP.html("Best: 0");
shieldBar = new ShieldBar();
attractor = createVector(width / 2, height / 2);
reset();
}
/*
Reset the game variables.
Every new game this function should be called.
*/
function reset() {
worm = new Worm(
random(width / 4, width * 0.75),
random(height / 4, height * 0.75)
);
obsticles = [];
fcCrashDeactivateBypass = 0;
fcShieldDeactivate = 0;
fcCanUseShield = 0;
startShieldFrameCount = -1;
startShieldCoolDownFrameCount = -1;
fcCrashDeactivateBypass = 0;
fcShieldDeactivate = SHIELD_COOLDOWN;
fcCanUseShield = 0;
shieldActivator = false;
lives = LIVES_AMOUNT;
scoreTimer = 0;
startScoreTimer = Math.round(new Date() / 10);
}
function draw() {
background(BG_COLOR);
DrawHearts();
// Score
now = Math.round(new Date() / 10);
scoreTimer = (now - startScoreTimer) / 100;
bestTime = Math.max(bestTime, scoreTimer);
setCookie("bestTime", bestTime, 365);
scoreP.html("<p class = score> Score: " + scoreTimer + "</p>");
bestP.html("<p class = best> Best: " + bestTime + "</p>");
if (shieldActivator) {
shieldBar.update(fcShieldDeactivate, startShieldFrameCount, frameCount);
} else {
shieldBar.update(startShieldCoolDownFrameCount, fcCanUseShield, frameCount);
}
shieldBar.show();
// Attractor
if (mouseIsPressed || isGameStarted == true) {
// The rate of the obsticles.
//tempo = floor(random(250,300));
if (frameCount % tempo == 0) {
obsticles.push(new Obsticle(floor(random(4)), 100));
}
attractor = createVector(mouseX, mouseY);
isGameStarted = true;
} else {
attractor = createVector(width / 2, height / 2);
}
// Obsticles
for (let i = obsticles.length - 1; i >= 0; i--) {
let obsticle = obsticles[i];
obsticle.update();
obsticle.show();
if (obsticle.isOutOfScreen()) {
obsticles.splice(i, 1);
}
}
renderMouseAttractor();
worm.attracted(attractor);
worm.update();
if (shieldActivator) {
worm.shield();
} else {
CheckCollisions();
}
if (frameCount >= fcShieldDeactivate && shieldActivator) {
startShieldCoolDownFrameCount = frameCount;
fcCanUseShield = frameCount + SHIELD_COOLDOWN;
shieldActivator = false;
}
worm.show();
}
function renderMouseAttractor() {
strokeWeight(10);
stroke(0, 25, 255);
point(attractor.x, attractor.y);
strokeWeight(6);
stroke(250);
point(attractor.x, attractor.y);
}
function DrawHearts() {
push();
translate(width - 90, height - 60);
for (let heartIndex = 0; heartIndex < LIVES_AMOUNT; heartIndex++) {
heart(lives, heartIndex);
translate(0, -60);
}
pop();
}
function CheckCollisions() {
if (frameCount < fcCrashDeactivateBypass) {
return;
}
for (let i = obsticles.length - 1; i >= 0; i--) {
if (worm.checkCrash(obsticles[i])) {
lives--;
if (lives > 0) {
fcCrashDeactivateBypass = frameCount + 10;
obsticles.splice(i, 1);
} else {
reset();
}
// There is no need to keep check for collisions
// since we have a crash safety timer. (After hit we have a shield for a second).
break;
}
}
}
function keyPressed() {
if (keyCode === SPACE_KEYCODE && !shieldActivator && frameCount >= fcCanUseShield) {
startShieldFrameCount = frameCount;
fcShieldDeactivate = frameCount + SHIELD_ACTIVE_TIME;
shieldActivator = true;
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}