From 0f7179cb622888d86a17dd74ab038fa937a3e27d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Mon, 18 Jan 2021 13:16:38 +0100 Subject: [PATCH] code: structure out flecs code --- code/apps/server/CMakeLists.txt | 5 +- .../server/header/components/controllers.h | 51 +++++++++++++++++++ code/apps/server/header/components/general.h | 30 ++++++----- code/apps/server/header/components/net.h | 15 ++++-- code/apps/server/header/components/physics.h | 34 +++++++++++++ code/apps/server/header/network.h | 4 +- code/apps/server/source/network.c | 19 ++++--- code/apps/server/source/world/world.c | 2 +- 8 files changed, 129 insertions(+), 31 deletions(-) create mode 100644 code/apps/server/header/components/controllers.h create mode 100644 code/apps/server/header/components/physics.h diff --git a/code/apps/server/CMakeLists.txt b/code/apps/server/CMakeLists.txt index cb75178..07cc10c 100644 --- a/code/apps/server/CMakeLists.txt +++ b/code/apps/server/CMakeLists.txt @@ -13,8 +13,11 @@ add_executable(eco2d-server header/world/world.h header/world/blocks.h header/world/blocks_info.h - header/components/general.h + header/components/net.h + header/components/physics.h + header/components/general.h + header/components/controllers.h ../../vendors/cwpack/cwpack.c ../../vendors/cwpack/cwpack.h diff --git a/code/apps/server/header/components/controllers.h b/code/apps/server/header/components/controllers.h new file mode 100644 index 0000000..bf148f9 --- /dev/null +++ b/code/apps/server/header/components/controllers.h @@ -0,0 +1,51 @@ +#pragma once +#include "flecs/flecs.h" +#include "flecs/flecs_meta.h" + +#include "components/general.h" +#include "components/physics.h" + +ECS_STRUCT(Input, { + double x; + double y; + uint8_t use; +}); + +typedef struct { + ECS_DECLARE_COMPONENT(Input); + ECS_DECLARE_ENTITY(EcsPlayer); + ECS_DECLARE_ENTITY(EcsBuilder); + ECS_DECLARE_TYPE(Player); + ECS_DECLARE_TYPE(Builder); +} Controllers; + +#define ControllersImportHandles(handles)\ + ECS_IMPORT_COMPONENT(handles, Input);\ + ECS_IMPORT_TYPE(handles, Player);\ + ECS_IMPORT_TYPE(handles, Builder);\ + 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, EcsPlayer); + ECS_TAG(ecs, EcsBuilder); + + ECS_PREFAB(ecs, Base, general.Position, physics.Velocity, Input); + ECS_TYPE(ecs, Player, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Walking, EcsPlayer); + ECS_TYPE(ecs, Builder, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Flying, EcsBuilder); + + ECS_SET_COMPONENT(Input); + ECS_SET_ENTITY(EcsPlayer); + ECS_SET_ENTITY(EcsBuilder); + ECS_SET_TYPE(Builder); + ECS_SET_TYPE(Player); +} diff --git a/code/apps/server/header/components/general.h b/code/apps/server/header/components/general.h index d400224..ea154c4 100644 --- a/code/apps/server/header/components/general.h +++ b/code/apps/server/header/components/general.h @@ -2,35 +2,37 @@ #include "flecs/flecs.h" #include "flecs/flecs_meta.h" -ECS_STRUCT(Chunk, { +ECS_STRUCT(Vector2D, { int16_t x; int16_t y; }); -ECS_STRUCT(Position, { - int16_t x; - int16_t y; -}); +typedef Vector2D Chunk; +typedef Vector2D Position; typedef struct { ECS_DECLARE_COMPONENT(Chunk); ECS_DECLARE_COMPONENT(Position); -} Common; + ECS_DECLARE_COMPONENT(Vector2D); +} General; -#define CommonImportHandles(handles)\ +#define GeneralImportHandles(handles)\ ECS_IMPORT_COMPONENT(handles, Chunk);\ + ECS_IMPORT_COMPONENT(handles, Vector2D);\ ECS_IMPORT_COMPONENT(handles, Position); -static inline void CommonImport(ecs_world_t *ecs) { - ECS_MODULE(ecs, Common); +static inline void GeneralImport(ecs_world_t *ecs) { + ECS_MODULE(ecs, General); + ecs_set_name_prefix(ecs, "General"); - ecs_set_name_prefix(ecs, "Common"); + ECS_COMPONENT(ecs, Chunk); + ECS_COMPONENT(ecs, Position); ECS_IMPORT(ecs, FlecsMeta); - ECS_META(ecs, Chunk); - ECS_META(ecs, Position); + ECS_META(ecs, Vector2D); - ECS_EXPORT_COMPONENT(Chunk); - ECS_EXPORT_COMPONENT(Position); + ECS_SET_COMPONENT(Chunk); + ECS_SET_COMPONENT(Vector2D); + ECS_SET_COMPONENT(Position); } diff --git a/code/apps/server/header/components/net.h b/code/apps/server/header/components/net.h index e2b4428..402fbc4 100644 --- a/code/apps/server/header/components/net.h +++ b/code/apps/server/header/components/net.h @@ -2,25 +2,30 @@ #include "flecs/flecs.h" #include "flecs/flecs_meta.h" -ECS_STRUCT(NetClient, { +ECS_STRUCT(ClientInfo, { uint16_t peer_id; }); typedef struct { - ECS_DECLARE_COMPONENT(NetClient); + ECS_DECLARE_ENTITY(EcsClient); + ECS_DECLARE_COMPONENT(ClientInfo); } Net; #define NetImportHandles(handles)\ - ECS_IMPORT_COMPONENT(handles, NetClient);\ + 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, NetClient); + ECS_META(ecs, ClientInfo); - ECS_EXPORT_COMPONENT(NetClient); + ECS_EXPORT_ENTITY(EcsClient); + ECS_EXPORT_COMPONENT(ClientInfo); } diff --git a/code/apps/server/header/components/physics.h b/code/apps/server/header/components/physics.h new file mode 100644 index 0000000..be43da4 --- /dev/null +++ b/code/apps/server/header/components/physics.h @@ -0,0 +1,34 @@ +#pragma once +#include "flecs/flecs.h" +#include "components/general.h" + +typedef Vector2D Velocity; + +typedef struct { + ECS_DECLARE_TYPE(Movement); + ECS_DECLARE_ENTITY(Walking); + ECS_DECLARE_ENTITY(Flying); + ECS_DECLARE_COMPONENT(Velocity); +} Physics; + +#define PhysicsImportHandles(handles)\ + ECS_IMPORT_TYPE(handles, Movement);\ + ECS_IMPORT_ENTITY(handles, Walking);\ + 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); +} diff --git a/code/apps/server/header/network.h b/code/apps/server/header/network.h index 29e29a2..b3ef261 100644 --- a/code/apps/server/header/network.h +++ b/code/apps/server/header/network.h @@ -8,5 +8,5 @@ int32_t network_server_stop(void); int32_t network_server_tick(void); void network_server_update(void *data); -uint64_t network_player_create(uint16_t peer_id); -void network_player_destroy(uint64_t ent_id); +uint64_t network_client_create(uint16_t peer_id); +void network_client_destroy(uint64_t ent_id); diff --git a/code/apps/server/source/network.c b/code/apps/server/source/network.c index 68911c1..80b1899 100644 --- a/code/apps/server/source/network.c +++ b/code/apps/server/source/network.c @@ -12,6 +12,7 @@ #include "world/world.h" #include "components/general.h" +#include "components/controllers.h" #include "components/net.h" #define NETWORK_UPDATE_DELAY 0.100 @@ -67,14 +68,15 @@ int32_t network_server_tick(void) { case ENET_EVENT_TYPE_CONNECT: { zpl_printf("[INFO] A new user %d connected.\n", event.peer->incomingPeerID); uint16_t peer_id = event.peer->incomingPeerID; - uint64_t ent_id = network_player_create(peer_id); + uint64_t ent_id = network_client_create(peer_id); + // TODO: Make sure ent_id does not get truncated with large entity numbers. event.peer->data = (void*)((uint32_t)ent_id); } break; case ENET_EVENT_TYPE_DISCONNECT: case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: { zpl_printf("[INFO] A user %d disconnected.\n", event.peer->incomingPeerID); ecs_entity_t e = (ecs_entity_t)((uint32_t)event.peer->data); - network_player_destroy(e); + network_client_destroy(e); } break; case ENET_EVENT_TYPE_RECEIVE: { @@ -126,13 +128,14 @@ void network_server_update(void *data) { // } } -uint64_t network_player_create(uint16_t peer_id) { - ECS_IMPORT(world_ecs(), Common); +uint64_t network_client_create(uint16_t peer_id) { + ECS_IMPORT(world_ecs(), General); + ECS_IMPORT(world_ecs(), Controllers); ECS_IMPORT(world_ecs(), Net); - ecs_entity_t e = ecs_new(world_ecs(), 0); - ecs_set(world_ecs(), e, Position, {0, 0}); - ecs_set(world_ecs(), e, NetClient, {peer_id}); + ecs_entity_t e = ecs_new(world_ecs(), Player); + ecs_add(world_ecs(), e, EcsClient); + ecs_set(world_ecs(), e, ClientInfo, {peer_id}); librg_entity_track(world_tracker(), e); librg_entity_owner_set(world_tracker(), e, peer_id); @@ -142,7 +145,7 @@ uint64_t network_player_create(uint16_t peer_id) { return (uint64_t)e; } -void network_player_destroy(uint64_t ent_id) { +void network_client_destroy(uint64_t ent_id) { librg_entity_untrack(world_tracker(), ent_id); ecs_delete(world_ecs(), ent_id); } diff --git a/code/apps/server/source/world/world.c b/code/apps/server/source/world/world.c index 5d3964b..b7c67c6 100644 --- a/code/apps/server/source/world/world.c +++ b/code/apps/server/source/world/world.c @@ -84,7 +84,7 @@ int32_t world_init(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint1 librg_config_chunkamount_set(world.tracker, world_size, world_size, 1); librg_config_chunkoffset_set(world.tracker, LIBRG_OFFSET_MID, LIBRG_OFFSET_MID, LIBRG_OFFSET_MID); - ECS_IMPORT(world.ecs, Common); + ECS_IMPORT(world.ecs, General); for (int i = 0; i < chunk_size * chunk_size; ++i) { ecs_entity_t e = ecs_new(world.ecs, 0);