everything I got
This commit is contained in:
54
states/statemachine.go
Normal file
54
states/statemachine.go
Normal file
@@ -0,0 +1,54 @@
|
||||
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)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user