diff options
author | jak1 <mike.wollmann@gmail.com> | 2021-08-01 17:02:17 +0200 |
---|---|---|
committer | jak1 <mike.wollmann@gmail.com> | 2021-08-01 17:02:17 +0200 |
commit | a8fb9c3e5ca4e089b8161b0e0f0150c1100d68c3 (patch) | |
tree | b3eb94b0ff4cd3792fcebb8b34e0e7192d0c7b50 /server/Entities/Bullet.js | |
parent | c9bdbee1b58b8530b6508ea705d5b9fd9a17f627 (diff) | |
download | thepixelworld-a8fb9c3e5ca4e089b8161b0e0f0150c1100d68c3.tar.gz thepixelworld-a8fb9c3e5ca4e089b8161b0e0f0150c1100d68c3.tar.bz2 thepixelworld-a8fb9c3e5ca4e089b8161b0e0f0150c1100d68c3.tar.xz thepixelworld-a8fb9c3e5ca4e089b8161b0e0f0150c1100d68c3.zip |
added some more features, and fixed some bugs (was to lazy to commit every change *hides)
Diffstat (limited to 'server/Entities/Bullet.js')
-rw-r--r-- | server/Entities/Bullet.js | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/server/Entities/Bullet.js b/server/Entities/Bullet.js new file mode 100644 index 0000000..cb5a1a4 --- /dev/null +++ b/server/Entities/Bullet.js @@ -0,0 +1,83 @@ +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.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){ + 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, + }; + } + + self.getUpdatePack = function() { + return { + id:self.id, + x:self.x, + y:self.y, + map:self.map, + }; + } + + 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;
\ No newline at end of file |