From 8f44f5eec9575c9818da8f5eb80b5483cccfa162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Tue, 19 Jan 2021 11:19:12 +0100 Subject: [PATCH] code: refactor components --- code/apps/server/CMakeLists.txt | 7 ++--- .../server/header/components/controllers.h | 29 +----------------- code/apps/server/header/components/general.h | 20 +------------ code/apps/server/header/components/net.h | 15 +--------- code/apps/server/header/components/physics.h | 17 ++--------- .../server/source/components/controllers.c | 30 +++++++++++++++++++ code/apps/server/source/components/general.c | 19 ++++++++++++ code/apps/server/source/components/net.c | 16 ++++++++++ code/apps/server/source/components/physics.c | 17 +++++++++++ code/apps/server/source/network.c | 2 ++ code/vendors/flecs/CMakeLists.txt | 1 - 11 files changed, 92 insertions(+), 81 deletions(-) create mode 100644 code/apps/server/source/components/controllers.c create mode 100644 code/apps/server/source/components/general.c create mode 100644 code/apps/server/source/components/net.c create mode 100644 code/apps/server/source/components/physics.c diff --git a/code/apps/server/CMakeLists.txt b/code/apps/server/CMakeLists.txt index f021f4f..04dd097 100644 --- a/code/apps/server/CMakeLists.txt +++ b/code/apps/server/CMakeLists.txt @@ -1,3 +1,5 @@ +file(GLOB COMPONENTS header/components/*.h source/components/*.c) + add_executable(eco2d-server source/main.c source/network.c @@ -14,10 +16,7 @@ add_executable(eco2d-server header/world/blocks.h header/world/blocks_info.h - header/components/net.h - header/components/physics.h - header/components/general.h - header/components/controllers.h + ${COMPONENTS} ) include_directories(eco2d-server header) diff --git a/code/apps/server/header/components/controllers.h b/code/apps/server/header/components/controllers.h index b760d63..9363743 100644 --- a/code/apps/server/header/components/controllers.h +++ b/code/apps/server/header/components/controllers.h @@ -2,9 +2,6 @@ #include "flecs/flecs.h" #include "flecs/flecs_meta.h" -#include "components/general.h" -#include "components/physics.h" - ECS_STRUCT(Input, { double x; double y; @@ -28,28 +25,4 @@ typedef struct { ECS_IMPORT_ENTITY(handles, EcsPlayer);\ ECS_IMPORT_ENTITY(handles, EcsBuilder);\ -static inline void ControllersImport(ecs_world_t *ecs) { - ECS_MODULE(ecs, Controllers); - ecs_set_name_prefix(ecs, "Controllers"); - - ECS_IMPORT(ecs, General); - ECS_IMPORT(ecs, Physics); - - ECS_IMPORT(ecs, FlecsMeta); - ECS_META(ecs, Input); - - ECS_TAG(ecs, EcsActor); - ECS_TAG(ecs, EcsPlayer); - ECS_TAG(ecs, EcsBuilder); - - ECS_PREFAB(ecs, Base, general.Position, physics.Velocity, Input, EcsActor); - ECS_TYPE(ecs, Player, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Walking, EcsActor, EcsPlayer); - ECS_TYPE(ecs, Builder, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Flying, EcsActor, EcsBuilder); - - ECS_SET_COMPONENT(Input); - ECS_SET_ENTITY(EcsActor); - ECS_SET_ENTITY(EcsPlayer); - ECS_SET_ENTITY(EcsBuilder); - ECS_SET_TYPE(Builder); - ECS_SET_TYPE(Player); -} +void ControllersImport(ecs_world_t *ecs); diff --git a/code/apps/server/header/components/general.h b/code/apps/server/header/components/general.h index e6c1712..448168b 100644 --- a/code/apps/server/header/components/general.h +++ b/code/apps/server/header/components/general.h @@ -2,8 +2,6 @@ #include "flecs/flecs.h" #include "flecs/flecs_meta.h" -#include "assets.h" - ECS_STRUCT(Vector2D, { int16_t x; int16_t y; @@ -29,20 +27,4 @@ typedef struct { ECS_IMPORT_COMPONENT(handles, Position);\ ECS_IMPORT_COMPONENT(handles, Drawable);\ -static inline void GeneralImport(ecs_world_t *ecs) { - ECS_MODULE(ecs, General); - ecs_set_name_prefix(ecs, "General"); - - ECS_COMPONENT(ecs, Chunk); - ECS_COMPONENT(ecs, Position); - - ECS_IMPORT(ecs, FlecsMeta); - - ECS_META(ecs, Vector2D); - ECS_META(ecs, Drawable); - - ECS_SET_COMPONENT(Chunk); - ECS_SET_COMPONENT(Vector2D); - ECS_SET_COMPONENT(Position); - ECS_SET_COMPONENT(Drawable); -} +void GeneralImport(ecs_world_t *ecs); diff --git a/code/apps/server/header/components/net.h b/code/apps/server/header/components/net.h index 402fbc4..349b6bc 100644 --- a/code/apps/server/header/components/net.h +++ b/code/apps/server/header/components/net.h @@ -15,17 +15,4 @@ typedef struct { ECS_IMPORT_ENTITY(handles, EcsClient);\ ECS_IMPORT_COMPONENT(handles, ClientInfo);\ -static inline void NetImport(ecs_world_t *ecs) { - ECS_MODULE(ecs, Net); - - ecs_set_name_prefix(ecs, "Net"); - - ECS_TAG(ecs, EcsClient); - - ECS_IMPORT(ecs, FlecsMeta); - - ECS_META(ecs, ClientInfo); - - ECS_EXPORT_ENTITY(EcsClient); - ECS_EXPORT_COMPONENT(ClientInfo); -} +void NetImport(ecs_world_t *ecs); diff --git a/code/apps/server/header/components/physics.h b/code/apps/server/header/components/physics.h index be43da4..0169930 100644 --- a/code/apps/server/header/components/physics.h +++ b/code/apps/server/header/components/physics.h @@ -1,5 +1,6 @@ #pragma once #include "flecs/flecs.h" + #include "components/general.h" typedef Vector2D Velocity; @@ -17,18 +18,4 @@ typedef struct { ECS_IMPORT_ENTITY(handles, Flying);\ ECS_IMPORT_COMPONENT(handles, Velocity);\ -static inline void PhysicsImport(ecs_world_t *ecs) { - ECS_MODULE(ecs, Physics); - ecs_set_name_prefix(ecs, "Physics"); - - ECS_TAG(ecs, Walking); - ECS_TAG(ecs, Flying); - ECS_TYPE(ecs, Movement, Walking, Flying); - - ECS_COMPONENT(ecs, Velocity); - - ECS_SET_TYPE(Movement); - ECS_SET_ENTITY(Walking); - ECS_SET_ENTITY(Flying); - ECS_SET_COMPONENT(Velocity); -} +void PhysicsImport(ecs_world_t *ecs); diff --git a/code/apps/server/source/components/controllers.c b/code/apps/server/source/components/controllers.c new file mode 100644 index 0000000..3386e68 --- /dev/null +++ b/code/apps/server/source/components/controllers.c @@ -0,0 +1,30 @@ +#include "components/controllers.h" + +#include "components/general.h" +#include "components/physics.h" + +void ControllersImport(ecs_world_t *ecs) { + ECS_MODULE(ecs, Controllers); + ecs_set_name_prefix(ecs, "Controllers"); + + ECS_IMPORT(ecs, General); + ECS_IMPORT(ecs, Physics); + + ECS_IMPORT(ecs, FlecsMeta); + ECS_META(ecs, Input); + + ECS_TAG(ecs, EcsActor); + ECS_TAG(ecs, EcsPlayer); + ECS_TAG(ecs, EcsBuilder); + + ECS_PREFAB(ecs, Base, general.Position, physics.Velocity, Input, EcsActor); + ECS_TYPE(ecs, Player, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Walking, EcsActor, EcsPlayer); + ECS_TYPE(ecs, Builder, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Flying, EcsActor, EcsBuilder); + + ECS_SET_COMPONENT(Input); + ECS_SET_ENTITY(EcsActor); + ECS_SET_ENTITY(EcsPlayer); + ECS_SET_ENTITY(EcsBuilder); + ECS_SET_TYPE(Builder); + ECS_SET_TYPE(Player); +} diff --git a/code/apps/server/source/components/general.c b/code/apps/server/source/components/general.c new file mode 100644 index 0000000..84c4367 --- /dev/null +++ b/code/apps/server/source/components/general.c @@ -0,0 +1,19 @@ +#include "components/general.h" + +void GeneralImport(ecs_world_t *ecs) { + ECS_MODULE(ecs, General); + ecs_set_name_prefix(ecs, "General"); + + ECS_COMPONENT(ecs, Chunk); + ECS_COMPONENT(ecs, Position); + + ECS_IMPORT(ecs, FlecsMeta); + + ECS_META(ecs, Vector2D); + ECS_META(ecs, Drawable); + + ECS_SET_COMPONENT(Chunk); + ECS_SET_COMPONENT(Vector2D); + ECS_SET_COMPONENT(Position); + ECS_SET_COMPONENT(Drawable); +} diff --git a/code/apps/server/source/components/net.c b/code/apps/server/source/components/net.c new file mode 100644 index 0000000..b9f5829 --- /dev/null +++ b/code/apps/server/source/components/net.c @@ -0,0 +1,16 @@ +#include "components/net.h" + +void NetImport(ecs_world_t *ecs) { + ECS_MODULE(ecs, Net); + + ecs_set_name_prefix(ecs, "Net"); + + ECS_TAG(ecs, EcsClient); + + ECS_IMPORT(ecs, FlecsMeta); + + ECS_META(ecs, ClientInfo); + + ECS_EXPORT_ENTITY(EcsClient); + ECS_EXPORT_COMPONENT(ClientInfo); +} diff --git a/code/apps/server/source/components/physics.c b/code/apps/server/source/components/physics.c new file mode 100644 index 0000000..b6528d9 --- /dev/null +++ b/code/apps/server/source/components/physics.c @@ -0,0 +1,17 @@ +#include "components/physics.h" + +void PhysicsImport(ecs_world_t *ecs) { + ECS_MODULE(ecs, Physics); + ecs_set_name_prefix(ecs, "Physics"); + + ECS_TAG(ecs, Walking); + ECS_TAG(ecs, Flying); + ECS_TYPE(ecs, Movement, Walking, Flying); + + ECS_COMPONENT(ecs, Velocity); + + ECS_SET_TYPE(Movement); + ECS_SET_ENTITY(Walking); + ECS_SET_ENTITY(Flying); + ECS_SET_COMPONENT(Velocity); +} diff --git a/code/apps/server/source/network.c b/code/apps/server/source/network.c index 9ebdbd6..2e681bb 100644 --- a/code/apps/server/source/network.c +++ b/code/apps/server/source/network.c @@ -15,6 +15,8 @@ #include "components/controllers.h" #include "components/net.h" +#include "assets.h" + #define NETWORK_UPDATE_DELAY 0.100 #define NETWORK_MAX_CLIENTS 32 diff --git a/code/vendors/flecs/CMakeLists.txt b/code/vendors/flecs/CMakeLists.txt index 3cb9024..5540a42 100644 --- a/code/vendors/flecs/CMakeLists.txt +++ b/code/vendors/flecs/CMakeLists.txt @@ -1,3 +1,2 @@ file(GLOB SRCS *.c *.h) add_library(flecs-bundle STATIC ${SRCS}) -include_directories(flecs-bundle .)