-
Notifications
You must be signed in to change notification settings - Fork 3
/
vehicle.js
109 lines (76 loc) · 1.86 KB
/
vehicle.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class Vehicle {
constructor(x, y, modelIndex = 0) {
this.name = "vehicle";
this.x = x;
this.y = y;
this.radius = 30;
this.width = 2 * this.radius;
this.height = 2 * this.radius;
this.life = 100;
this.ground = y;
this.velX = 0;
this.velY = 0;
this.lastY = this.y;
this.hasExploded = false;
this.listeners = [];
this.name = "";
this.imageWidth = 60;
this.imageHeight = 60;
this.smokePosition = {
x: 0,
y: 10
};
this.Width = 20;
this.Length = 40;
}
step(dt) {
// apply forces
this.x += dt * this.velX;
this.y += dt * this.velY;
}
draw(ctx) {
ctx.save();
ctx.translate(this.x, this.y);
if (game.debug) {
ctx.beginPath(),
ctx.arc(0, 0, this.radius, 0, Math.PI * 2);
ctx.stroke();
ctx.closePath();
ctx.rect(-this.width / 2, -this.height / 2, this.width, this.height);
ctx.stroke();
}
ctx.restore();
}
destroyVehicle() {
if (!this.hasExploded) {
this.exploding();
this.life = 0;
this.hasExploded = true;
}
}
addListener(listener) {
this.listeners.push(listener);
}
smoking() {
this.listeners.forEach(listener => {
listener.smoking(this);
});
}
exploding() {
this.listeners.forEach(listener => {
listener.exploding(this);
});
}
smokingPosition() {
return {
x: this.x + this.smokePosition.x,
y: this.y + this.smokePosition.y
};
}
explodePosition() {
return {
x: this.x,
y: this.y
};
}
}