-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobstacle.js
More file actions
46 lines (37 loc) · 1.26 KB
/
obstacle.js
File metadata and controls
46 lines (37 loc) · 1.26 KB
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
pc.script.attribute('halfExtents', 'vector', [1,1,1]);
pc.script.attribute('destroyOnContact', 'boolean', true);
pc.script.create('obstacle', function (context) {
var temp = new pc.Vec3();
var pos = new pc.Vec3();
var game = null;
var Obstacle = function (entity) {
this.entity = entity;
};
Obstacle.prototype = {
initialize: function () {
if (!game) {
game = context.root.findByName('Game').script.game;
}
if (!this.destroyOnContact) {
game.on('reset', this.onEnable, this);
}
this.box = new pc.shape.Box(this.entity.getWorldTransform(), this.halfExtents);
},
onEnable: function () {
game.physics.addCollider(this);
},
containsPlayer: function (player) {
// just check the tip of the player instead of doing proper OBB-OBB test
// for optimization
player.getTipPosition(temp);
return this.box.containsPoint(temp);
},
onContact: function () {
game.onCollision(!this.destroyOnContact);
if (this.destroyOnContact) {
this.entity.enabled = false;
}
}
};
return Obstacle;
});