From 04e257ec611d5b662cf44b39480c24f93368ce23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Sat, 13 Aug 2022 08:43:15 +0200 Subject: [PATCH] introduce entity spawndefs --- code/game/src/entity.c | 14 ++++++++++++++ code/game/src/entity.h | 2 ++ code/game/src/entity_spawnlist.c | 11 +++++++++++ code/game/src/gui/build_mode.c | 7 +++++++ code/game/src/items.c | 2 +- code/game/src/items.h | 2 +- code/game/src/items_list.c | 9 +++------ code/game/src/world/world.c | 2 ++ 8 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 code/game/src/entity_spawnlist.c diff --git a/code/game/src/entity.c b/code/game/src/entity.c index d35741d..cac3cb7 100644 --- a/code/game/src/entity.c +++ b/code/game/src/entity.c @@ -45,6 +45,20 @@ void entity_despawn(uint64_t ent_id) { ecs_delete(world_ecs(), ent_id); } +// NOTE(zaklaus): bring in entity spawnlist +#include "entity_spawnlist.c" + +uint64_t entity_spawn_id(uint16_t id){ + for (size_t i = 0; i < MAX_ENTITY_SPAWNDEFS; ++i){ + if (entity_spawnlist[i].id == id){ + ZPL_ASSERT(entity_spawnlist[i].proc); + return entity_spawnlist[i].proc(); + } + } + + return 0; +} + void entity_set_position(uint64_t ent_id, float x, float y) { Position *p = ecs_get_mut(world_ecs(), ent_id, Position); p->x = x; diff --git a/code/game/src/entity.h b/code/game/src/entity.h index d03a915..c0f4792 100644 --- a/code/game/src/entity.h +++ b/code/game/src/entity.h @@ -8,6 +8,8 @@ void entity_batch_despawn(uint64_t *ids, size_t num_ids); void entity_despawn(uint64_t ent_id); void entity_set_position(uint64_t ent_id, float x, float y); +uint64_t entity_spawn_id(uint16_t id); + // NOTE(zaklaus): action-based entity stream throttling void entity_wake(uint64_t ent_id); void entity_update_action_timers(); diff --git a/code/game/src/entity_spawnlist.c b/code/game/src/entity_spawnlist.c new file mode 100644 index 0000000..3b54b1f --- /dev/null +++ b/code/game/src/entity_spawnlist.c @@ -0,0 +1,11 @@ +// NOTE(zaklaus): access to spawners +#include "storage.h" + +struct { + asset_id id; + uint64_t (*proc)(); +} entity_spawnlist[] = { + { .id = ASSET_CHEST, .proc = storage_spawn } +}; + +#define MAX_ENTITY_SPAWNDEFS ((sizeof(entity_spawnlist))/(sizeof(entity_spawnlist[0]))) diff --git a/code/game/src/gui/build_mode.c b/code/game/src/gui/build_mode.c index 4fc7633..61a9cfa 100644 --- a/code/game/src/gui/build_mode.c +++ b/code/game/src/gui/build_mode.c @@ -67,6 +67,11 @@ void buildmode_draw(void) { qty = item->quantity; } + world_block_lookup l = world_block_from_realpos((float)cam.x, (float)cam.y); + if (build_is_deletion_mode && !l.is_outer){ + goto build_skip_placements; + } + if (build_is_in_draw_mode) { for (size_t i = 0; i < BUILD_MAX_PLACEMENTS; i++) { item_placement *it = &build_placements[i]; @@ -96,9 +101,11 @@ void buildmode_draw(void) { } } + if (!is_outside_range) renderer_draw_single((float)cam.x, (float)cam.y, ASSET_BUILDMODE_HIGHLIGHT, ColorAlpha(build_is_deletion_mode ? RED : WHITE, 0.2f)); + build_skip_placements: build_num_placements = zpl_min(build_num_placements, qty); } } diff --git a/code/game/src/items.c b/code/game/src/items.c index d880992..92526e6 100644 --- a/code/game/src/items.c +++ b/code/game/src/items.c @@ -84,7 +84,7 @@ void item_use(ecs_world_t *ecs, ItemDrop *it, Position p, uint64_t udata) { return; } - ecs_entity_t e = desc->place_item.spawn_proc(); + ecs_entity_t e = entity_spawn_id(desc->place_item.id); ZPL_ASSERT(world_entity_valid(e)); Position *pos = ecs_get_mut(ecs, e, Position); pos->x = p.x; diff --git a/code/game/src/items.h b/code/game/src/items.h index ef1a131..a5a13ea 100644 --- a/code/game/src/items.h +++ b/code/game/src/items.h @@ -34,7 +34,7 @@ typedef struct { } proxy; struct { - uint64_t (*spawn_proc)(); + asset_id id; } place_item; }; } item_desc; diff --git a/code/game/src/items_list.c b/code/game/src/items_list.c index f2efcc4..4a137b1 100644 --- a/code/game/src/items_list.c +++ b/code/game/src/items_list.c @@ -38,22 +38,19 @@ }\ } -#define ITEM_ENT(asset, qty, proc)\ +#define ITEM_ENT(asset, qty, eid)\ {\ .kind = asset,\ .usage = UKIND_PLACE_ITEM,\ .max_quantity = qty,\ .place_item = {\ -.spawn_proc = proc\ +.id = eid\ }\ } #define ITEM_SELF(asset, qty) ITEM_BLOCK(asset, qty, asset) #define ITEM_SELF_DIR(asset, qty) ITEM_BLOCK_DIR(asset, qty, asset) -// NOTE(zaklaus): access to spawners -#include "storage.h" - static item_desc items[] = { { .kind = 0, @@ -70,5 +67,5 @@ static item_desc items[] = { ITEM_PROXY(ASSET_BELT_UP, ASSET_BELT), ITEM_PROXY(ASSET_BELT_DOWN, ASSET_BELT), - ITEM_ENT(ASSET_CHEST, 32, storage_spawn), + ITEM_ENT(ASSET_CHEST, 32, ASSET_CHEST), }; diff --git a/code/game/src/world/world.c b/code/game/src/world/world.c index 6462940..076ca57 100644 --- a/code/game/src/world/world.c +++ b/code/game/src/world/world.c @@ -157,11 +157,13 @@ int32_t tracker_write_update(librg_world *w, librg_event *e) { } // NOTE(zaklaus): action-based updates +#if ECO2D_STREAM_ACTIONFILTER { if (view.kind != EKIND_CHUNK && !entity_can_stream(entity_id)) { return LIBRG_WRITE_REJECT; } } +#endif return (int32_t)entity_view_pack_struct(buffer, actual_length, view); }