Refactor for easier management.

Refactored to have events be a union so we can pack data in cases.
And in others just emit a simple notifying event.
This commit is contained in:
2026-09-21 07:31:06 +02:00
parent df18e0c8b7
commit 1ce0992cf3
+56 -42
View File
@@ -1,28 +1,26 @@
package game package game
// all possible game events Change_Resolution_Data :: struct { res_dir: int }
Event_Type :: enum { Game_Over_Data :: struct { winner_is_player: bool }
// game state
Simple_Event :: enum {
Game_Start, Game_Start,
Game_Over,
Game_Quit,
Game_Pause, Game_Pause,
Game_Resume, Game_Resume,
Game_Back,
Game_Main_Menu, Game_Main_Menu,
Game_Settings, Game_Settings,
Game_Over_Generic,
// ui events Game_Quit,
UI_Select, UI_Select,
// gameplay
Score_Change, Score_Change,
Ball_Collision, Ball_Collision,
} }
// optional details attached to an event Event :: union {
Event :: struct { Simple_Event,
type: Event_Type, Change_Resolution_Data,
winner_is_player: bool, Game_Over_Data,
} }
// global event storage // global event storage
@@ -38,38 +36,54 @@ flush_events :: proc() {
process_events :: proc(game: ^Game) { process_events :: proc(game: ^Game) {
for event in GLOBAL_EVENTS { for event in GLOBAL_EVENTS {
#partial switch event.type { switch e in event {
case .Game_Start: case Change_Resolution_Data:
game.overlay_scene = nil new_index := (game.res_index + e.res_dir + len(RESOLUTIONS)) % len(RESOLUTIONS)
game.scene = make_gameplay_scene() game_set_resolution(game, new_index)
audio_fade_in(&game.audio, duration = 1.5)
case .Game_Pause: // Dynamically update the active UI_Selector label on screen
game.overlay_scene = make_pause_overlay() if active_scene := get_active_scene(game); active_scene != nil {
if canvas, ok := &active_scene.canvas.?; ok {
case .Game_Resume: if menu, menu_ok := &canvas.menu.?; menu_ok {
game.overlay_scene = nil if selector, sel_ok := &menu.items[1].(UI_Selector); sel_ok {
selector.value_label = RESOLUTIONS[game.res_index].label
case .Game_Main_Menu: }
game.overlay_scene = nil }
game.scene = make_main_menu() }
audio_fade_out(&game.audio, duration = 1.0) }
case .Game_Settings:
game.overlay_scene = nil
game.overlay_scene = make_settings_menu(&game.audio)
case .Game_Over:
game.overlay_scene = make_game_over_overlay(event.winner_is_player)
case .Game_Quit:
game.should_close = true
case .UI_Select:
audio_play_sfx(&game.audio, .UI_Select) audio_play_sfx(&game.audio, .UI_Select)
case .Ball_Collision: case Game_Over_Data:
audio_play_sfx(&game.audio, .Ball_Collision) push_scene(game, make_game_over_overlay(e.winner_is_player))
case Simple_Event:
#partial switch e {
case .Game_Start:
set_root_scene(game, make_gameplay_scene())
audio_fade_in(&game.audio, duration = 1.5)
case .Game_Pause:
push_scene(game, make_pause_overlay())
case .Game_Resume, .Game_Back:
pop_scene(game)
case .Game_Settings:
push_scene(game, make_settings_menu(&game.audio, RESOLUTIONS[game.res_index].label))
case .Game_Main_Menu:
set_root_scene(game, make_main_menu())
audio_fade_out(&game.audio, duration = 1.0)
case .Game_Quit:
game.should_close = true
case .UI_Select:
audio_play_sfx(&game.audio, .UI_Select)
case .Ball_Collision:
audio_play_sfx(&game.audio, .Ball_Collision)
}
} }
} }
} }