-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpower.js
More file actions
64 lines (61 loc) · 1.96 KB
/
power.js
File metadata and controls
64 lines (61 loc) · 1.96 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
import Matter from "./matter.js";
import settings from "./settings.js";
import audio from "./audio.js";
export default class Power {
constructor(options, x, y, action) {
this.body = Matter.Bodies.circle(x, y, 30, settings.POW_OPTIONS);
this.body.power = this;
if(action)
this.action = action;
this.options = options;
this.ringColor = 'white';
this.active = (this.options.active === 1) || this.options.active > Math.random();
this.good = (this.options.good === 1) || this.options.good > Math.random();
if(this.active) {
this.good ? this.ringColor = 'green' : this.ringColor = 'red';
}
this.body.render.sprite.texture = this.getTexture(this.options.icon, this.ringColor);
this.body.render.sprite.xScale = this.body.render.sprite.yScale = 0.5;
Matter.Body.setAngularVelocity(this.body, (Math.random() * 2 - 1) * 0.05);
}
hit() {
if(this.active) {
audio.play('pop');
this.active = false;
this.deathCount = 10;
}
else {
audio.play('pop');
this.good ? this.ringColor = 'green' : this.ringColor = 'red';
this.body.render.sprite.texture = this.getTexture(
this.options.icon,
this.ringColor
);
this.active = true;
}
}
getTexture(string, ringColor) {
let tex = document.createElement('canvas');
let size = 150;
tex.width = tex.height = size;
let ctx = tex.getContext('2d');
ctx.fillStyle = 'lightblue';
ctx.globalAlpha = 0.5;
ctx.beginPath();
ctx.arc(size / 2, size / 2, 60, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();
ctx.strokeStyle = ringColor || 'white';
ctx.lineWidth = 6;
ctx.beginPath();
ctx.arc(size / 2, size / 2, 60, 0, Math.PI * 2, true);
ctx.closePath();
ctx.stroke();
ctx.globalAlpha = 1;
ctx.font = '50px serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(string, size / 2, size / 2 + 5);
return tex.toDataURL('image/png');
}
}