world: access all components globally!

isolation_bkp/dynres
Dominik Madarász 2021-08-09 17:18:34 +02:00
parent 55ad205441
commit 626457e1b9
9 changed files with 80 additions and 74 deletions

View File

@ -12,10 +12,10 @@
uint64_t entity_spawn(uint16_t class_id) {
ecs_entity_t e = ecs_new(world_ecs(), 0);
w_ecs_set(e, Velocity, {0});
w_ecs_set(e, Classify, { .id = class_id });
w_ecs_add(e, Walking);
Position *pos = w_ecs_get_mut(e, Position, NULL);
ecs_set(world_ecs(), e, Velocity, {0});
ecs_set(world_ecs(), e, Classify, { .id = class_id });
ecs_add(world_ecs(), e, Walking);
Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL);
#if 1
pos->x=rand() % world_dim();
pos->y=rand() % world_dim();

View File

@ -33,7 +33,7 @@ int32_t pkt_00_init_handler(pkt_header *header) {
uint64_t peer_id = (uint64_t)header->udata;
uint64_t ent_id = player_spawn(NULL);
w_ecs_set(ent_id, ClientInfo, {.peer = ent_id, .view_id = header->view_id });
ecs_set(world_ecs(), ent_id, ClientInfo, {.peer = ent_id, .view_id = header->view_id });
pkt_01_welcome_send(peer_id, header->view_id, ent_id, world_chunk_size(), world_chunk_amount());
return 0;
}

View File

