forked from cvan/tanx-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tanks.js
100 lines (78 loc) · 3.31 KB
/
tanks.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
pc.script.create('tanks', function (context) {
var Tanks = function (entity) {
this.entity = entity;
this.ind = 0;
var uri = new pc.URI(window.location.href);
var query = uri.getQuery();
if ('multi' in query) {
this.multi = true;
}
};
Tanks.prototype = {
initialize: function () {
this.tank = context.root.findByName('tank');
this.tank.enabled = false;
// this.tank.findByName('light').enabled = false;
this.tanks = context.root.findByName('tanks');
this.client = context.root.getChildren()[0].script.client;
this.camera = context.root.findByName('camera');
this.minimap = context.root.getChildren()[0].script.minimap;
this.multimap = context.root.getChildren()[0].script.multimap;
this.teams = context.root.getChildren()[0].script.teams;
this.hpBar = context.root.getChildren()[0].script.hp;
this.explodeSound = context.root.findByName('explode_sound');
},
new: function(args) {
var newTank = this.tank.clone();
newTank.setName('tank_' + args.id);
newTank.owner = args.owner;
newTank.enabled = true;
newTank.setPosition(args.pos[0], 0, args.pos[1]);
newTank.rotate(0, Math.random() * 360, 0);
this.teams.tankAdd(newTank.script.tank, args.team);
if (args.owner == this.client.id)
this.camera.script.link.link = newTank;
this.tanks.addChild(newTank);
},
delete: function(args) {
var tank = this.tanks.findByName('tank_' + args.id);
if (! tank) return;
tank.destroy();
},
updateData: function(data) {
for(var i = 0; i < data.length; i++) {
var tankData = data[i];
var tank = this.tanks.findByName('tank_' + tankData.id);
if (! tank) continue;
tank = tank.script.tank;
// movement
if (tankData.hasOwnProperty('x'))
tank.moveTo([ tankData.x, tankData.y ]);
// targeting
if (! tank.own && tankData.hasOwnProperty('a'))
tank.targeting(tankData.a);
// hp
if (tankData.hasOwnProperty('hp'))
tank.setHP(tankData.hp);
// shield
tank.sp = tankData.sp || 0;
// dead/alive
tank.dead = tankData.dead || false;
// killer
if (tank.own && tankData.hasOwnProperty('killer')) {
// find killer
tank.killer = this.tanks.findByName('tank_' + tankData.killer);
}
// score
if (tank.own && tankData.hasOwnProperty('s'))
this.hpBar.setScore(tankData.s);
}
if (!this.multi) {
this.minimap.draw();
} else {
this.multimap.render();
}
}
};
return Tanks;
});