summaryrefslogtreecommitdiff
path: root/server/Entities/Bullet.js
blob: cc473628507c8985ab29db7d6d574270509f2015 (plain) (blame)
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
const Entity = require('./Entity');
const Player = require('./Player');

var Bullet = function (parent, angle)
{
    var self = Entity.Entity();
    self.id = Math.random();
    self.speedX = Math.cos(angle / 180 * Math.PI) * 10;
    self.speedY = Math.sin(angle / 180 * Math.PI) * 10;
    self.dir = 0;
    self.parent = parent;
    self.timer = 0;
    self.toRemove = false;

    var super_update = self.update;
    self.update = function ()
    {
        if (self.timer++ > 100)
            self.toRemove = true;
        super_update();
        for (var i in Player.list)
        {
            var p = Player.list[i];
            if (self.getDistance(p) < 32 && self.parent !== p.id)
            {
                if (!p.ignorePlayerAttack)
                    p.hp -= 1;
                if (p.hp <= 0)
                {
                    var shooter = Player.list[self.parent];
                    if (shooter)
                        shooter.score += 1;
                    p.hp = p.hpMax;
                    p.x = Math.random() * 500;
                    p.y = Math.random() * 500;

                }
                self.toRemove = true;
            }
        }
    }

    self.getInitPack = function ()
    {
        return {
            id: self.id,
            x: self.x,
            y: self.y,
            map: self.map,
            dir: self.dir,
        };
    }

    self.getUpdatePack = function ()
    {
        return {
            id: self.id,
            x: self.x,
            y: self.y,
            map: self.map,
            dir: self.dir,
        };
    }

    Bullet.list[self.id] = self;
    Entity.initPack.bullet.push(self.getInitPack());
    return self;
}

Bullet.list = {};

Bullet.update = function ()
{
    var pack = [];
    for (var i in Bullet.list)
    {
        var bullet = Bullet.list[i];
        bullet.update();
        if (bullet.toRemove)
        {
            delete Bullet.list[i];
            Entity.removePack.bullet.push(bullet.id);
        }
        else
            pack.push(bullet.getUpdatePack());
    }
    return pack;
}

Bullet.getAllInitPack = function ()
{
    var bullets = [];
    for (var i in Bullet.list)
        bullets.push(Bullet.list[i].getInitPack());
    return bullets;
}

module.exports = { Bullet };
exports.list = Bullet.list;