Refactor collision handling
This commit is contained in:
parent
6c896c3f23
commit
00970b9faa
1 changed files with 7 additions and 6 deletions
|
@ -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) {
|
||||
|
|
Reference in a new issue