@ -33,7 +33,7 @@ int32_t pkt_send_keystate_handler(pkt_header *header) {
PKT_IF(pkt_msg_decode(header, pkt_send_keystate_desc, pkt_pack_desc_args(pkt_send_keystate_desc), PKT_STRUCT_PTR(&table)));
ecs_entity_t e = PKT_GET_ENT(header);
Input *i = w_ecs_get_mut(e, Input, NULL);
Input *i = ecs_get_mut(world_ecs(), e, Input, NULL);
if (i && !i->is_blocked) {
i->x = table.x;
i->y = table.y;

View File

@ -20,11 +20,11 @@ uint64_t player_spawn(char *name) {
}
ecs_set(world_ecs(), e, EcsName, {.alloc_value = name });
w_ecs_add(e, EcsClient);
w_ecs_set(e, ClientInfo, {0});
w_ecs_set(e, Input, {0});
w_ecs_set(e, Health, {.hp = PLAYER_MAX_HP, .max_hp = PLAYER_MAX_HP});
w_ecs_add(e, Player);
ecs_add(world_ecs(), e, EcsClient);
ecs_set(world_ecs(), e, ClientInfo, {0});
ecs_set(world_ecs(), e, Input, {0});
ecs_set(world_ecs(), e, Health, {.hp = PLAYER_MAX_HP, .max_hp = PLAYER_MAX_HP});
ecs_add(world_ecs(), e, Player);
librg_entity_owner_set(world_tracker(), e, (int64_t)e);

View File

@ -8,7 +8,7 @@
uint64_t vehicle_spawn(void) {
ecs_entity_t e = entity_spawn(EKIND_VEHICLE);
w_ecs_add(e, Vehicle);
ecs_add(world_ecs(), e, Vehicle);
return (uint64_t)e;
}

View File

@ -14,7 +14,6 @@ ZPL_TABLE(static, world_snapshot, world_snapshot_, entity_view);
static world_data world = {0};
static world_snapshot streamer_snapshot;
static ecs_entity_t components_handle;
entity_view world_build_entity_view(int64_t e) {
entity_view *cached_ev = world_snapshot_get(&streamer_snapshot, e);
@ -22,32 +21,32 @@ entity_view world_build_entity_view(int64_t e) {
entity_view view = {0};
const Classify *classify = w_ecs_get(e, Classify);
const Classify *classify = ecs_get(world_ecs(), e, Classify);
assert(classify);
view.kind = classify->id;
const Position *pos = w_ecs_get(e, Position);
const Position *pos = ecs_get(world_ecs(), e, Position);
if (pos) {
view.x = pos->x;
view.y = pos->y;
}
const Velocity *vel = w_ecs_get(e, Velocity);
const Velocity *vel = ecs_get(world_ecs(), e, Velocity);
if (vel) {
view.flag |= EFLAG_INTERP;
view.vx = vel->x;
view.vy = vel->y;
}
const Health *health = w_ecs_get(e, Health);
const Health *health = ecs_get(world_ecs(), e, Health);
if (health) {
view.hp = health->hp;
view.max_hp = health->max_hp;
}
if (w_ecs_get(e, Chunk)) {
Chunk *chpos = w_ecs_get_mut(e, Chunk, 0);
if (ecs_get(world_ecs(), e, Chunk)) {
Chunk *chpos = ecs_get_mut(world_ecs(), e, Chunk, 0);
view.x = chpos->x;
view.y = chpos->y;
view.blocks_used = 1;
@ -154,7 +153,6 @@ 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));
components_handle = ecs_typeid(Components);
world_snapshot_init(&streamer_snapshot, zpl_heap());
int32_t world_build_status = worldgen_test(&world);
@ -283,11 +281,6 @@ ecs_world_t * world_ecs() {
return world.ecs;
}
Components const *world_components(void) {
ecs_entity_t ecs_typeid(Components) = components_handle;
return ecs_get(world.ecs, ecs_typeid(Components), Components);
}
librg_world *world_tracker() {
return world.tracker;
}
@ -377,14 +370,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 = w_ecs_get_mut(e, Chunk, &was_added);
Chunk *chunk = ecs_get_mut(world_ecs(), e, 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 = w_ecs_get_mut(e, Chunk, &was_added);
Chunk *chunk = ecs_get_mut(world_ecs(), e, Chunk, &was_added);
assert(!was_added);
if (chunk) return chunk->is_dirty;
return false;

View File

@ -54,7 +54,6 @@ 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);
@ -78,21 +77,3 @@ void world_chunk_replace_block(int64_t id, uint16_t block_idx, uint8_t block_id)
uint8_t *world_chunk_get_blocks(int64_t id);
void world_chunk_mark_dirty(ecs_entity_t e);
uint8_t world_chunk_is_dirty(ecs_entity_t e);
// NOTE(zaklaus): flecs wrappers for easier world access
#define w_ecs_set(entity, component, ...)\
ecs_set_ptr_w_entity(world_ecs(), entity, world_components()->ecs_typeid(component), sizeof(component), &(component)__VA_ARGS__)
#define w_ecs_add(entity, component)\
ecs_add_type(world_ecs(), entity, world_components()->ecs_type(component))
#define w_ecs_get(entity, component)\
((const component*)ecs_get_w_entity(world_ecs(), entity, world_components()->ecs_typeid(component)))
#define w_ecs_get_ref(ref, entity, component)\
((const component*)ecs_get_ref_w_entity(world_ecs(), ref, entity, world_components()->ecs_typeid(component)))
#define w_ecs_get_mut(entity, component, is_added)\
((component*)ecs_get_mut_w_entity(world_ecs(), entity, world_components()->ecs_typeid(component), is_added))

View File

@ -1,33 +1,55 @@
#include "modules/components.h"
ECS_COMPONENT_DECLARE(Chunk);
ECS_COMPONENT_DECLARE(Position);
ECS_COMPONENT_DECLARE(Vector2D);
ECS_COMPONENT_DECLARE(Drawable);
ECS_COMPONENT_DECLARE(Input);
ECS_COMPONENT_DECLARE(Velocity);
ECS_COMPONENT_DECLARE(ClientInfo);
ECS_COMPONENT_DECLARE(Health);
ECS_COMPONENT_DECLARE(Classify);
ECS_COMPONENT_DECLARE(Vehicle);
ECS_TAG_DECLARE(EcsActor);
ECS_TAG_DECLARE(EcsDemoNPC);
ECS_TYPE_DECLARE(Player);
ECS_TYPE_DECLARE(Movement);
ECS_TYPE_DECLARE(Walking);
ECS_TYPE_DECLARE(Flying);
ECS_TYPE_DECLARE(EcsClient);
// NOTE(zaklaus): custom macro to define meta components outside the current scope
#ifndef ECS_META_DEFINE
#define ECS_META_DEFINE(world, T)\
ECS_COMPONENT_DEFINE(world, T);\
ecs_new_meta(world, ecs_entity(T), &__##T##__);
#endif
void ComponentsImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Components);
ECS_IMPORT(ecs, FlecsMeta);
ECS_META(ecs, Position);
ECS_META(ecs, Chunk);
ECS_META(ecs, Vector2D);
ECS_META(ecs, Drawable);
ECS_META(ecs, ClientInfo);
ECS_META(ecs, Velocity);
ECS_META(ecs, Input);
ECS_META(ecs, Health);
ECS_META(ecs, Classify);
ECS_META(ecs, Vehicle);
ECS_META_DEFINE(ecs, Position);
ECS_META_DEFINE(ecs, Chunk);
ECS_META_DEFINE(ecs, Vector2D);
ECS_META_DEFINE(ecs, Drawable);
ECS_META_DEFINE(ecs, ClientInfo);
ECS_META_DEFINE(ecs, Velocity);
ECS_META_DEFINE(ecs, Input);
ECS_META_DEFINE(ecs, Health);
ECS_META_DEFINE(ecs, Classify);
ECS_META_DEFINE(ecs, Vehicle);
ECS_TAG(ecs, Walking);
ECS_TAG(ecs, Flying);
ECS_TAG(ecs, EcsClient);
ECS_TAG_DEFINE(ecs, Walking);
ECS_TAG_DEFINE(ecs, Flying);
ECS_TAG_DEFINE(ecs, EcsClient);
ECS_TAG(ecs, EcsActor);
ECS_TAG(ecs, EcsPlayer);
ECS_TAG(ecs, EcsBuilder);
ECS_TAG(ecs, EcsDemoNPC);
ECS_TAG_DEFINE(ecs, EcsActor);
ECS_TAG_DEFINE(ecs, EcsDemoNPC);
ECS_PREFAB(ecs, Base, Position, Velocity, Input, EcsActor);
ECS_TYPE(ecs, Movement, Walking, Flying);
ECS_TYPE(ecs, Player, INSTANCEOF | Base, SWITCH | Movement, CASE | Walking, EcsActor, EcsPlayer);
ECS_TYPE(ecs, Builder, INSTANCEOF | Base, SWITCH | Movement, CASE | Flying, EcsActor, EcsBuilder);
ECS_TYPE_DEFINE(ecs, Movement, Walking, Flying);
ECS_SET_COMPONENT(Chunk);
ECS_SET_COMPONENT(Vector2D);
@ -43,10 +65,6 @@ void ComponentsImport(ecs_world_t *ecs) {
ECS_SET_ENTITY(Walking);
ECS_SET_ENTITY(Flying);
ECS_SET_ENTITY(EcsActor);
ECS_SET_ENTITY(EcsPlayer);
ECS_SET_ENTITY(EcsBuilder);
ECS_SET_ENTITY(EcsDemoNPC);
ECS_SET_TYPE(Movement);
ECS_SET_TYPE(Builder);
ECS_SET_TYPE(Player);
}

View File

@ -51,6 +51,24 @@ ECS_STRUCT(Vehicle, {
uint64_t seats[4];
});
ECS_COMPONENT_EXTERN(Chunk);
ECS_COMPONENT_EXTERN(Position);
ECS_COMPONENT_EXTERN(Vector2D);
ECS_COMPONENT_EXTERN(Drawable);
ECS_COMPONENT_EXTERN(Input);
ECS_COMPONENT_EXTERN(Velocity);
ECS_COMPONENT_EXTERN(ClientInfo);
ECS_COMPONENT_EXTERN(Health);
ECS_COMPONENT_EXTERN(Classify);
ECS_COMPONENT_EXTERN(Vehicle);
ECS_TAG_EXTERN(EcsActor);
ECS_TAG_EXTERN(EcsDemoNPC);
ECS_TYPE_EXTERN(Player);
ECS_TYPE_EXTERN(Movement);
ECS_TYPE_EXTERN(Walking);
ECS_TYPE_EXTERN(Flying);
ECS_TYPE_EXTERN(EcsClient);
typedef struct {
ECS_DECLARE_COMPONENT(Chunk);
ECS_DECLARE_COMPONENT(Position);
@ -63,8 +81,6 @@ typedef struct {
ECS_DECLARE_COMPONENT(Classify);
ECS_DECLARE_COMPONENT(Vehicle);
ECS_DECLARE_ENTITY(EcsActor);
ECS_DECLARE_ENTITY(EcsPlayer);
ECS_DECLARE_ENTITY(EcsBuilder);
ECS_DECLARE_ENTITY(EcsDemoNPC);
ECS_DECLARE_TYPE(Player);
ECS_DECLARE_TYPE(Builder);
@ -89,8 +105,6 @@ ECS_IMPORT_TYPE(handles, Player);\
ECS_IMPORT_TYPE(handles, Builder);\
ECS_IMPORT_TYPE(handles, Movement);\
ECS_IMPORT_ENTITY(handles, EcsActor);\
ECS_IMPORT_ENTITY(handles, EcsPlayer);\
ECS_IMPORT_ENTITY(handles, EcsBuilder);\
ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\
ECS_IMPORT_ENTITY(handles, Walking);\
ECS_IMPORT_ENTITY(handles, Flying);\