This commit is contained in:
Josh Deprez 2021-08-02 15:16:58 +10:00 committed by Josh Deprez
parent 6e8a3e67df
commit 27cbaf9403

66
engine/actor.go Normal file
View file

@ -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
}