From f8da96b96872568269c13d6d392d603a93f0268f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Tue, 2 Nov 2021 11:48:32 +0100 Subject: [PATCH] make item system depend on assets --- art/fence.aseprite | Bin 0 -> 662 bytes art/gen/fence.png | Bin 0 -> 301 bytes code/game/src/assets.h | 5 ++++- code/game/src/assets_list.c | 6 +++++- code/game/src/debug_ui_actions.c | 2 +- code/game/src/gen/texgen.c | 1 + code/game/src/gui/build_mode.c | 4 ++-- code/game/src/gui/inventory.c | 6 ++---- code/game/src/items.c | 13 ++++--------- code/game/src/items.h | 17 +++-------------- code/game/src/items_list.c | 7 +++++-- code/game/src/renderer_v0.c | 5 ++--- code/game/src/world/world.c | 2 +- code/game/src/world/worldgen/worldgen_test.c | 2 +- code/modules/source/system_items.c | 10 ++++++++-- 15 files changed, 39 insertions(+), 41 deletions(-) create mode 100644 art/fence.aseprite create mode 100644 art/gen/fence.png diff --git a/art/fence.aseprite b/art/fence.aseprite new file mode 100644 index 0000000000000000000000000000000000000000..a51c6ff7e390ef69dd195780308bc6f8c571beea GIT binary patch literal 662 zcmcJMQAkr!7{@(K7ZHz<`H-Zsu1uCh84=$F2jaV!~Cb}Ub2&6Rx zY1ASl2z*eXmrRjfdW@h4p`6GH+(I%z@kw*X-(7~j^yqi)`El-d9{#`Y-e)F}?{BKa z!%|WuB6rz`MbBu;8P;)>mHeGeb0yVGfR$}gmB=G;dCv7p9dv9*ZYoDDa!$FR_d+WS zwK!m=`wA??8sXAJ2yT3Q0e819!{^0z82Mm_&Erw%4>!S1|9R+aZGmq0Lue1AU~lvX z4CXZK`@9N2E&qX<63I=^!bi!+FdT`%+ufaT;OZ!xaysEsPY`|`nunRJ502iq>j(p! zG`_pyzz|G;KJJKPjWWK-Vu~h?2x5mCUPxhtj^Uy;7ATNUILXA4Ng!!N*^DH00{$x$ z#3DED4E8@zjx;Z3Wn5WA^HctoM_dwr`8)r%X2gEh+i`8|*1J?X@XKUPovIG$w%3V! zmr|wS_bcv(g)gTsPUfH1b;WL$N>N|W?zcj*9Dnt5@c)m0XEt}eyqxUKZZFyrYkPC+ Mx@)F3^Q3WNA6Z<+0w@{ne~u+mqif)ekUi zVd!PdV3cA8s%2Zin!)41eL?3^b20z` literal 0 HcmV?d00001 diff --git a/code/game/src/assets.h b/code/game/src/assets.h index 923671e..f348609 100644 --- a/code/game/src/assets.h +++ b/code/game/src/assets.h @@ -5,7 +5,7 @@ typedef enum { // NOTE(zaklaus): Debug - ASSET_DEBUG_TILE, + ASSET_EMPTY, // NOTE(zaklaus): entities ASSET_PLAYER, @@ -14,6 +14,9 @@ typedef enum { // NOTE(zaklaus): items ASSET_DEMO_ICEMAKER, + // NOTE(zaklaus): blocks + ASSET_FENCE, + MAX_ASSETS, FORCE_ASSET_UINT16 = UINT16_MAX } asset_id; diff --git a/code/game/src/assets_list.c b/code/game/src/assets_list.c index 46cd8ed..d52cfa8 100644 --- a/code/game/src/assets_list.c +++ b/code/game/src/assets_list.c @@ -2,11 +2,15 @@ static asset assets[] = { { - .id = ASSET_DEBUG_TILE, + .id = ASSET_EMPTY, .kind = AKIND_TEXTURE, }, { .id = ASSET_DEMO_ICEMAKER, .kind = AKIND_TEXTURE, + }, + { + .id = ASSET_FENCE, + .kind = AKIND_TEXTURE, } }; diff --git a/code/game/src/debug_ui_actions.c b/code/game/src/debug_ui_actions.c index abd9847..b9111f7 100644 --- a/code/game/src/debug_ui_actions.c +++ b/code/game/src/debug_ui_actions.c @@ -21,7 +21,7 @@ ActSpawnCar(void) { void ActSpawnIcemaker(void) { - ecs_entity_t e = item_spawn(IKIND_DEMO_ICEMAKER, 32); + ecs_entity_t e = item_spawn(ASSET_DEMO_ICEMAKER, 32); ecs_entity_t plr = camera_get().ent_id; Position const* origin = ecs_get(world_ecs(), plr, Position); diff --git a/code/game/src/gen/texgen.c b/code/game/src/gen/texgen.c index 0f6c002..835bf65 100644 --- a/code/game/src/gen/texgen.c +++ b/code/game/src/gen/texgen.c @@ -36,6 +36,7 @@ Texture2D texgen_build_block(uint32_t biome, uint32_t kind) { Texture2D texgen_build_sprite(asset_id id) { switch (id) { case ASSET_DEMO_ICEMAKER: return LoadImageEco("demo_icemaker"); + case ASSET_FENCE: return LoadImageEco("fence"); default: { Image img = GenImageColor(1, 1, RAYWHITE); diff --git a/code/game/src/gui/build_mode.c b/code/game/src/gui/build_mode.c index e66479a..f4bcc8b 100644 --- a/code/game/src/gui/build_mode.c +++ b/code/game/src/gui/build_mode.c @@ -70,14 +70,14 @@ void buildmode_draw(void) { } if (!is_outside_range) - renderer_draw_single(cam.x, cam.y, ASSET_DEBUG_TILE, ColorAlpha(BLUE, 0.4f)); + renderer_draw_single(cam.x, cam.y, ASSET_EMPTY, ColorAlpha(BLUE, 0.4f)); build_num_placements = zpl_min(build_num_placements, qty); } for (size_t i = 0; i < build_num_placements; i++) { item_placement *it = &build_placements[i]; - renderer_draw_single(it->x, it->y, ASSET_DEBUG_TILE, ColorAlpha(BLUE, 0.4f)); + renderer_draw_single(it->x, it->y, ASSET_EMPTY, ColorAlpha(BLUE, 0.4f)); } if (build_is_in_draw_mode) { diff --git a/code/game/src/gui/inventory.c b/code/game/src/gui/inventory.c index e530a60..e93f032 100644 --- a/code/game/src/gui/inventory.c +++ b/code/game/src/gui/inventory.c @@ -64,8 +64,7 @@ void inventory_draw() { DrawRectangleLinesEco(x, y, 64, 64, color); if (item->quantity > 0) { - asset_id asset = item_get_asset(item->kind); - DrawTexturePro(GetSpriteTexture2D(assets_find(asset)), ASSET_SRC_RECT(), ASSET_DST_RECT(x,y), (Vector2){0.5f,0.5f}, 0.0f, WHITE); + DrawTexturePro(GetSpriteTexture2D(assets_find(item->kind)), ASSET_SRC_RECT(), ASSET_DST_RECT(x,y), (Vector2){0.5f,0.5f}, 0.0f, WHITE); DrawTextEco(zpl_bprintf("%d", item->quantity), x+5, y+5, 16, RAYWHITE, 0.0f); } } @@ -81,8 +80,7 @@ void inventory_draw() { Vector2 mpos = GetMousePosition(); mpos.x -= 32; mpos.y -= 32; - asset_id asset = item_get_asset(item_find(inv_held_item.kind)); - DrawTexturePro(GetSpriteTexture2D(assets_find(asset)), ASSET_SRC_RECT(), ASSET_DST_RECT(mpos.x, mpos.y), (Vector2){0.5f,0.5f}, 0.0f, ColorAlpha(WHITE, 0.8f)); + DrawTexturePro(GetSpriteTexture2D(assets_find(inv_held_item.kind)), ASSET_SRC_RECT(), ASSET_DST_RECT(mpos.x, mpos.y), (Vector2){0.5f,0.5f}, 0.0f, ColorAlpha(WHITE, 0.8f)); DrawTextEco(zpl_bprintf("%d", inv_held_item.quantity), mpos.x, mpos.y, 16, RAYWHITE, 0.0f); debug_area_status area = check_mouse_area(sx, sy, 64*3, 64*3); diff --git a/code/game/src/items.c b/code/game/src/items.c index 5b659ae..9d43e20 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)) -uint64_t item_spawn(item_kind kind, uint32_t qty) { +uint64_t item_spawn(asset_id kind, uint32_t qty) { ecs_entity_t e = entity_spawn(EKIND_ITEM); ItemDrop *d = ecs_get_mut(world_ecs(), e, ItemDrop, NULL); @@ -24,12 +24,12 @@ uint64_t item_spawn(item_kind kind, uint32_t qty) { return (uint64_t)e; } -uint16_t item_find(item_kind kind) { - for (uint32_t i=0; i= 0 && id < ITEMS_COUNT); - return items[id].asset; -} - item_usage item_get_usage(uint16_t id) { ZPL_ASSERT(id >= 0 && id < ITEMS_COUNT); return items[id].usage; diff --git a/code/game/src/items.h b/code/game/src/items.h index c3d0a49..0122fc0 100644 --- a/code/game/src/items.h +++ b/code/game/src/items.h @@ -5,12 +5,6 @@ #include "modules/components.h" -#define ITEMS_INVALID 0xFF - -typedef enum { - IKIND_DEMO_ICEMAKER, -} item_kind; - typedef enum { UKIND_PLACE, UKIND_PLACE_ITEM, @@ -18,9 +12,8 @@ typedef enum { } item_usage; typedef struct { - item_kind kind; + asset_id kind; item_usage usage; - asset_id asset; uint32_t max_quantity; // NOTE(zaklaus): usage data @@ -33,16 +26,12 @@ typedef struct { } item_desc; // NOTE(zaklaus): item drops -uint64_t item_spawn(item_kind kind, uint32_t qty); +uint64_t item_spawn(asset_id kind, uint32_t qty); void item_despawn(uint64_t id); // NOTE(zaklaus): items -uint16_t item_find(item_kind kind); +uint16_t item_find(asset_id kind); void item_use(ecs_world_t *ecs, ItemDrop *it, Position p); uint32_t item_max_quantity(uint16_t id); item_usage item_get_usage(uint16_t id); - -// NOTE(zaklaus): client - -asset_id item_get_asset(uint16_t id); \ No newline at end of file diff --git a/code/game/src/items_list.c b/code/game/src/items_list.c index b1f2b41..e405939 100644 --- a/code/game/src/items_list.c +++ b/code/game/src/items_list.c @@ -2,9 +2,12 @@ static item_desc items[] = { { - .kind = IKIND_DEMO_ICEMAKER, + .kind = 0, + .max_quantity = 0, + }, + { + .kind = ASSET_DEMO_ICEMAKER, .usage = UKIND_PLACE, - .asset = ASSET_DEMO_ICEMAKER, .max_quantity = 64, .place = { diff --git a/code/game/src/renderer_v0.c b/code/game/src/renderer_v0.c index 46ad7bd..5f75e3a 100644 --- a/code/game/src/renderer_v0.c +++ b/code/game/src/renderer_v0.c @@ -75,10 +75,9 @@ void DEBUG_draw_entities(uint64_t key, entity_view * data) { float ix = data->x; float iy = data->y; if (data->items[data->selected_item].quantity > 0) { - item_kind it_kind = data->items[data->selected_item].kind; + asset_id it_kind = data->items[data->selected_item].kind; uint32_t qty = data->items[data->selected_item].quantity; - uint16_t it_id = item_find(it_kind); - DrawTexturePro(GetSpriteTexture2D(assets_find(item_get_asset(it_id))), ASSET_SRC_RECT(), ((Rectangle){ix, iy, 32, 32}), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE)); + DrawTexturePro(GetSpriteTexture2D(assets_find(it_kind)), ASSET_SRC_RECT(), ((Rectangle){ix, iy, 32, 32}), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE)); if (!inv_is_open) DrawTextEco(zpl_bprintf("%d", qty), ix+24, iy+24, 8, RAYWHITE, 0.0f); diff --git a/code/game/src/world/world.c b/code/game/src/world/world.c index a019c39..f900737 100644 --- a/code/game/src/world/world.c +++ b/code/game/src/world/world.c @@ -55,7 +55,7 @@ entity_view world_build_entity_view(int64_t e) { if (ecs_get(world_ecs(), e, ItemDrop)) { ItemDrop const* dr = ecs_get(world_ecs(), e, ItemDrop); - view.asset = item_get_asset(dr->kind); + view.asset = dr->kind; view.quantity = dr->quantity; } diff --git a/code/game/src/world/worldgen/worldgen_test.c b/code/game/src/world/worldgen/worldgen_test.c index e7e370d..9699cb6 100644 --- a/code/game/src/world/worldgen/worldgen_test.c +++ b/code/game/src/world/worldgen/worldgen_test.c @@ -191,7 +191,7 @@ int32_t worldgen_test(world_data *wld) { // items #if 1 for (int i=0; ix = RAND_RANGE(0, world->dim*WORLD_BLOCK_SIZE); diff --git a/code/modules/source/system_items.c b/code/modules/source/system_items.c index ee6593b..e14b7bf 100644 --- a/code/modules/source/system_items.c +++ b/code/modules/source/system_items.c @@ -23,14 +23,16 @@ void PickItem(ecs_iter_t *it) { float dy = p2->y - p[i].y; float range = zpl_sqrt(dx*dx + dy*dy); if (range <= ITEM_PICK_RADIUS) { + uint16_t drop_id = item_find(drop->kind); for (size_t k = 0; k < ITEMS_INVENTORY_SIZE; k += 1) { ItemDrop *item = &inv[i].items[k]; uint16_t item_id = item_find(item->kind); - if ((item->quantity == 0 || (item->quantity != 0 && item->kind == drop->kind)) && item->quantity < item_max_quantity(item_id)) { + if (item_id != ASSET_INVALID && (item->quantity == 0 || (item->quantity != 0 && item->kind == drop->kind)) && item->quantity < item_max_quantity(drop_id)) { uint32_t picked_count = zpl_max(0, drop->quantity); - picked_count = zpl_clamp(picked_count, 0, item_max_quantity(item_id) - item->quantity); + picked_count = zpl_clamp(picked_count, 0, item_max_quantity(drop_id) - item->quantity); item->quantity += picked_count; drop->quantity -= picked_count; + item->kind = drop->kind; if (drop->quantity == 0) item_despawn(ents[j]); @@ -91,6 +93,10 @@ void DropItem(ecs_iter_t *it) { inv[i].pickup_time = game_time() + ITEM_DROP_PICKUP_TIME; in[i].drop = false; + + if (item->quantity == 0) { + item->kind = 0; + } } }