-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstation.js
76 lines (60 loc) · 1.77 KB
/
station.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Station {
constructor(x, y, w, h) {
this.deltaHealth = 0.01;
this.health = 1.0;
this.clr = 255;
const options = {
restitution: 0.5,
//density: 0.1, no effect //default is 0.001, mass gets calculated
isStatic: true, //can't dampen movement, body & p5 pos not equal, needs edges()
//isSensor: true,
label: "station"
}
this.body = Matter.Bodies.rectangle(x, y, w, h, options);
Body.setMass(this.body, this.body.mass*4);
//Body.setDensity(this.body, 100);
let group = Body.nextGroup(false); //to get a non-colliding group, then set
this.body.collisionFilter.group = group;
//console.log(group);
//console.log("station - Body Collision Filter = ", this.body.collisionFilter);
Matter.World.add(world, this.body);
this.id = this.body.id;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
show() {
const pos = this.body.position;
const angle = this.body.angle;
push();
translate(pos.x, pos.y);
rotate(angle);
noStroke()
fill(this.clr);
rectMode(CENTER);
rect(0, 0, this.w, this.h);
pop();
}
changeColor() {
var gr = color(255, 255, 255);
var rd = color(255, 0, 0);
this.clr = lerpColor(gr, rd, 1 - this.health);
}
explode(idx) {
//console.log(this.x, this.y);
//for (let i = 0; i < 15; i++) {
let offsetX = random(this.x - this.w/2, this.x + this.w/2);
let offsetY = random(this.y - this.h/2, this.y + this.h/2);
//let numP = floor(random(400, 800));
fireworks.push(new Firework(offsetX, offsetY, 200));
//remove p5 station from array - body removed in addEvents collisionEnd
stations.splice(idx, 1);
}
collisions(hitMass) {
this.health -= this.deltaHealth * hitMass;
this.health =
(this.health < 0) ? 0 : this.health;
this.changeColor();
}
}