diff options
-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); |