Arkadodge/state.odin

53 lines
922 B
Odin
Raw Normal View History

2024-09-04 22:43:27 +03:00
package main
2024-09-05 22:39:52 +03:00
import "core:slice"
2024-09-09 23:41:16 +03:00
StateVariant :: union{^Game, ^Menu, ^Pause, ^GameOver, ^LevelDone, ^Howtoplay}
2024-09-04 22:43:27 +03:00
GameState :: struct {
2024-09-05 22:39:52 +03:00
update: proc(state: ^GameState, delta: f32),
draw: proc(state: ^GameState),
2024-09-09 00:18:35 +03:00
free: proc(state: ^GameState),
previous: ^GameState,
2024-09-04 22:43:27 +03:00
2024-09-05 22:39:52 +03:00
variant: StateVariant
2024-09-04 22:43:27 +03:00
}
new_state :: proc($T: typeid) -> ^T {
state := new(T)
state.variant = state
return state
}
2024-09-05 22:39:52 +03:00
STACK_SIZE :: 16
state_buf : [STACK_SIZE]^GameState
state_stack : [dynamic]^GameState
2024-09-05 23:42:18 +03:00
2024-09-05 22:39:52 +03:00
stack_init :: proc() {
state_stack = slice.into_dynamic(state_buf[:])
}
stack_push :: proc(state: ^GameState) -> (bool) {
if len(state_stack) == STACK_SIZE {
return false
}
append(&state_stack, state)
return true
}
stack_pop :: proc() -> (bool) {
if len(state_stack) == 0 {
return false
}
state := pop(&state_stack)
2024-09-09 00:18:35 +03:00
state->free()
2024-09-05 22:39:52 +03:00
return true
}
2024-09-09 23:41:16 +03:00
stack_top :: proc() -> ^GameState {
return state_stack[len(state_stack)-1]
}