herd / commit
Keep dog facing its last direction of travel when stopped
src/entities/Dog.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/entities/Dog.ts b/src/entities/Dog.ts index f24981d..cd6043b 100644 --- a/src/entities/Dog.ts +++ b/src/entities/Dog.ts @@ -7,6 +7,7 @@ export class Dog { vel: Vec2 = Vec2.ZERO target: Vec2 | null = null speed: number + private facing: number = -Math.PI / 2 constructor(x: number, y: number, speed = 160) { this.pos = new Vec2(x, y) @@ -26,14 +27,13 @@ export class Dog { return } this.vel = diff.normalize().scale(this.speed) + this.facing = Math.atan2(this.vel.y, this.vel.x) this.pos = this.pos.add(this.vel.scale(dt)) } draw(ctx: CanvasRenderingContext2D, scale = 1) { const { x, y } = this.pos - const facing = this.vel.lengthSq() > 0 - ? Math.atan2(this.vel.y, this.vel.x) - : -Math.PI / 2 + const facing = this.facing ctx.save() ctx.translate(x, y)