forked from cvan/tanx-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbullet.js
64 lines (52 loc) · 2.12 KB
/
bullet.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
var tmpVec = new pc.Vec3();
pc.script.create('bullet', function (context) {
var Bullet = function (entity) {
this.entity = entity;
this.speed = 1;
this.active = false;
};
Bullet.prototype = {
initialize: function () {
this.fires = context.root.getChildren()[0].script.fires;
// this.entity.audiosource.pitch = Math.random() * 0.2 + 0.7;
this.shadow = this.entity.findByName('shadow');
var self = this;
this.entity.on('culled', function(state) {
self.hidden(state);
});
},
update: function (dt) {
var pos = this.entity.getPosition();
tmpVec.copy(this.entity.targetPosition).sub(pos).normalize().scale(this.entity.speed * dt);
this.entity.setPosition(tmpVec.add(pos));
pos = this.entity.getPosition();
if (tmpVec.copy(this.entity.targetPosition).sub(pos).length() < this.entity.speed * dt * 1.5 ||
pos.x < 0 ||
pos.z < 0 ||
pos.x > 48 ||
pos.z > 48) {
if (! this._hidden) {
var i = Math.floor(Math.random() * 2 + 1);
while(i--) {
context.root.getChildren()[0].script.fires.new({
x: pos.x + (Math.random() - 0.5) * 2,
z: pos.z + (Math.random() - 0.5) * 2,
size: Math.random() * 1 + 1,
life: Math.floor(Math.random() * 50 + 200)
});
}
}
this.entity.fire('finish');
this.entity.enabled = false;
}
},
hidden: function(state) {
if (this._hidden === state)
return;
this._hidden = state;
this.shadow.enabled = ! this._hidden;
this.entity.model.enabled = ! this._hidden;
}
};
return Bullet;
});