-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbomb.js
More file actions
81 lines (74 loc) · 1.81 KB
/
bomb.js
File metadata and controls
81 lines (74 loc) · 1.81 KB
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
import { H, random } from "./utils.js";
export class Bomb {
constructor({
width = 100,
height = 100,
x = 0,
y = 0,
angularSpeed = { x: 10, y: 10, z: 10 },
alpha = 0,
vx = 0,
vy = 0,
classes = "",
gravity = 0.2,
alphaSpeed = 0.01,
scale = 1,
timeToDie = 0.6
}) {
this.type = "bomb";
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.rotationZ = random(0, 360);
this.alpha = alpha;
this.vx = vx;
this.vy = vy;
this.angularSpeed = angularSpeed;
this.gravity = gravity;
this.alphaSpeed = alphaSpeed;
this.scale = scale;
this.timeToDie = timeToDie;
this.bounces = random(1, 4);
this.el = document.createElement("div");
this.el.classList.add("bomb");
this.el.textContent = "💣";
if (classes) {
this.el.classList.add(classes);
}
this.el.style.width = `${this.width}px`;
this.el.style.height = `${this.height}px`;
document.querySelector('[data-screen="menu"]').append(this.el);
}
destroy() {
this.el.remove();
}
hasHitEnd() {
return this.y + this.height === H;
}
update() {
this.x += this.vx;
this.y += this.vy;
this.vy += this.gravity;
if (this.y + this.height > H) {
this.bounces--;
if (this.bounces) {
this.y = H - this.height - 1;
this.rotationZ = this.rotationZ + random(-50, 50);
// this.gravity = 0;
this.vy = -this.vy * 0.5;
} else {
this.y = H - this.height;
this.gravity = 0;
this.vy = 0;
}
}
if (this.alpha < 1) {
this.alpha += this.alphaSpeed;
}
}
draw() {
this.el.style.transform = `translate(${this.x}px, ${this.y}px) rotateZ(${this.rotationZ}deg) scale(${this.scale})`;
this.el.style.opacity = this.alpha;
}
}