From da8030bab86b3be395879aed56e260ba59aae234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Thu, 15 Sep 2022 22:50:48 +0200 Subject: [PATCH] device code --- code/game/CMakeLists.txt | 1 + code/game/src/device.c | 19 ++++++++++ code/game/src/device.h | 6 ++++ code/game/src/entity.c | 27 +++++++------- code/game/src/entity.h | 2 +- code/game/src/entity_spawnlist.c | 2 +- code/game/src/items_list.c | 56 ++---------------------------- code/game/src/items_list_helpers.h | 51 +++++++++++++++++++++++++++ code/game/src/storage.c | 6 ++-- 9 files changed, 96 insertions(+), 74 deletions(-) create mode 100644 code/game/src/device.c create mode 100644 code/game/src/device.h create mode 100644 code/game/src/items_list_helpers.h diff --git a/code/game/CMakeLists.txt b/code/game/CMakeLists.txt index 4734aa0..c32528f 100644 --- a/code/game/CMakeLists.txt +++ b/code/game/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable(eco2d src/player.c src/vehicle.c src/storage.c + src/device.c src/signal_handling.c src/profiler.c src/debug_ui.c diff --git a/code/game/src/device.c b/code/game/src/device.c new file mode 100644 index 0000000..e4872b4 --- /dev/null +++ b/code/game/src/device.c @@ -0,0 +1,19 @@ +#include "device.h" +#include "entity.h" +#include "entity_view.h" +#include "world/world.h" + +#include "modules/components.h" + +uint64_t device_spawn(asset_id id) { + ecs_entity_t e = entity_spawn(EKIND_DEVICE); + + Device *dev = ecs_get_mut(world_ecs(), e, Device); + dev->asset = id; + + return (uint64_t)e; +} + +void device_despawn(uint64_t ent_id) { + entity_despawn(ent_id); +} diff --git a/code/game/src/device.h b/code/game/src/device.h new file mode 100644 index 0000000..89a6a23 --- /dev/null +++ b/code/game/src/device.h @@ -0,0 +1,6 @@ +#pragma once +#include "system.h" +#include "assets.h" + +uint64_t device_spawn(asset_id id); +void device_despawn(uint64_t ent_id); diff --git a/code/game/src/entity.c b/code/game/src/entity.c index cac3cb7..190abab 100644 --- a/code/game/src/entity.c +++ b/code/game/src/entity.c @@ -8,6 +8,9 @@ #include "modules/systems.h" #include "zpl.h" +// NOTE(zaklaus): bring in entity spawnlist +#include "entity_spawnlist.c" + uint64_t entity_spawn(uint16_t class_id) { ecs_entity_t e = ecs_new(world_ecs(), 0); @@ -33,6 +36,16 @@ uint64_t entity_spawn(uint16_t class_id) { return (uint64_t)e; } +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_batch_despawn(uint64_t *ids, size_t num_ids) { for (size_t i = 0; i < num_ids; i++ ) { librg_entity_untrack(world_tracker(), ids[i]); @@ -45,20 +58,6 @@ 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 c0f4792..fe77d22 100644 --- a/code/game/src/entity.h +++ b/code/game/src/entity.h @@ -4,11 +4,11 @@ #define ENTITY_ACTION_VELOCITY_THRESHOLD 0.05f uint64_t entity_spawn(uint16_t class_id /* 0 = no streaming */); +uint64_t entity_spawn_id(uint16_t id); 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); diff --git a/code/game/src/entity_spawnlist.c b/code/game/src/entity_spawnlist.c index 3b54b1f..ad99f42 100644 --- a/code/game/src/entity_spawnlist.c +++ b/code/game/src/entity_spawnlist.c @@ -1,7 +1,7 @@ // NOTE(zaklaus): access to spawners #include "storage.h" -struct { +static struct { asset_id id; uint64_t (*proc)(); } entity_spawnlist[] = { diff --git a/code/game/src/items_list.c b/code/game/src/items_list.c index 4a137b1..c4a8862 100644 --- a/code/game/src/items_list.c +++ b/code/game/src/items_list.c @@ -1,61 +1,9 @@ #include "items.h" #include "entity_view.h" - -#define ITEM_HOLD(asset, qty)\ -{\ -.kind = asset,\ -.usage = UKIND_HOLD,\ -.max_quantity = qty,\ -} - -#define ITEM_BLOCK(asset, qty, build_asset)\ -{\ -.kind = asset,\ -.usage = UKIND_PLACE,\ -.max_quantity = qty,\ -.place = {\ -.kind = build_asset,\ -}\ -} - -#define ITEM_BLOCK_DIR(asset, qty, build_asset)\ -{\ -.kind = asset,\ -.usage = UKIND_PLACE,\ -.max_quantity = qty,\ -.place = {\ -.kind = build_asset,\ -.directional = true,\ -}\ -} - -#define ITEM_PROXY(asset, proxy_id)\ -{\ -.kind = asset,\ -.usage = UKIND_PROXY,\ -.proxy = {\ -.id = proxy_id,\ -}\ -} - -#define ITEM_ENT(asset, qty, eid)\ -{\ -.kind = asset,\ -.usage = UKIND_PLACE_ITEM,\ -.max_quantity = qty,\ -.place_item = {\ -.id = eid\ -}\ -} - -#define ITEM_SELF(asset, qty) ITEM_BLOCK(asset, qty, asset) -#define ITEM_SELF_DIR(asset, qty) ITEM_BLOCK_DIR(asset, qty, asset) +#include "items_list_helpers.h" static item_desc items[] = { - { - .kind = 0, - .max_quantity = 0, - }, + { .kind = 0, .max_quantity = 0, }, ITEM_BLOCK(ASSET_DEMO_ICEMAKER, 64, ASSET_WATER), ITEM_SELF(ASSET_FENCE, 64), ITEM_SELF(ASSET_WOOD, 64), diff --git a/code/game/src/items_list_helpers.h b/code/game/src/items_list_helpers.h new file mode 100644 index 0000000..166d50f --- /dev/null +++ b/code/game/src/items_list_helpers.h @@ -0,0 +1,51 @@ +#pragma once + +#define ITEM_HOLD(asset, qty)\ +{\ +.kind = asset,\ +.usage = UKIND_HOLD,\ +.max_quantity = qty,\ +} + +#define ITEM_BLOCK(asset, qty, build_asset)\ +{\ +.kind = asset,\ +.usage = UKIND_PLACE,\ +.max_quantity = qty,\ +.place = {\ +.kind = build_asset,\ +}\ +} + +#define ITEM_BLOCK_DIR(asset, qty, build_asset)\ +{\ +.kind = asset,\ +.usage = UKIND_PLACE,\ +.max_quantity = qty,\ +.place = {\ +.kind = build_asset,\ +.directional = true,\ +}\ +} + +#define ITEM_PROXY(asset, proxy_id)\ +{\ +.kind = asset,\ +.usage = UKIND_PROXY,\ +.proxy = {\ +.id = proxy_id,\ +}\ +} + +#define ITEM_ENT(asset, qty, eid)\ +{\ +.kind = asset,\ +.usage = UKIND_PLACE_ITEM,\ +.max_quantity = qty,\ +.place_item = {\ +.id = eid\ +}\ +} + +#define ITEM_SELF(asset, qty) ITEM_BLOCK(asset, qty, asset) +#define ITEM_SELF_DIR(asset, qty) ITEM_BLOCK_DIR(asset, qty, asset) diff --git a/code/game/src/storage.c b/code/game/src/storage.c index c6b13d6..e06c126 100644 --- a/code/game/src/storage.c +++ b/code/game/src/storage.c @@ -1,4 +1,5 @@ #include "storage.h" +#include "device.h" #include "entity.h" #include "entity_view.h" #include "world/world.h" @@ -6,10 +7,7 @@ #include "modules/components.h" uint64_t storage_spawn(void) { - ecs_entity_t e = entity_spawn(EKIND_DEVICE); - - Device *dev = ecs_get_mut(world_ecs(), e, Device); - dev->asset = ASSET_CHEST; + ecs_entity_t e = device_spawn(ASSET_CHEST); ItemContainer *storage = ecs_get_mut(world_ecs(), e, ItemContainer); *storage = (ItemContainer){0};