Ragnarokkr/state.odin

45 lines
728 B
Odin

package main
import "core:slice"
StateVariant :: union{^Game, ^Menu, ^Pause, ^GameOver, ^Winning}
GameState :: struct {
update: proc(state: ^GameState, delta: f32),
draw: proc(state: ^GameState),
free: proc(state: ^GameState),
previous: ^GameState,
variant: StateVariant
}
new_state :: proc($T: typeid) -> ^T {
state := new(T)
state.variant = state
return state
}
state_stack : [dynamic]^GameState
stack_push :: proc(state: ^GameState) -> (bool) {
append(&state_stack, state)
return true
}
stack_pop :: proc() -> (bool) {
if len(state_stack) == 0 {
return false
}
state := pop(&state_stack)
state->free()
return true
}
stack_top :: proc() -> ^GameState {
return state_stack[len(state_stack)-1]
}