From 27cbaf9403b53e0f80315d0a044ff13e87da1693 Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Mon, 2 Aug 2021 15:16:58 +1000 Subject: [PATCH] actor --- engine/actor.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 engine/actor.go diff --git a/engine/actor.go b/engine/actor.go new file mode 100644 index 0000000..e52e9bc --- /dev/null +++ b/engine/actor.go @@ -0,0 +1,66 @@ +package engine + +import "math" + +// Thorson-style movement: +// https://maddythorson.medium.com/celeste-and-towerfall-physics-d24bd2ae0fc5 + +const collide = false // TODO: add collision detection + +type Actor struct { + X, Y int + + game *Game + xRem, yRem float64 +} + +func (a *Actor) MoveX(dx float64, onCollide func()) { + a.xRem += dx + move := int(math.Round(a.xRem)) + if move == 0 { + return + } + a.xRem -= float64(move) + sign := sign(move) + for move != 0 { + if collide { + if onCollide != nil { + onCollide() + } + return + } + a.X += sign + move -= sign + } +} + +func (a *Actor) MoveY(dy float64, onCollide func()) { + a.yRem += dy + move := int(math.Round(a.yRem)) + if move == 0 { + return + } + a.yRem -= float64(move) + sign := sign(move) + for move != 0 { + if collide { + if onCollide != nil { + onCollide() + } + return + } + a.Y += sign + move -= sign + } +} + +func (a *Actor) Build(g *Game) { + a.game = g +} + +func sign(m int) int { + if m < 0 { + return -1 + } + return 1 +}