From 00970b9faa9338817ec3dddced6dadef69d86fe3 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 8 Jan 2016 08:27:38 +0100 Subject: Refactor collision handling --- src/control/MapContext.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/control/MapContext.ts b/src/control/MapContext.ts index 4e69408..73b3c4d 100644 --- a/src/control/MapContext.ts +++ b/src/control/MapContext.ts @@ -59,21 +59,22 @@ export default class MapContext { } + private inMap(p: Position): boolean { + return p.x >= 0 && p.x < this.map.width && p.y >= 0 && p.y < this.map.height; + } + private incCollision(p: Position) { - if (this.collision[p.x] && !_.isUndefined(this.collision[p.x][p.y])) + if (this.inMap(p)) this.collision[p.x][p.y]++; } private decCollision(p: Position) { - if (this.collision[p.x] && !_.isUndefined(this.collision[p.x][p.y])) + if (this.inMap(p)) this.collision[p.x][p.y]--; } private collides(p: Position): boolean { - if (this.collision[p.x] && !_.isUndefined(this.collision[p.x][p.y])) - return (this.collision[p.x][p.y] > 0); - else - return true; + return (!this.inMap(p)) || (this.collision[p.x][p.y] > 0); } private addEntity(entity: EntityPosition) { -- cgit v1.2.3