From 715b4b0d161a3971cd0b7e725447a9e367d7767c Mon Sep 17 00:00:00 2001 From: Stephen Cummings Date: Sun, 24 Aug 2014 20:00:05 -0500 Subject: [PATCH] add the player/item collision event back in --- index.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) 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)