-
Notifications
You must be signed in to change notification settings - Fork 0
/
pillows.js
61 lines (52 loc) · 1.5 KB
/
pillows.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
const pillowArray = [];
const pillow = new Image();
pillow.src = '/src/images/PILLOW.png';
class Pillow {
constructor() {
this.x = canvas.width;
this.y = Math.random() * canvas.height;
this.radius = 8;
this.speed = Math.random() * 5 + 1;
this.distance;
this.counted = false;
}
update() {
this.x -= this.speed;
const dx = this.x - tama.x;
const dy = this.y - tama.y;
this.distance = Math.sqrt(dx * dx + dy * dy)
}
draw() {
//hitbox
// ctx.fillStyle = 'white';
// ctx.beginPath();
// ctx.arc(this.x + 14, this.y + 8, this.radius, 0, Math.PI * 2)
// ctx.fill();
ctx.drawImage(pillow, this.x, this.y, canvas.width / 22, canvas.height / 25);
}
}
function handlePillows() {
//pillow creation
if (frame % 50 === 0) {
pillowArray.push(new Pillow());
}
//pillow draw
for (let i = 0; i < pillowArray.length; i++) {
pillowArray[i].update();
pillowArray[i].draw();
}
//to not have too many pillow for performance
if (pillowArray.length > 300) {
pillowArray.pop(pillowArray[0])
}
for (let i = 0; i < pillowArray.length; i++) {
//collision
if (pillowArray[i].distance < pillowArray[i].radius + tama.radius) {
if (!pillowArray[i].counted){
rest += 10;
pillowArray[i].counted = true;
pillowArray.splice(i, 1)
}
}
}
}