-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
246 lines (210 loc) · 5.92 KB
/
helpers.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
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
const checkCollisions = (
obj1,
obj2,
obj1Props,
obj2Props,
obj1Border = 10,
obj2Border = 10,
) => {
if (
(
(
(obj1.x + obj1Border <= obj2.x + obj2Props.width - obj2Border) &&
(obj1.x + obj1Border >= obj2.x + obj2Border)
) ||
(
(obj1.x + obj1Props.width - obj1Border >= obj2.x + obj2Border) &&
(obj1.x + obj1Props.width - obj1Border <= obj2.x + obj2Props.width - obj2Border)
)
) &&
(
(obj1.y <= obj2.y + obj2Props.height) && (obj1.y > obj2.y) ||
(obj1.y <= obj2.y) && (obj1.y + obj1Props.height > obj2.y)
)
) {
return true
}
return false
}
const getRoadsideWidth = (game) => {
const { canvas: { width: canvasWidth }} = game
const roadWidth = getRoadWidth()
const roadsideWidth = (canvasWidth - roadWidth) / 2
return roadsideWidth
}
const getCanvasProps = (game) => {
const { canvas } = game
return canvas
}
// objects generation
const enemyImg = document.getElementById('img-enemy').src
const { maxEnemiesNumber, probabilityOfEnemyCreation } = config
const controlEnemies = (game) => {
const {
ctx,
objects: {
enemies,
explosions,
},
} = game
if (enemies.length < maxEnemiesNumber) {
const shouldGenerateNew = Math.random() < probabilityOfEnemyCreation
if (shouldGenerateNew) {
const enemy = new Enemy(game, enemyImg)
enemies.push(enemy)
}
}
enemies.forEach((enemy, idx) => {
enemy.move(game)
const isAlive = enemy.validation(game)
if (isAlive) {
enemy.draw(ctx)
} else {
const removedEnemy = enemies.splice(idx, 1)[0]
if (!removedEnemy.drawn) return
const explosion = new Explosion(removedEnemy.x, removedEnemy.y)
const isExplosionAlive = explosion.validation(game)
if (isExplosionAlive) {
explosions.push(explosion)
}
}
})
}
// trees
const { maxTreesNumber, probabilityOfTreeCreation } = config
const controlTrees = (game) => {
const { objects: { trees }, ctx } = game
if (trees.length < maxTreesNumber) {
const shouldGenerateNew = Math.random() < probabilityOfTreeCreation
if (shouldGenerateNew) {
const tree = new Tree(game)
trees.push(tree)
}
}
trees.forEach((tree, idx) => {
tree.draw(ctx)
})
}
// explosions
const controlExplosions = (game) => {
const { objects: { explosions }, ctx } = game
explosions.forEach((explosion, idx) => {
explosion.move(game)
const isAlive = explosion.validation(game)
if (isAlive) {
explosion.draw(ctx)
} else {
explosions.splice(idx, 1)
}
})
}
// speed controller
const acceleration = 10
const MAX_SPEED = 100
const MIN_SPEED = 0
const setSpeed = (game, newSpeed) => {
if (!game || !game.control) return
game.control.speed = newSpeed
}
const getSpeed = (game) => {
if (!game || !game.control) return
const { control : { speed }} = game
return speed
}
const removeSpeedInterval = (game) => {
game.control.removeInterval = true
}
const updateSpeed = (game, newSpeed) => {
const initialActivePedal = getActiveControlType(game, 'pedals')
const { control: { prevPedal, removeInterval } } = game
if ((initialActivePedal === prevPedal) && !removeInterval) return
const currentSpeed = getSpeed(game)
let timeFrom = 0
const intervalPeriod = 50
const interval = setInterval(() => {
const activePedal = getActiveControlType(game, 'pedals')
let newSpeed
if (initialActivePedal === 'gas') {
newSpeed = currentSpeed + timeFrom * acceleration
} else if (initialActivePedal === 'brake') {
newSpeed = currentSpeed - timeFrom * (acceleration * 5)
} else {
newSpeed = currentSpeed - timeFrom * (acceleration / 2)
}
setSpeed(game, newSpeed)
timeFrom += (intervalPeriod / 1000)
if (activePedal !== initialActivePedal || game.control.removeInterval) {
customClearInterval(game, interval)
}
if (initialActivePedal === 'gas') {
if (newSpeed >= MAX_SPEED) {
setSpeed(game, MAX_SPEED)
customClearInterval(game, interval)
}
} else {
if (newSpeed <= MIN_SPEED) {
setSpeed(game, MIN_SPEED)
customClearInterval(game, interval)
}
}
}, intervalPeriod)
game.control.prevPedal = initialActivePedal
}
const customClearInterval = (game, interval) => {
clearInterval(interval)
game.control.removeInterval = false
}
const activatePedal = (game, pedalType) => {
if (!game || !game.control) return
const pedalState = getPedalState(game, pedalType)
if (pedalState) return
// reset all pedals to false
Object.keys(game.control.pedals)
.forEach((key) => resetPedal(game, key))
game.control.pedals[pedalType] = true
}
const activateTurn = (game, turnType) => {
if (!game || !game.control) return
const turnState = getTurnState(game, turnType)
if (turnState) return
// reset all pedals to false
Object.keys(game.control.turn)
.forEach((key) => resetTurn(game, key))
game.control.turn[turnType] = true
}
// reset pedals or turn
const resetControlType = (game, controlType, key) => {
if (
!game ||
!game.control ||
!game.control[controlType]
) return
game.control[controlType][key] = false
}
const resetPedal = (game, key) => {
resetControlType(game, 'pedals', key)
}
const resetTurn = (game, key) => {
resetControlType(game, 'turn', key)
}
// get active pedals or turn
const getActiveControlType = (game, controlType) => {
const active = Object.keys(game.control[controlType])
.find((key) => {
return game.control[controlType][key]
})
return active
}
const getActiveTurn = (game) => getActiveControlType(game, 'turn')
const getPedalState = (game, pedalType) => {
return game.control.pedals[pedalType]
}
const getTurnState = (game, turnType) => {
return game.control.turn[turnType]
}
// output helper
const speedEl = document.getElementById('speed')
const outputCurrentInfo = (game) => {
const speed = getSpeed(game)
speedEl.innerHTML = Number(speed).toFixed(1)
}