-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbullets.js
127 lines (100 loc) · 4.03 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
pc.script.create('bullets', function (context) {
var vecTmp = new pc.Vec3();
var matSpecial = null;
var matDefault = null;
var Bullets = function (entity) {
this.entity = entity;
};
Bullets.prototype = {
initialize: function () {
this.tanks = context.root.findByName('tanks');
this.bullet = context.root.findByName('bullet');
this.bullet.enabled = false;
this.bullets = context.root.findByName('bullets');
if (! matSpecial) {
var bulletSpecial = context.root.findByName('bullet-special');
matDefault = this.bullet.model.material;
matSpecial = bulletSpecial.model.material;
bulletSpecial.destroy();
}
this.active = [ ];
this.pool = [ ];
this.index = { };
this.length = 0;
},
new: function(data) {
var self = this;
if (this.pool.length === 0) {
var before = this.length;
// extend pool
this.length += 8;
for(var i = 0; i < this.length - before; i++) {
var bullet = this.bullet.clone();
// destroy when bullet has finished its life
bullet.on('finish', function() {
self.delete({ id: this.id });
});
bullet.active = false;
this.bullets.addChild(bullet);
// add to pool
this.pool.push(bullet);
}
}
var tank = this.tanks.findByName('tank_' + data.tank);
if (! tank) return;
// get bullet from pool
var bullet = this.pool.pop();
this.active.push(bullet);
bullet.active = true;
// attach ID
bullet.id = data.id;
// index
this.index[data.id] = bullet;
// clear minimap data
bullet.lastX = undefined;
bullet.lastZ = undefined;
bullet.oldX = undefined;
bullet.oldY = undefined;
// offset
vecTmp.set(0, 0, 1);
tank.script.tank.head.getRotation().transformVector(vecTmp, vecTmp);
vecTmp.normalize().scale(0.5);
bullet.setPosition(tank.getPosition().x + vecTmp.x, 0.9, tank.getPosition().z + vecTmp.z);
bullet.targetPosition = new pc.Vec3(data.tx, 0.9, data.ty);
bullet.speed = data.sp * 50 * 0.5;
// material and scale if special
if (data.s) {
bullet.model.material = matSpecial;
bullet.setLocalScale(.3, .2, .3);
} else {
bullet.model.material = matDefault;
bullet.setLocalScale(.2, .2, .2);
}
bullet.enabled = true;
},
delete: function(args) {
var bullet = this.index[args.id];
if (! bullet) return;
// fire particles
var i = Math.floor(Math.random() * 2 + 1);
while(i--) {
context.root.getChildren()[0].script.fires.new({
x: args.x + (Math.random() - 0.5) * 2,
z: args.y + (Math.random() - 0.5) * 2,
size: Math.random() * 1 + 1,
life: Math.floor(Math.random() * 50 + 200)
});
}
// remove from index
delete this.index[args.id];
// disable
bullet.enabled = false;
// push to pool back
this.pool.push(bullet);
var ind = this.active.indexOf(bullet);
if (ind !== -1)
this.active.splice(ind, 1);
}
};
return Bullets;
});