ecs: simplify access to components

isolation_bkp/dynres
Dominik Madarász 2021-07-27 15:36:24 +02:00
parent 6b2f049704
commit a43360161c
3 changed files with 10 additions and 5 deletions

View File

@ -3,6 +3,7 @@
#include "librg.h" #include "librg.h"
#include "packet.h" #include "packet.h"
#include "flecs/flecs.h" #include "flecs/flecs.h"
#include "modules/components.h"
#define WORLD_ERROR_NONE +0x0000 #define WORLD_ERROR_NONE +0x0000
#define WORLD_ERROR_OUTOFMEM -0x0001 #define WORLD_ERROR_OUTOFMEM -0x0001
@ -36,7 +37,6 @@ typedef struct {
uint8_t active_layer_id; uint8_t active_layer_id;
ecs_world_t *ecs; ecs_world_t *ecs;
ecs_query_t *ecs_update; ecs_query_t *ecs_update;
ecs_entity_t chunk_handle;
ecs_entity_t *chunk_mapping; ecs_entity_t *chunk_mapping;
librg_world *tracker; librg_world *tracker;
world_pkt_reader_proc *reader_proc; 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); uint32_t world_buf(uint8_t const **ptr, uint32_t *width);
ecs_world_t * world_ecs(void); ecs_world_t * world_ecs(void);
Components const* world_components(void);
librg_world * world_tracker(void); librg_world * world_tracker(void);
uint16_t world_chunk_size(void); uint16_t world_chunk_size(void);

View File

@ -11,6 +11,7 @@
#include "packets/pkt_send_librg_update.h" #include "packets/pkt_send_librg_update.h"
static world_data world = {0}; static world_data world = {0};
static Components const *ecs_components;
entity_view world_build_entity_view(int64_t e) { entity_view world_build_entity_view(int64_t e) {
ECS_IMPORT(world_ecs(), Components); 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.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.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.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); int32_t world_build_status = worldgen_test(&world);
ZPL_ASSERT(world_build_status >= 0); ZPL_ASSERT(world_build_status >= 0);
@ -264,6 +265,10 @@ ecs_world_t * world_ecs() {
return world.ecs; return world.ecs;
} }
Components const* world_components(void) {
return ecs_components;
}
librg_world * world_tracker() { librg_world * world_tracker() {
return 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) { void world_chunk_mark_dirty(ecs_entity_t e) {
bool was_added=false; 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); assert(!was_added);
if (chunk) chunk->is_dirty = true; if (chunk) chunk->is_dirty = true;
} }
uint8_t world_chunk_is_dirty(ecs_entity_t e) { uint8_t world_chunk_is_dirty(ecs_entity_t e) {
bool was_added=false; 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); assert(!was_added);
if (chunk) return chunk->is_dirty; if (chunk) return chunk->is_dirty;
return false; return false;

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "flecs/flecs.h" #include "flecs/flecs.h"
#include "flecs/flecs_meta.h" #include "flecs/flecs_meta.h"
#include "world/world.h"
ECS_STRUCT(Vector2D, { ECS_STRUCT(Vector2D, {
float x; float x;