55 lines
985 B
Go
55 lines
985 B
Go
|
|
package states
|
||
|
|
|
||
|
|
import (
|
||
|
|
"github.com/hajimehoshi/ebiten/v2"
|
||
|
|
)
|
||
|
|
|
||
|
|
var SM StateMachine
|
||
|
|
var StateMap map[string]State
|
||
|
|
|
||
|
|
type State interface {
|
||
|
|
Enter()
|
||
|
|
Exit()
|
||
|
|
Update()
|
||
|
|
Draw(screen *ebiten.Image)
|
||
|
|
}
|
||
|
|
|
||
|
|
type StateMachine struct {
|
||
|
|
CurrentState State
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewStateMachine(initialState State) *StateMachine {
|
||
|
|
sm := &StateMachine{CurrentState: initialState}
|
||
|
|
sm.CurrentState.Enter()
|
||
|
|
return sm
|
||
|
|
}
|
||
|
|
|
||
|
|
func (sm *StateMachine) Transition(newState string) {
|
||
|
|
sm.CurrentState.Exit()
|
||
|
|
sm.CurrentState = StateMap[newState]
|
||
|
|
sm.CurrentState.Enter()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (sm *StateMachine) Update() {
|
||
|
|
sm.CurrentState.Update()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (sm *StateMachine) Draw(screen *ebiten.Image) {
|
||
|
|
sm.CurrentState.Draw(screen)
|
||
|
|
}
|
||
|
|
|
||
|
|
func SetupStateMachine() {
|
||
|
|
|
||
|
|
loadState := &LoadLevelState{}
|
||
|
|
playState := &PlayLevelState{}
|
||
|
|
gameOverState := &GameOverState{}
|
||
|
|
|
||
|
|
StateMap = make(map[string]State)
|
||
|
|
StateMap["load"] = loadState
|
||
|
|
StateMap["play"] = playState
|
||
|
|
StateMap["game over"] = gameOverState
|
||
|
|
|
||
|
|
SM = *NewStateMachine(loadState)
|
||
|
|
|
||
|
|
}
|