From 890bc09d1f523fb3a83cf17d5d1046f7cb2c20c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 3 Nov 2021 19:09:19 +0100 Subject: [PATCH] introduce item_id + cleanup --- code/game/src/assets.h | 1 + code/game/src/assets_list.c | 1 + code/game/src/gui/build_mode.c | 4 ++-- code/game/src/items.c | 22 +++++++++++----------- code/game/src/items.h | 10 ++++++---- code/game/src/world/blocks.h | 2 +- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/code/game/src/assets.h b/code/game/src/assets.h index 0b3606b..116570c 100644 --- a/code/game/src/assets.h +++ b/code/game/src/assets.h @@ -6,6 +6,7 @@ typedef enum { // NOTE(zaklaus): Debug ASSET_EMPTY, + ASSET_BUILDMODE_HIGHLIGHT, // NOTE(zaklaus): entities ASSET_PLAYER, diff --git a/code/game/src/assets_list.c b/code/game/src/assets_list.c index e33446a..e3cb653 100644 --- a/code/game/src/assets_list.c +++ b/code/game/src/assets_list.c @@ -11,6 +11,7 @@ static asset assets[] = { ASSET_TEX(ASSET_EMPTY), + ASSET_TEX(ASSET_BUILDMODE_HIGHLIGHT), ASSET_TEX(ASSET_DEMO_ICEMAKER), // NOTE(zaklaus): blocks diff --git a/code/game/src/gui/build_mode.c b/code/game/src/gui/build_mode.c index 8ff7425..ce0e901 100644 --- a/code/game/src/gui/build_mode.c +++ b/code/game/src/gui/build_mode.c @@ -86,7 +86,7 @@ void buildmode_draw(void) { } if (!is_outside_range) - renderer_draw_single(cam.x, cam.y, ASSET_EMPTY, ColorAlpha(WHITE, 0.2f)); + renderer_draw_single(cam.x, cam.y, ASSET_BUILDMODE_HIGHLIGHT, ColorAlpha(WHITE, 0.2f)); build_num_placements = zpl_min(build_num_placements, qty); } @@ -94,7 +94,7 @@ void buildmode_draw(void) { for (size_t i = 0; i < build_num_placements; i++) { item_placement *it = &build_placements[i]; - renderer_draw_single(it->x, it->y, ASSET_EMPTY, ColorAlpha(WHITE, 0.4f)); + renderer_draw_single(it->x, it->y, ASSET_BUILDMODE_HIGHLIGHT, ColorAlpha(WHITE, 0.4f)); } if (build_is_in_draw_mode) { diff --git a/code/game/src/items.c b/code/game/src/items.c index 411f550..00468c4 100644 --- a/code/game/src/items.c +++ b/code/game/src/items.c @@ -11,7 +11,7 @@ #include "items_list.c" #define ITEMS_COUNT (sizeof(items)/sizeof(item_desc)) -static inline uint16_t item_resolve_proxy(uint16_t id) { +static inline item_id item_resolve_proxy(item_id id) { ZPL_ASSERT(id >= 0 && id < ITEMS_COUNT); item_usage usage = items[id].usage; if (usage == UKIND_PROXY) { @@ -37,8 +37,8 @@ uint64_t item_spawn(asset_id kind, uint32_t qty) { return (uint64_t)e; } -uint16_t item_find(asset_id kind) { - for (uint16_t i=0; ikind); - item_desc *desc = &items[item_id]; + uint16_t it_id = item_find(it->kind); + item_desc *desc = &items[it_id]; if (it->quantity <= 0) return; - switch (item_get_usage(item_id)) { + switch (item_get_usage(it_id)) { case UKIND_HOLD: /* NOOP */ break; case UKIND_PLACE:{ world_block_lookup l = world_block_from_realpos(p.x, p.y); if (l.is_outer && l.bid > 0) { asset_id item_asset = blocks_get_asset(l.bid); - uint16_t item_asset_id = item_find(item_asset); + item_id item_asset_id = item_find(item_asset); if (item_asset_id == ASSET_INVALID) return; // NOTE(zaklaus): If we replace the same item, refund 1 qty and let it replace it - if (item_asset_id == item_id) { + if (item_asset_id == it_id) { it->quantity++; } else { return; @@ -80,17 +80,17 @@ void item_despawn(uint64_t id) { entity_despawn(id); } -uint32_t item_max_quantity(uint16_t id) { +uint32_t item_max_quantity(item_id id) { ZPL_ASSERT(id >= 0 && id < ITEMS_COUNT); return items[id].max_quantity; } -item_usage item_get_usage(uint16_t id) { +item_usage item_get_usage(item_id id) { ZPL_ASSERT(id >= 0 && id < ITEMS_COUNT); return items[id].usage; } -bool item_get_place_directional(uint16_t id) { +bool item_get_place_directional(item_id id) { ZPL_ASSERT(id >= 0 && id < ITEMS_COUNT); return items[id].place.directional; } \ No newline at end of file diff --git a/code/game/src/items.h b/code/game/src/items.h index b1904ae..05cf0e1 100644 --- a/code/game/src/items.h +++ b/code/game/src/items.h @@ -34,14 +34,16 @@ typedef struct { }; } item_desc; +typedef uint16_t item_id; + // NOTE(zaklaus): item drops uint64_t item_spawn(asset_id kind, uint32_t qty); void item_despawn(uint64_t id); // NOTE(zaklaus): items -uint16_t item_find(asset_id kind); +item_id item_find(asset_id kind); void item_use(ecs_world_t *ecs, ItemDrop *it, Position p, uint64_t udata); -uint32_t item_max_quantity(uint16_t id); -item_usage item_get_usage(uint16_t id); -bool item_get_place_directional(uint16_t id); +uint32_t item_max_quantity(item_id id); +item_usage item_get_usage(item_id id); +bool item_get_place_directional(item_id id); diff --git a/code/game/src/world/blocks.h b/code/game/src/world/blocks.h index c5bbee8..5f48ffb 100644 --- a/code/game/src/world/blocks.h +++ b/code/game/src/world/blocks.h @@ -8,7 +8,7 @@ typedef enum { BLOCK_FLAG_ESSENTIAL = (1 << 3), } block_flags; -typedef uint8_t block_id; +typedef uint16_t block_id; int32_t blocks_setup(void); void blocks_destroy(void);