-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactors.js
More file actions
251 lines (207 loc) · 5.41 KB
/
actors.js
File metadata and controls
251 lines (207 loc) · 5.41 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
class Car {
constructor(x = 0, y = 0, imgSrc) {
this.x = x
this.y = y
this.image = new Image()
this.image.src = imgSrc
}
draw(ctx) {
const { x, y } = this
ctx.drawImage(this.image, x, y, Car.width, Car.height)
this.drawn = true
}
}
Car.height = 160
Car.width = 80
class Enemy extends Car {
constructor(game, imgSrc) {
const { canvas: { width: canvasWidth }} = game
const roadWidth = getRoadWidth()
const roadsideWidth = (canvasWidth - roadWidth) / 2
const randomVar = Math.random()
const x = (randomVar * (roadWidth - Enemy.width)) + roadsideWidth
const y = -Enemy.height
super(x, y, imgSrc)
const [minSpeed, maxSpeed] = [10, 100]
this.speed = (randomVar * (maxSpeed - minSpeed)) + minSpeed
this.direction = randomVar < 0.33
? 'left'
: randomVar > 0.66
? 'right'
: null
this.numberOfValidtions = 0
this.rotationDirection = null
}
move(game) {
const speed = getSpeed(game)
this.y = this.y + (speed - this.speed)
const shift = this.speed * 0.05
const roadWidth = getRoadWidth()
const roadsideWidth = getRoadsideWidth(game)
if (this.direction === 'left') {
this.x -= shift
if (this.x < roadsideWidth) {
this.direction = 'right'
}
} else if (this.direction === 'right') {
this.x += shift
if (this.x > roadsideWidth + roadWidth - Enemy.width) {
this.direction = 'left'
}
}
}
validation(game) {
this.numberOfValidtions++
// make validation on each 5th iteration
if (this.numberOfValidtions < 5) return true
else this.numberOfValidtions = 0
const { y } = this
const { height: canvasHeight } = getCanvasProps(game)
if (
(y + Enemy.height < -canvasHeight) ||
(y > canvasHeight)
) return false
const {
objects: { enemies = [] },
} = game
// check collisions with another enemies
return enemies.every((enemy) => {
if (enemy === this) return true
const isCollision = checkCollisions(this, enemy, Enemy, Enemy)
if (isCollision) return false
return true
})
return true
}
}
Enemy.height = 160
Enemy.width = 80
const playerImg = document.getElementById('img-player')
class Player extends Car {
constructor(x = 300, y = 400) {
super(x, y)
this.rotationDirection = null
}
draw(ctx) {
const { x, y, rotationDirection } = this
const {
width: pWidth,
height: pHeight,
rotationDeg,
} = Player
if (rotationDirection) {
ctx.save()
const deg = rotationDirection === 'right' ? rotationDeg : -rotationDeg
var rad = deg * Math.PI / 180
// Set the origin to the center of the image
ctx.translate(x + pWidth / 2, y + pHeight / 5)
// Rotate the canvas around the origin
ctx.rotate(rad)
ctx.drawImage(Player.image, pWidth / 2 * (-1), pHeight / 5 * (-1), pWidth, pHeight)
ctx.restore()
} else {
ctx.drawImage(Player.image, x, y, Player.width, Player.height)
}
}
move(game) {
const speed = getSpeed(game)
const turn = getActiveTurn(game)
const roadWidth = getRoadWidth()
const roadsideWidth = getRoadsideWidth(game)
if (turn === 'left' && this.x > roadsideWidth) {
this.x -= 0.4 * speed
} else if (turn === 'right' && this.x < roadsideWidth + roadWidth - Player.width) {
this.x += 0.4 * speed
}
}
moveCarByEvent(e, game) {
switch(e.key) {
case 'ArrowLeft': {
activateTurn(game, 'left')
this.rotationDirection = 'left'
break
}
case 'ArrowRight': {
activateTurn(game, 'right')
this.rotationDirection = 'right'
break
}
case 'ArrowDown': {
activatePedal(game, 'brake')
break
}
case 'ArrowUp': {
activatePedal(game, 'gas')
break
}
}
}
stopKeyEvent(e, game) {
switch(e.key) {
case 'ArrowLeft': {
this.stopTurning()
resetTurn(game, 'left')
break
}
case 'ArrowRight': {
this.stopTurning()
resetTurn(game, 'right')
break
}
case 'ArrowDown': {
resetPedal(game, 'brake')
break
}
case 'ArrowUp': {
resetPedal(game, 'gas')
break
}
}
}
stopTurning() {
this.rotationDirection = null
}
}
Player.height = 160
Player.width = 80
Player.rotationDeg = 10
Player.image = playerImg
const explosprite = document.getElementById('img-explosprite')
const FRAME_SIZE = 128
class Explosion {
constructor(x = 0, y = 0) {
this.x = x
this.y = y
this.image = new Image()
this.image.src = explosprite.src
this.currentFrame = 0
}
move(game) {
const speed = getSpeed(game)
const { y } = this
this.y += speed
}
draw(ctx) {
if (this.currentFrame > 15) return
const { x, y } = this
const dx = (this.currentFrame % 4) * FRAME_SIZE
const dy = Math.floor(this.currentFrame / 4) * FRAME_SIZE
ctx.drawImage(
this.image,
dx, dy,
Explosion.width, Explosion.height,
x, y,
FRAME_SIZE, FRAME_SIZE
)
this.currentFrame++
}
validation(game) {
const { y } = this
const { height: canvasHeight } = getCanvasProps(game)
if ((y < -Explosion.height) || (y > canvasHeight)) return false
if (this.currentFrame > 15) return false
return true
}
}
Explosion.height = FRAME_SIZE
Explosion.width = FRAME_SIZE