-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullets.js
53 lines (45 loc) · 1.02 KB
/
bullets.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
const { bounds } = require('./print');
function outOfBounds(item) {
return (item.x > (bounds.x - 2) || item.x < 1) || (item.y > (bounds.y - 3) || item.y < 1)
}
const bullets = []
const speed = 2;
const MAX_BULLETS = 3;
const draw = "·"
function fire({ x, y, facing }) {
if (bullets.filter(({ dead }) => !dead).length >= MAX_BULLETS) return
switch(facing) {
case 'up': {
bullets.push({ x, y, dx: 0, dy: -speed, draw })
break;
}
case 'down': {
bullets.push({ x, y, dx: 0, dy: speed, draw })
break;
}
case 'left': {
bullets.push({ x, y, dx: -speed, dy: 0, draw })
break;
}
case 'right': {
bullets.push({ x, y, dx: speed, dy: 0, draw })
break;
}
}
}
function removeDeadBullets() {
bullets.forEach((bullet, idx) => {
if (outOfBounds(bullet) || bullet.dead) {
bullets.splice(idx, 1)
}
})
}
function clearAll () {
bullets.forEach(bullet => bullet.dead = true)
}
module.exports = {
bullets,
fire,
removeDeadBullets,
clearAll,
}