-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverBullet.js
60 lines (56 loc) · 1.56 KB
/
serverBullet.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
// var bullets = [];
var enemyList = [];
function checkCollision(bullet) {
for (var i = 0; i < enemyList.length; i++) {
enemy = enemyList[i];
// HTML5 ROCKS COLLISION DETECTION
if (bullet.x < enemy.x + enemy.width &&
bullet.x + bullet.width > enemy.x &&
bullet.y < enemy.y + enemy.height &&
bullet.y + bullet.height > enemy.y)
{
enemy.health -= bullet.damage;
enemyList.splice(i, 1);
i--;
return true;
}
}
return false;
}
exports.moveBullets = function(lobby) {
bullets = lobby.bulletList;
enemyList = lobby.enemyList;
for (var i = 0; i < bullets.length; i++) {
bullets[i].x += bullets[i].dx;
bullets[i].y += bullets[i].dy;
if (checkCollision(bullets[i], lobby)) {
bullets.splice(i, 1);
i--;
}
// should not be hardcoded
else {
if (bullets[i].x < -200 || bullets[i].x > 763 || bullets[i].y < -200 || bullets[i].y > 510) {
bullets.splice(i, 1);
i--;
}
}
}
}
exports.createBullet = function(player, dx, dy, lobby) {
if (player !== undefined && player.x !== undefined && player.y !== undefined){
var bullet = {};
// bullet.damage = player.damage;
// bullet.name = player.name;
bullet.x = player.x + 12;
bullet.y = player.y + 17;
bullet.dx = dx *3;
bullet.dy = dy *3;
bullet.start = true;
bullet.height = 5;
bullet.width = 5;
lobby.bulletList.push(bullet);
console.log("lobby => ", lobby.name);
console.log("bulletListLENGTH => ", lobby.bulletList.length);
// bullets.push(bullet);
}
}