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
// all possible game events
Event_Type :: enum {
// game state
Change_Resolution_Data :: struct { res_dir: int }
Game_Over_Data :: struct { winner_is_player: bool }
Simple_Event :: enum {
Game_Start,
Game_Over,
Game_Quit,
Game_Pause,
Game_Resume,
Game_Back,
Game_Main_Menu,
Game_Settings,
// ui events
Game_Over_Generic,
Game_Quit,
UI_Select,
// gameplay
Score_Change,
Ball_Collision,
}
// optional details attached to an event
Event :: struct {
type: Event_Type,
winner_is_player: bool,
Event :: union {
Simple_Event,
Change_Resolution_Data,
Game_Over_Data,
}
// global event storage
@@ -38,38 +36,54 @@ flush_events :: proc() {
process_events :: proc(game: ^Game) {
for event in GLOBAL_EVENTS {
#partial switch event.type {
case .Game_Start:
game.overlay_scene = nil
game.scene = make_gameplay_scene()
audio_fade_in(&game.audio, duration = 1.5)
switch e in event {
case Change_Resolution_Data:
new_index := (game.res_index + e.res_dir + len(RESOLUTIONS)) % len(RESOLUTIONS)
game_set_resolution(game, new_index)
case .Game_Pause:
game.overlay_scene = make_pause_overlay()
case .Game_Resume:
game.overlay_scene = nil
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:
// Dynamically update the active UI_Selector label on screen
if active_scene := get_active_scene(game); active_scene != nil {
if canvas, ok := &active_scene.canvas.?; ok {
if menu, menu_ok := &canvas.menu.?; menu_ok {
if selector, sel_ok := &menu.items[1].(UI_Selector); sel_ok {
selector.value_label = RESOLUTIONS[game.res_index].label
}
}
}
}
audio_play_sfx(&game.audio, .UI_Select)
case .Ball_Collision:
audio_play_sfx(&game.audio, .Ball_Collision)
case Game_Over_Data:
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)
}
}
}
}