-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHazard.js
More file actions
72 lines (66 loc) · 3.46 KB
/
Hazard.js
File metadata and controls
72 lines (66 loc) · 3.46 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
class Hazard {
constructor(x, y, angle, defaultColor) {
this.x = x;
this.y = y;
this.angle = 0;
this.height = 5;
this.width = 5;
this.defaultColor = defaultColor;
this.color = defaultColor;
}
show(ctx) {
// Don't ask me how this works just yet, but one day I'll hopefully
// come back later and learn it better
ctx.save();
ctx.beginPath();
ctx.translate(this.x, this.y);
ctx.rotate(this.angle * Math.PI / 180);
ctx.rect(-this.width / 2, -this.height / 2, this.width, this.height);
ctx.fillStyle = this.color;
ctx.fill();
ctx.restore();
}
collision(booleo) {
if (booleo) {
this.color = "red";
} else {
this.color = this.defaultColor;
}
}
minX() {
// Calculate the X-coordinate of every corner and return the lowest
return Math.min(
this.x + this.width * Math.cos( this.angle * Math.PI / 180 ) / 2 + this.height * Math.sin( this.angle * Math.PI / 180 ) / 2,
this.x + this.width * Math.cos( this.angle * Math.PI / 180 ) / 2 - this.height * Math.sin( this.angle * Math.PI / 180 ) / 2,
this.x - this.width * Math.cos( this.angle * Math.PI / 180 ) / 2 + this.height * Math.sin( this.angle * Math.PI / 180 ) / 2,
this.x - this.width * Math.cos( this.angle * Math.PI / 180 ) / 2 - this.height * Math.sin( this.angle * Math.PI / 180 ) / 2
);
}
maxX() {
// Calculate the X-coordinate of every corner and return the highest
return Math.max(
this.x + this.width * Math.cos( this.angle * Math.PI / 180 ) / 2 + this.height * Math.sin( this.angle * Math.PI / 180 ) / 2,
this.x + this.width * Math.cos( this.angle * Math.PI / 180 ) / 2 - this.height * Math.sin( this.angle * Math.PI / 180 ) / 2,
this.x - this.width * Math.cos( this.angle * Math.PI / 180 ) / 2 + this.height * Math.sin( this.angle * Math.PI / 180 ) / 2,
this.x - this.width * Math.cos( this.angle * Math.PI / 180 ) / 2 - this.height * Math.sin( this.angle * Math.PI / 180 ) / 2
);
}
minY() {
// Calculate the Y-coordinate of every corner and return the lowest
return Math.min(
this.y + this.height * Math.cos( this.angle * Math.PI / 180 ) / 2 + this.width * Math.sin( this.angle * Math.PI / 180 ) / 2,
this.y + this.height * Math.cos( this.angle * Math.PI / 180 ) / 2 - this.width * Math.sin( this.angle * Math.PI / 180 ) / 2,
this.y - this.height * Math.cos( this.angle * Math.PI / 180 ) / 2 + this.width * Math.sin( this.angle * Math.PI / 180 ) / 2,
this.y - this.height * Math.cos( this.angle * Math.PI / 180 ) / 2 - this.width * Math.sin( this.angle * Math.PI / 180 ) / 2
);
}
maxY() {
// Calculate the Y-coordinate of every corner and return the highest
return Math.max(
this.y + this.height * Math.cos( this.angle * Math.PI / 180 ) / 2 + this.width * Math.sin( this.angle * Math.PI / 180 ) / 2,
this.y + this.height * Math.cos( this.angle * Math.PI / 180 ) / 2 - this.width * Math.sin( this.angle * Math.PI / 180 ) / 2,
this.y - this.height * Math.cos( this.angle * Math.PI / 180 ) / 2 + this.width * Math.sin( this.angle * Math.PI / 180 ) / 2,
this.y - this.height * Math.cos( this.angle * Math.PI / 180 ) / 2 - this.width * Math.sin( this.angle * Math.PI / 180 ) / 2
);
}
}