From 65766d874851f015bd39d88d86eaff714b3c614d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Thu, 2 Feb 2023 13:24:16 +0100 Subject: [PATCH] small refactor of game layer --- code/foundation/src/core/game.c | 6 ++-- code/foundation/src/core/game.h | 7 +++- code/foundation/src/gui/notifications.h | 3 ++ code/games/minimal/CMakeLists.txt | 1 + code/games/minimal/src/game.c | 22 +++++++++++++ code/games/minimal/src/main.c | 4 --- code/games/minimal/src/platform.c | 1 + code/games/sandbox/CMakeLists.txt | 1 + code/games/sandbox/src/game.c | 22 +++++++++++++ code/games/sandbox/src/main.c | 4 --- code/games/survival/CMakeLists.txt | 1 + code/games/survival/src/game.c | 43 +++++++++++++++++++++++++ code/games/survival/src/game.h | 3 ++ code/games/survival/src/main.c | 19 ++--------- code/games/survival/src/platform.c | 2 -- code/games/survival/src/system_mob.c | 2 +- 16 files changed, 109 insertions(+), 32 deletions(-) create mode 100644 code/foundation/src/gui/notifications.h create mode 100644 code/games/minimal/src/game.c create mode 100644 code/games/sandbox/src/game.c create mode 100644 code/games/survival/src/game.c create mode 100644 code/games/survival/src/game.h diff --git a/code/foundation/src/core/game.c b/code/foundation/src/core/game.c index 5c848aa..1ff17a6 100644 --- a/code/foundation/src/core/game.c +++ b/code/foundation/src/core/game.c @@ -228,14 +228,14 @@ game_kind game_get_kind(void) { return game_mode; } -void game_input() { +void game_core_input() { if (game_mode != GAMEKIND_HEADLESS) { platform_input(); UpdateNuklear(game_ui); } } -void game_update() { +void game_core_update() { static double last_update = 0.0f; if (game_mode == GAMEKIND_CLIENT) { network_client_tick(); @@ -257,7 +257,7 @@ void game_update() { last_update = get_cached_time(); } -void game_render() { +void game_core_render() { if (game_mode != GAMEKIND_HEADLESS) { platform_render(); } diff --git a/code/foundation/src/core/game.h b/code/foundation/src/core/game.h index 4d87431..8ae274a 100644 --- a/code/foundation/src/core/game.h +++ b/code/foundation/src/core/game.h @@ -19,13 +19,18 @@ float game_time(); game_kind game_get_kind(void); //~ NOTE(zaklaus): game events +// Implemented by games void game_input(); void game_update(); void game_render(); - void game_player_joined(uint64_t ent); void game_player_departed(uint64_t ent); +// base methods called from games +void game_core_input(); +void game_core_update(); +void game_core_render(); + //~ Called from platform.c void game_draw_ui(); diff --git a/code/foundation/src/gui/notifications.h b/code/foundation/src/gui/notifications.h new file mode 100644 index 0000000..5a3dce2 --- /dev/null +++ b/code/foundation/src/gui/notifications.h @@ -0,0 +1,3 @@ +#pragma once + +void notification_push(const char* title, const char* text); diff --git a/code/games/minimal/CMakeLists.txt b/code/games/minimal/CMakeLists.txt index 08a019d..578ca24 100644 --- a/code/games/minimal/CMakeLists.txt +++ b/code/games/minimal/CMakeLists.txt @@ -4,6 +4,7 @@ add_executable(minimal src/worldgen.c src/texgen.c src/rules.c + src/game.c ) target_compile_definitions(minimal PRIVATE CLIENT) diff --git a/code/games/minimal/src/game.c b/code/games/minimal/src/game.c new file mode 100644 index 0000000..c15e41c --- /dev/null +++ b/code/games/minimal/src/game.c @@ -0,0 +1,22 @@ +#include "core/game.h" + +void game_input() { + game_core_input(); +} + +void game_update() { + game_core_update(); +} + +void game_render() { + game_core_render(); +} + + +void game_player_joined(uint64_t ent) { + +} + +void game_player_departed(uint64_t ent) { + +} \ No newline at end of file diff --git a/code/games/minimal/src/main.c b/code/games/minimal/src/main.c index b90df32..7caafe3 100644 --- a/code/games/minimal/src/main.c +++ b/code/games/minimal/src/main.c @@ -80,7 +80,3 @@ int main(int argc, char** argv) { zpl_opts_free(&opts); return 0; } - -//------------------------------------------------------------------------ -void game_player_joined(uint64_t ent) {} -void game_player_departed(uint64_t ent) {} \ No newline at end of file diff --git a/code/games/minimal/src/platform.c b/code/games/minimal/src/platform.c index d1222c0..5d06551 100644 --- a/code/games/minimal/src/platform.c +++ b/code/games/minimal/src/platform.c @@ -22,6 +22,7 @@ ZPL_DIAGNOSTIC_POP #include "platform/arch.h" #include "gui/tooltip.c" +#include "gui/notifications.c" #include "renderer.c" diff --git a/code/games/sandbox/CMakeLists.txt b/code/games/sandbox/CMakeLists.txt index eda60c1..8fb250e 100644 --- a/code/games/sandbox/CMakeLists.txt +++ b/code/games/sandbox/CMakeLists.txt @@ -4,6 +4,7 @@ add_executable(eco2d src/worldgen.c src/texgen.c src/rules.c + src/game.c ) target_compile_definitions(eco2d PRIVATE CLIENT) diff --git a/code/games/sandbox/src/game.c b/code/games/sandbox/src/game.c new file mode 100644 index 0000000..c15e41c --- /dev/null +++ b/code/games/sandbox/src/game.c @@ -0,0 +1,22 @@ +#include "core/game.h" + +void game_input() { + game_core_input(); +} + +void game_update() { + game_core_update(); +} + +void game_render() { + game_core_render(); +} + + +void game_player_joined(uint64_t ent) { + +} + +void game_player_departed(uint64_t ent) { + +} \ No newline at end of file diff --git a/code/games/sandbox/src/main.c b/code/games/sandbox/src/main.c index d82e0bd..8327227 100644 --- a/code/games/sandbox/src/main.c +++ b/code/games/sandbox/src/main.c @@ -84,7 +84,3 @@ int main(int argc, char** argv) { zpl_opts_free(&opts); return 0; } - -//------------------------------------------------------------------------ -void game_player_joined(uint64_t ent) {} -void game_player_departed(uint64_t ent) {} \ No newline at end of file diff --git a/code/games/survival/CMakeLists.txt b/code/games/survival/CMakeLists.txt index ccfcd3b..cca030d 100644 --- a/code/games/survival/CMakeLists.txt +++ b/code/games/survival/CMakeLists.txt @@ -4,6 +4,7 @@ add_executable(survival src/worldgen.c src/texgen.c src/rules.c + src/game.c ) target_compile_definitions(minimal PRIVATE CLIENT) diff --git a/code/games/survival/src/game.c b/code/games/survival/src/game.c new file mode 100644 index 0000000..ec08b59 --- /dev/null +++ b/code/games/survival/src/game.c @@ -0,0 +1,43 @@ +#include "core/game.h" +#include "game.h" +#include "world/world.h" +#include "models/components.h" +#include "systems/systems.h" +#include "models/entity.h" +#include "world/entity_view.h" + +#include "gui/notifications.h" + +// custom systems +#include "system_mob.c" + +void mob_systems(ecs_world_t *ecs) { + ECS_SYSTEM_TICKED_EX(ecs, MobDetectPlayers, EcsPostUpdate, 100.0f, components.Position, components.Mob); + ECS_SYSTEM(ecs, MobMovement, EcsPostUpdate, components.Velocity, components.Position, components.MobHuntPlayer); + ECS_SYSTEM_TICKED(ecs, MobMeleeAtk, EcsPostUpdate, components.Position, components.Mob, components.MobHuntPlayer, components.MobMelee); + //ECS_OBSERVER(ecs, MobDetectPlayers1, EcsOnAdd, components.Mob); +} + +void game_input() { + game_core_input(); +} + +void game_update() { + game_core_update(); +} + +void game_render() { + game_core_render(); +} + +void game_setup_ecs() { + mob_systems(world_ecs()); +} + +void game_player_joined(uint64_t ent) { + notification_push("test1", "Hello World!"); +} + +void game_player_departed(uint64_t ent) { + +} \ No newline at end of file diff --git a/code/games/survival/src/game.h b/code/games/survival/src/game.h new file mode 100644 index 0000000..3d63f13 --- /dev/null +++ b/code/games/survival/src/game.h @@ -0,0 +1,3 @@ +#pragma once + +void game_setup_ecs(); diff --git a/code/games/survival/src/main.c b/code/games/survival/src/main.c index 0b88cdd..4c94411 100644 --- a/code/games/survival/src/main.c +++ b/code/games/survival/src/main.c @@ -2,6 +2,7 @@ #include "zpl.h" #include "platform/system.h" #include "core/game.h" +#include "game.h" #include "models/entity.h" #include "world/entity_view.h" #include "utils/options.h" @@ -37,14 +38,6 @@ ZPL_DIAGNOSTIC_POP - somewhat believable world gen, small hamlets with cols, etc */ -#include "system_mob.c" - -void mob_systems(ecs_world_t *ecs) { - ECS_SYSTEM_TICKED_EX(ecs, MobDetectPlayers, EcsPostUpdate, 100.0f, components.Position, components.Mob); - ECS_SYSTEM(ecs, MobMovement, EcsPostUpdate, components.Velocity, components.Position, components.MobHuntPlayer); - ECS_SYSTEM_TICKED(ecs, MobMeleeAtk, EcsPostUpdate, components.Position, components.Mob, components.MobHuntPlayer, components.MobMelee); - //ECS_OBSERVER(ecs, MobDetectPlayers1, EcsOnAdd, components.Mob); -} int main(int argc, char** argv) { zpl_opts opts={0}; @@ -96,11 +89,7 @@ int main(int argc, char** argv) { sighandler_register(); game_init(host, port, play_mode, 1, seed, chunk_size, world_size, 0); - - { - mob_systems(world_ecs()); - } - + game_setup_ecs(); game_run(); game_shutdown(); @@ -110,7 +99,3 @@ int main(int argc, char** argv) { zpl_opts_free(&opts); return 0; } - -//------------------------------------------------------------------------ -void game_player_joined(uint64_t ent) {} -void game_player_departed(uint64_t ent) {} \ No newline at end of file diff --git a/code/games/survival/src/platform.c b/code/games/survival/src/platform.c index e0384e2..b17265a 100644 --- a/code/games/survival/src/platform.c +++ b/code/games/survival/src/platform.c @@ -34,8 +34,6 @@ ZPL_DIAGNOSTIC_POP void platform_init() { platform_create_window("horde survival game"); renderer_init(); - - notification_push("test1", "Hello World!"); } void platform_shutdown() { diff --git a/code/games/survival/src/system_mob.c b/code/games/survival/src/system_mob.c index 939e7c1..84579d6 100644 --- a/code/games/survival/src/system_mob.c +++ b/code/games/survival/src/system_mob.c @@ -28,7 +28,7 @@ void MobDetectPlayers(ecs_iter_t *it) { } void MobDetectPlayers1(ecs_iter_t *it) { - MobDetectPlayers(it); + } #define MOB_MOVEMENT_SPEED 300.0f