-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdust.html
353 lines (306 loc) · 14.4 KB
/
dust.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dust</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
<link rel="stylesheet" href="buttons.css">
<style>
body {
background-color: black;
cursor: url('sprites/room/cursor1.png'), auto;
}
#start:hover,
#returnButton:hover {
cursor: url('sprites/room/cursor2.png'), auto;
}
</style>
</head>
<body>
<button id="returnButton">Return to Room</button>
<button id="start">Continue</button>
<script>
var dusterCheck = localStorage.getItem('dusterCheck');
if (dusterCheck < '0') {
localStorage.setItem('dusterCheck', '0');
}
let canvasX;
let canvasY;
let shelf;
let dustBunnyImages = [];
let dustBunnies = [];
let lastMouseX = 0;
let lastMouseY = 0;
let lastTime = 0;
let speedThreshold = 10;
let tooFast = false;
let halt = 0;
let duster;
let dusterHuman;
let dusterSize;
let dusterDir = 1;
let dusterAngle = 0;
let dusterAngleDirection = 1;
let dusterMaxAngle = 0.3; // max angle for fanning motion
let dusterAngleSpeed = 0.03; // speed of angle change
const num = 40;
let noiseScale = 0.01;
let radius;
let bunnySize;
let scale;
let width;
let height;
// Preload all images
function preload() {
shelf = loadImage('sprites/miniGames/shelf.png');
for (let i = 0; i < num; i++) {
dustBunnyImages.push(loadImage("sprites/enemies/dustBunny.png"));
}
duster = loadImage("sprites/room/dusterIRL.png");
dusterHuman = loadImage('sprites/characters/duster.png');
}
// setup the vectors of each bunny in the window
// as well as adjusting the sizes of bunnies, roomba,
// and radius of suck according to the size of the window
function setup() {
frameRate(60);
scale = Math.min(windowWidth / shelf.width, windowHeight / shelf.height);
canvasX = (windowWidth / 2) - (shelf.width / 2) * scale;
canvasY = (windowHeight / 2) - (shelf.height / 2) * scale;
width = shelf.width * scale;
height = shelf.height * scale;
canvas = createCanvas(width, height).attribute('id', 'mainCanvas').attribute('willReadFrequently', true);
canvas.position(canvasX, canvasY);
for (let i = 0; i < num; i++) {
dustBunnies.push(createVector(random(width), random(height)));
}
stroke(0, 128, 128);
noiseSeed(1);
bunnySize = 150 * scale;
dusterSize = 200 * scale;
radius = 350 * scale;
noiseScale = 0.01 * scale;
mouseX = windowWidth / 2;
mouseY = windowHeight / 2;
positionButton();
lastMouseX = mouseX;
lastMouseY = mouseY;
lastTime = millis();
}
function windowResized() {
scale = Math.min(windowWidth / shelf.width, windowHeight / shelf.height);
canvasX = (windowWidth / 2) - (shelf.width / 2) * scale;
canvasY = (windowHeight / 2) - (shelf.height / 2) * scale;
width = shelf.width * scale;
height = shelf.height * scale;
resizeCanvas(width, height);
canvas.position(canvasX, canvasY);
bunnySize = 150 * scale;
dusterSize = 200 * scale;
radius = 350 * scale;
positionButton();
}
function draw() {
if (dusterCheck > '1') {
document.body.style.cursor = 'none';
}
background(shelf); // set the background every frame
// makes the screen look dirty until all dust bunnies are sucked up
fill(100, 50, 0, 150 * dustBunnies.length / num);
rect(0, 0, canvas.width, canvas.height);
if (dusterCheck <= '0') {
document.getElementById("start").style.display = "none";
for (let i = dustBunnies.length - 1; i >= 0; i--) {
let bunny = dustBunnies[i];
let bunnyImage = dustBunnyImages[i];
let n = noise(bunny.x * noiseScale, bunny.y * noiseScale);
let a = TAU * n;
let safe;
// bunny follows path
bunny.x += cos(a);
bunny.y += sin(a);
image(bunnyImage, bunny.x - bunnySize / 2, bunny.y - bunnySize / 2, bunnySize, bunnySize);
// If bunny goes off screen, respawn at a random spot
if (!onScreen(bunny)) {
bunny.x = random(width);
bunny.y = random(height);
}
}
styledText("Jeez this shelf is dusty", width / 2, height / 2.7, 80);
styledText("I'll need some help to dust this...", width / 2, height / 2.12, 80);
document.getElementById("returnButton").style.display = "block";
} else if (dusterCheck === '1') {
styledText("Dust off the bookshelf!", width / 2, height / 2.7, 80);
styledText("Press and hold your mouse to use duster", width / 2, height / 2.1, 80);
styledText("Don't shake the duster too fast!", width / 2, height / 1.71, 80);
document.getElementById("start").style.display = "block";
document.getElementById("returnButton").style.display = "none";
} else {
document.getElementById("start").style.display = "none";
// once every dust bunny has been sucked up, display text to say, "All clean!"
if (dustBunnies.length < 1) {
document.body.style.cursor = 'url("sprites/room/cursor1.png"), auto';
push();
rotate(45);
imageMode(CENTER);
image(duster, width / 2 + 100 * scale, height / 2 - 950 * scale, duster.width * scale, duster.height * scale);
pop();
styledText("All Clean!", width / 2 + 10 * scale, height / 2, 130);
localStorage.setItem('dusterCheck', '2');
document.getElementById("returnButton").style.display = "block";
} else if (mouseIsPressed && !tooFast) {
document.getElementById("returnButton").style.display = "none";
for (let i = dustBunnies.length - 1; i >= 0; i--) {
let bunny = dustBunnies[i];
let bunnyImage = dustBunnyImages[i];
let n = noise(bunny.x * noiseScale, bunny.y * noiseScale);
let a = TAU * n;
// bunny in range
if (dist(bunny.x, bunny.y, mouseX, mouseY - 300 * scale) <= radius) {
// make the bunny go away from the duster
let directionX = mouseX - bunny.x;
let directionY = mouseY - bunny.y;
bunny.x -= directionX / 70;
bunny.y -= directionY / 70;
// spin the bunnies
push();
translate(bunny.x, bunny.y);
rotate(frameCount * .15);
image(bunnyImage, -bunnySize / 2, -bunnySize / 2, bunnySize, bunnySize);
pop();
}
// bunny just outside of range
else if (dist(bunny.x, bunny.y, mouseX, mouseY - 300 * scale) < radius + 5 * scale) {
// spin the bunnies
push();
translate(bunny.x, bunny.y);
rotate(frameCount * .15);
image(bunnyImage, -bunnySize / 2, -bunnySize / 2, bunnySize, bunnySize);
pop();
}
// bunny out of range
else {
// Go towards center
let directionX = (width / 2 - bunny.x) / 300;
let directionY = (height / 2 - bunny.y) / 300;
bunny.x += directionX;
bunny.y += directionY;
image(bunnyImage, bunny.x - bunnySize / 2, bunny.y - bunnySize / 2, bunnySize, bunnySize);
}
// Check if the bunny has moved out of frame
if (!onScreen(bunny)) {
dustBunnies.splice(i, 1); // Remove the bunny if it's off the screen
}
}
// Duster dusts
dusterAngle += dusterAngleDirection * dusterAngleSpeed; // calculate angle to rotate
if (abs(dusterAngle) > dusterMaxAngle) { // if angle is too great, flip the direction
dusterAngleDirection *= -1;
}
push();
translate(mouseX, mouseY + dusterSize * 1.75 - 100 * scale); // Adjusting the pivot point to the bottom
rotate(dusterAngle);
image(duster, -dusterSize / 3.5 - 60 * scale, -dusterSize * 3.5 - 160 * scale, dusterSize * 1.7, dusterSize * 4.5); // Position image relative to the new pivot
pop();
} else {
document.getElementById("returnButton").style.display = "none";
// Mouse not being clicked/held
for (let i = dustBunnies.length - 1; i >= 0; i--) {
let bunny = dustBunnies[i];
let bunnyImage = dustBunnyImages[i];
let n = noise(bunny.x * noiseScale, bunny.y * noiseScale);
let a = TAU * n;
let safe;
// bunny follows path
bunny.x += cos(a);
bunny.y += sin(a);
image(bunnyImage, bunny.x - bunnySize / 2, bunny.y - bunnySize / 2, bunnySize, bunnySize);
if (!tooFast) {
image(duster, mouseX - dusterSize * 1.7 / 2, mouseY - dusterSize * 3.5 + 100 * scale, dusterSize * 1.7, dusterSize * 4.5);
}
// If bunny goes off screen, respawn at a random spot
if (!onScreen(bunny)) {
bunny.x = random(width);
bunny.y = random(height);
}
}
}
if (tooFast) {
document.body.style.cursor = 'url("sprites/room/cursor1.png"), auto';
image(dusterHuman, width - dusterHuman.width / 2 * scale, height - dusterHuman.height / 2.1 * scale, dusterHuman.width / 2 * scale, dusterHuman.height / 2 * scale);
styledText("I'm gonna hurl...", width / 1.7, height / 1.3, 40);
styledText("Slow down!", width / 1.7, height / 1.2, 40);
halt += 0.007;
if (halt >= 2) {
tooFast = false;
halt = 0;
}
}
else {
let currentTime = millis();
let elapsedTime = currentTime - lastTime;
if (elapsedTime > 0) {
let dusterDistance = dist(mouseX, mouseY, lastMouseX, lastMouseY);
let dusterSpeed = dusterDistance / elapsedTime;
if (dusterSpeed > speedThreshold) {
halt += .07;
if (halt >= 1) {
tooFast = true;
}
}
lastMouseX = mouseX;
lastMouseY = mouseY;
lastTime = currentTime;
}
}
}
}
function styledText(string, x, y, size) {
stroke(0, 0, 0);
strokeWeight(4);
sizeOfText = size * scale;
textSize(sizeOfText);
textStyle(BOLD);
textAlign(CENTER);
textFont('Papyrus');
fill(255);
text(string, x, y);
}
// Check if bunny is still on screen
function onScreen(bunny) {
if (bunny.x >= -bunnySize && bunny.x <= width + bunnySize && bunny.y >= 0 && bunny.y <= height) {
return true;
} else {
return false;
}
}
function positionButton() {
const canvas = document.getElementById('mainCanvas');
const returnButton = document.getElementById('returnButton');
const canvasRect = canvas.getBoundingClientRect();
returnButton.style.top = `${canvasY + height / 1.2}px`;
returnButton.style.left = `${canvasX + width / 2}px`;
const startButton = document.getElementById('start');
startButton.style.top = `${canvasY + height / 1.2}px`;
startButton.style.left = `${canvasX + width / 2}px`;
if (dusterCheck === '1') {
document.getElementById('start').style.display = 'none';
}
};
document.addEventListener("DOMContentLoaded", function () {
var returnButton = document.getElementById('returnButton');
returnButton.addEventListener('click', function () {
window.location.href = 'room.html';
});
});
document.addEventListener("DOMContentLoaded", function () {
var startButton = document.getElementById('start');
startButton.addEventListener('click', function () {
localStorage.setItem('dusterCheck', '2');
window.location.href = 'dust.html';
});
});
</script>
</body>
</html>