From a43360161c9346ce24fd84046f0f8250d56dfd63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Tue, 27 Jul 2021 15:36:24 +0200 Subject: [PATCH] ecs: simplify access to components --- code/game/header/world/world.h | 3 ++- code/game/source/world/world.c | 11 ++++++++--- code/modules/modules/components.h | 1 - 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/code/game/header/world/world.h b/code/game/header/world/world.h index 2e060c0..8bbafa2 100644 --- a/code/game/header/world/world.h +++ b/code/game/header/world/world.h @@ -3,6 +3,7 @@ #include "librg.h" #include "packet.h" #include "flecs/flecs.h" +#include "modules/components.h" #define WORLD_ERROR_NONE +0x0000 #define WORLD_ERROR_OUTOFMEM -0x0001 @@ -36,7 +37,6 @@ typedef struct { uint8_t active_layer_id; ecs_world_t *ecs; ecs_query_t *ecs_update; - ecs_entity_t chunk_handle; ecs_entity_t *chunk_mapping; librg_world *tracker; world_pkt_reader_proc *reader_proc; @@ -53,6 +53,7 @@ int32_t world_write(pkt_header *pkt, void *udata); uint32_t world_buf(uint8_t const **ptr, uint32_t *width); ecs_world_t * world_ecs(void); +Components const* world_components(void); librg_world * world_tracker(void); uint16_t world_chunk_size(void); diff --git a/code/game/source/world/world.c b/code/game/source/world/world.c index 2d93d28..ce72968 100644 --- a/code/game/source/world/world.c +++ b/code/game/source/world/world.c @@ -11,6 +11,7 @@ #include "packets/pkt_send_librg_update.h" static world_data world = {0}; +static Components const *ecs_components; entity_view world_build_entity_view(int64_t e) { ECS_IMPORT(world_ecs(), Components); @@ -141,7 +142,7 @@ int32_t world_init(int32_t seed, uint16_t chunk_size, uint16_t chunk_amount) { world.ecs_update = ecs_query_new(world.ecs, "components.ClientInfo, components.Position"); world.chunk_mapping = zpl_malloc(sizeof(ecs_entity_t)*zpl_square(chunk_amount)); world.block_mapping = zpl_malloc(sizeof(uint8_t*)*zpl_square(chunk_amount)); - world.chunk_handle = ecs_typeid(Chunk); + ecs_components = ecs_get(world.ecs, ecs_typeid(Components), Components); int32_t world_build_status = worldgen_test(&world); ZPL_ASSERT(world_build_status >= 0); @@ -264,6 +265,10 @@ ecs_world_t * world_ecs() { return world.ecs; } +Components const* world_components(void) { + return ecs_components; +} + librg_world * world_tracker() { return world.tracker; } @@ -353,14 +358,14 @@ uint8_t *world_chunk_get_blocks(int64_t id) { void world_chunk_mark_dirty(ecs_entity_t e) { bool was_added=false; - Chunk *chunk = (Chunk *)ecs_get_mut_w_entity(world.ecs, e, world.chunk_handle, &was_added); + Chunk *chunk = (Chunk *)ecs_get_mut_w_entity(world.ecs, e, ecs_components->ecs_typeid(Chunk), &was_added); assert(!was_added); if (chunk) chunk->is_dirty = true; } uint8_t world_chunk_is_dirty(ecs_entity_t e) { bool was_added=false; - Chunk *chunk = (Chunk *)ecs_get_mut_w_entity(world.ecs, e, world.chunk_handle, &was_added); + Chunk *chunk = (Chunk *)ecs_get_mut_w_entity(world.ecs, e, ecs_components->ecs_typeid(Chunk), &was_added); assert(!was_added); if (chunk) return chunk->is_dirty; return false; diff --git a/code/modules/modules/components.h b/code/modules/modules/components.h index 1158d46..203f31e 100644 --- a/code/modules/modules/components.h +++ b/code/modules/modules/components.h @@ -1,7 +1,6 @@ #pragma once #include "flecs/flecs.h" #include "flecs/flecs_meta.h" -#include "world/world.h" ECS_STRUCT(Vector2D, { float x;