Files
aego/modules/gameplay/res/docs/try/StateMachine.aes

67 lines
1.8 KiB
Plaintext

package demo
import (
"aego/debug"
"aego/input"
)
// Player keeps its states in a State Machine: Idle, Run and Air, each with its own hooks and colour.
script Player {
func Update(dt float) {
m := node.Get<Motion2D>()
x := 0.0
if input.Down(input.KeyA) || input.Down(input.KeyLeft) {
x -= 1
}
if input.Down(input.KeyD) || input.Down(input.KeyRight) {
x += 1
}
m.Move(x)
if input.KeyPressed(input.KeySpace) || input.KeyPressed(input.KeyW) || input.KeyPressed(input.KeyUp) {
m.Jump()
}
if input.KeyReleased(input.KeySpace) || input.KeyReleased(input.KeyW) || input.KeyReleased(input.KeyUp) {
m.JumpReleased()
}
sm := node.Get<StateMachine>()
p := node.WorldPosition()
debug.Text(Vec2{p.X - 0.4, p.Y - 1}, sm.Current, Color{1, 1, 1, 1})
}
func UpdateIdle(dt float) {
m := node.Get<Motion2D>()
if !m.Grounded {
node.Get<StateMachine>().Go("Air")
} else if m.Velocity.X > 0.5 || m.Velocity.X < -0.5 {
node.Get<StateMachine>().Go("Run")
}
}
func UpdateRun(dt float) {
m := node.Get<Motion2D>()
if !m.Grounded {
node.Get<StateMachine>().Go("Air")
} else if m.Velocity.X < 0.5 && m.Velocity.X > -0.5 {
node.Get<StateMachine>().Go("Idle")
}
}
func UpdateAir(dt float) {
if node.Get<Motion2D>().Grounded {
node.Get<StateMachine>().Go("Idle")
}
}
func EnterIdle() {
node.Get<SpriteRenderer>().Tint = Color{0.96, 0.62, 0.04, 1}
}
func EnterRun() {
node.Get<SpriteRenderer>().Tint = Color{0.2, 0.8, 0.4, 1}
}
func EnterAir() {
node.Get<SpriteRenderer>().Tint = Color{0.3, 0.6, 1, 1}
}
}