introduce item_id + cleanup

isolation_bkp/dynres
Dominik Madarász 2021-11-03 19:09:19 +01:00
parent 331ee39672
commit 890bc09d1f
6 changed files with 22 additions and 18 deletions

View File

@ -6,6 +6,7 @@
typedef enum {
// NOTE(zaklaus): Debug
ASSET_EMPTY,
ASSET_BUILDMODE_HIGHLIGHT,
// NOTE(zaklaus): entities
ASSET_PLAYER,

View File

@ -11,6 +11,7 @@
static asset assets[] = {
ASSET_TEX(ASSET_EMPTY),
ASSET_TEX(ASSET_BUILDMODE_HIGHLIGHT),
ASSET_TEX(ASSET_DEMO_ICEMAKER),
// NOTE(zaklaus): blocks

View File

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

View File

@ -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; i<ITEMS_COUNT; i++) {
item_id item_find(asset_id kind) {
for (item_id i=0; i<ITEMS_COUNT; i++) {
if (items[i].kind == kind)
return item_resolve_proxy(i);
}
@ -47,20 +47,20 @@ uint16_t item_find(asset_id kind) {
void item_use(ecs_world_t *ecs, ItemDrop *it, Position p, uint64_t udata) {
(void)ecs;
uint16_t item_id = item_find(it->kind);
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;
}

View File

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

View File

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