make item system depend on assets

isolation_bkp/dynres
Dominik Madarász 2021-11-02 11:48:32 +01:00
parent 9482b41fb9
commit f8da96b968
15 changed files with 39 additions and 41 deletions

BIN
art/fence.aseprite 100644

Binary file not shown.

BIN
art/gen/fence.png 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 301 B

View File

@ -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;

View File

@ -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,
}
};

View File

@ -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);

View File

@ -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);

View File

@ -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) {

View File

@ -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);

View File

@ -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<ITEMS_COUNT; i++) {
uint16_t item_find(asset_id kind) {
for (uint16_t i=0; i<ITEMS_COUNT; i++) {
if (items[i].kind == kind)
return i;
}
return ITEMS_INVALID;
return ASSET_INVALID;
}
void item_use(ecs_world_t *ecs, ItemDrop *it, Position p) {
@ -55,11 +55,6 @@ uint32_t item_max_quantity(uint16_t id) {
return items[id].max_quantity;
}
asset_id item_get_asset(uint16_t id) {
ZPL_ASSERT(id >= 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;

View File

@ -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);

View File

@ -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 = {

View File

@ -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);

View File

@ -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;
}

View File

@ -191,7 +191,7 @@ int32_t worldgen_test(world_data *wld) {
// items
#if 1
for (int i=0; i<RAND_RANGE(328, 164); i++) {
uint64_t e = item_spawn(IKIND_DEMO_ICEMAKER, 32);
uint64_t e = item_spawn(ASSET_DEMO_ICEMAKER, 32);
Position *dest = ecs_get_mut(world_ecs(), e, Position, NULL);
dest->x = RAND_RANGE(0, world->dim*WORLD_BLOCK_SIZE);

View File

@ -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;
}
}
}