diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-05 23:39:55 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-05 23:39:55 +0100 |
commit | a3962f36aaa4d4aace4f56fca482d4f93df1b565 (patch) | |
tree | daa009be9567a40dab9518e7e52faa8fbab604c9 | |
parent | f086908e087bbf29eaa2045f17ebf6de525023b9 (diff) | |
download | rpgedit-a3962f36aaa4d4aace4f56fca482d4f93df1b565.tar rpgedit-a3962f36aaa4d4aace4f56fca482d4f93df1b565.zip |
controller/gamecontext: refactor collision code
-rw-r--r-- | src/controller/gamecontext.ts | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/src/controller/gamecontext.ts b/src/controller/gamecontext.ts index f0a3346..38a08ed 100644 --- a/src/controller/gamecontext.ts +++ b/src/controller/gamecontext.ts @@ -94,34 +94,34 @@ export class GameContext { return diff; } - private updateStep(): void { - const dest = vec2.scaleAndAdd(vec2.create(), this.entityPos, this.entityMovement, this.speed); - const dest2 = vec2.create(); + private updateStepCollide(out: vec2, dest: vec2): boolean { + const move = new Movement(this.entityPos, dest); - let rescan = true; + for (const c of this.collision) { + if (!c.collide(out, move, this.collisionRadius)) + continue; - while (rescan) { - rescan = false; + if (vec2.squaredDistance(this.entityPos, out) >= vec2.squaredDistance(this.entityPos, dest)) + continue; - if (vec2.equals(dest, this.entityPos)) - return; + return true; + } - const move = new Movement(this.entityPos, dest); + return false; + } - for (const c of this.collision) { - if (!c.collide(dest2, move, this.collisionRadius)) - continue; + private updateStep(): void { + const dest = vec2.scaleAndAdd(vec2.create(), this.entityPos, this.entityMovement, this.speed); + const newDest = vec2.create(); - if (!vec2.exactEquals(dest, dest2)) { - // Ensure termination - if (vec2.squaredDistance(this.entityPos, dest2) >= vec2.squaredDistance(this.entityPos, dest)) - continue; + while (true) { + if (vec2.equals(dest, this.entityPos)) + return; - vec2.copy(dest, dest2); - rescan = true; - break; - } - } + if (!this.updateStepCollide(newDest, dest)) + break; + + vec2.copy(dest, newDest); } vec2.copy(this.entityPos, dest); |