diff --git a/index.js b/index.js index ae286fb..3efb6fc 100644 --- a/index.js +++ b/index.js @@ -376,7 +376,11 @@ Game.prototype.control = function(target) { } Game.prototype.potentialCollisionSet = function() { - return [{ collide: this.collideTerrain.bind(this) }] + return [{ + collide: this.collideTerrain.bind(this) + }, { + collide: this.collidePlayer.bind(this) + }] } /** @@ -420,6 +424,19 @@ Game.prototype.collideTerrain = function(other, bbox, vec, resting) { }) } +Game.prototype.collidePlayer = function(item) { + // ignore non-debris items + if(!item.repr || item.repr() !== 'debris') return + + // aabb is a representation of an item's position and dimensions + var playerSpace = this.playerAABB(), + itemSpace = item.aabb() + + if(playerSpace.intersects(itemSpace)) { + this.emit('collision', item) + } +} + // # Three.js specific methods Game.prototype.addStats = function() { @@ -627,7 +644,7 @@ Game.prototype.setTimeout = tic.timeout.bind(tic) Game.prototype.tick = function(delta) { for(var i = 0, len = this.items.length; i < len; ++i) { - this.items[i].tick(delta) + if(this.items[i]) this.items[i].tick(delta) } if (this.materials) this.materials.tick(delta)