-
Notifications
You must be signed in to change notification settings - Fork 0
/
beverages.js
59 lines (49 loc) · 1.49 KB
/
beverages.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
const beverageArray = [];
const beverage = new Image();
beverage.src = '/src/images/BEVERAGE.png';
class Beverage {
constructor() {
this.x = canvas.width;
this.y = Math.random() * canvas.height;
this.radius = 7;
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() {
// ctx.fillStyle = 'white';
// ctx.beginPath();
// ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2)
// ctx.fill();
ctx.drawImage(beverage, this.x - 11, this.y - 10, canvas.width / 23, canvas.height / 25);
}
}
function handleBeverages() {
if (frame % 50 === 0) {
beverageArray.push(new Beverage());
}
for (let i = 0; i < beverageArray.length; i++) {
beverageArray[i].update();
beverageArray[i].draw();
}
//to not have too many beverages for performance
if (beverageArray.length > 300) {
beverageArray.pop(beverageArray[0])
}
for (let i = 0; i < beverageArray.length; i++) {
//collision
if (beverageArray[i].distance < beverageArray[i].radius + tama.radius) {
if (!beverageArray[i].counted) {
thirst += 10;
beverageArray[i].counted = true;
beverageArray.splice(i, 1)
}
}
}
}