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:
+42
-28
@@ -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,29 +36,44 @@ 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 Change_Resolution_Data:
|
||||||
|
new_index := (game.res_index + e.res_dir + len(RESOLUTIONS)) % len(RESOLUTIONS)
|
||||||
|
game_set_resolution(game, new_index)
|
||||||
|
|
||||||
|
// 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 Game_Over_Data:
|
||||||
|
push_scene(game, make_game_over_overlay(e.winner_is_player))
|
||||||
|
|
||||||
|
case Simple_Event:
|
||||||
|
#partial switch e {
|
||||||
case .Game_Start:
|
case .Game_Start:
|
||||||
game.overlay_scene = nil
|
set_root_scene(game, make_gameplay_scene())
|
||||||
game.scene = make_gameplay_scene()
|
|
||||||
audio_fade_in(&game.audio, duration = 1.5)
|
audio_fade_in(&game.audio, duration = 1.5)
|
||||||
|
|
||||||
case .Game_Pause:
|
case .Game_Pause:
|
||||||
game.overlay_scene = make_pause_overlay()
|
push_scene(game, make_pause_overlay())
|
||||||
|
|
||||||
case .Game_Resume:
|
case .Game_Resume, .Game_Back:
|
||||||
game.overlay_scene = nil
|
pop_scene(game)
|
||||||
|
|
||||||
case .Game_Main_Menu:
|
|
||||||
game.overlay_scene = nil
|
|
||||||
game.scene = make_main_menu()
|
|
||||||
audio_fade_out(&game.audio, duration = 1.0)
|
|
||||||
|
|
||||||
case .Game_Settings:
|
case .Game_Settings:
|
||||||
game.overlay_scene = nil
|
push_scene(game, make_settings_menu(&game.audio, RESOLUTIONS[game.res_index].label))
|
||||||
game.overlay_scene = make_settings_menu(&game.audio)
|
|
||||||
|
|
||||||
case .Game_Over:
|
case .Game_Main_Menu:
|
||||||
game.overlay_scene = make_game_over_overlay(event.winner_is_player)
|
set_root_scene(game, make_main_menu())
|
||||||
|
audio_fade_out(&game.audio, duration = 1.0)
|
||||||
|
|
||||||
case .Game_Quit:
|
case .Game_Quit:
|
||||||
game.should_close = true
|
game.should_close = true
|
||||||
@@ -72,4 +85,5 @@ process_events :: proc(game: ^Game) {
|
|||||||
audio_play_sfx(&game.audio, .Ball_Collision)
|
audio_play_sfx(&game.audio, .Ball_Collision)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user