durability meter + fix dropping items

isolation
Dominik Madarász 2022-09-29 22:49:40 +02:00
parent e51d941010
commit 9a6a8b360a
8 changed files with 99 additions and 55 deletions

View File

@ -142,6 +142,7 @@ static debug_item items[] = {
{ .kind = DITEM_BUTTON, .name = "spawn belt", .on_click = ActSpawnBelt },
{ .kind = DITEM_BUTTON, .name = "spawn furnace", .on_click = ActSpawnFurnace },
{ .kind = DITEM_BUTTON, .name = "spawn demo blueprint", .on_click = ActSpawnDemoHouseItem },
{ .kind = DITEM_BUTTON, .name = "spawn random durability icemaker", .on_click = ActSpawnDurabilityTest },
{
.kind = DITEM_LIST,
.name = "demo npcs",

View File

@ -60,6 +60,20 @@ ActSpawnBelt(void) {
debug_replay_special_action(RPKIND_SPAWN_BELT);
}
void
ActSpawnDurabilityTest(void) {
ecs_entity_t e = item_spawn(ASSET_DEMO_ICEMAKER, 1);
ecs_entity_t plr = camera_get().ent_id;
Position const* origin = ecs_get(world_ecs(), plr, Position);
Position * dest = ecs_get_mut(world_ecs(), e, Position);
*dest = *origin;
entity_set_position(e, dest->x, dest->y);
Item *it = ecs_get_mut(world_ecs(), e, Item);
it->durability = (float)(rand() % 100) / 100.0f;
}
void
ActSpawnFurnace(void) {
ecs_entity_t e = item_spawn(ASSET_FURNACE, 32);

View File

@ -84,8 +84,16 @@ void inventory_draw_panel(entity_view *e, bool is_player, float sx, float sy){
if (item->quantity > 0) {
DrawTexturePro(GetSpriteTexture2D(assets_find(item->kind)), ASSET_SRC_RECT(), ASSET_DST_RECT(x,y), (Vector2){0.5f,0.5f}, 0.0f, WHITE);
}
if (item->quantity > 1) {
DrawTextEco(zpl_bprintf("%d", item->quantity), x+5, y+5, 16, RAYWHITE, 0.0f);
}
if (item->quantity > 0 && item->durability < 1.0f) {
DrawRectangleEco(x, y+56, 64, 8, BLACK);
DrawRectangleEco(x, y+56, 64*item->durability, 8, BlendColor(RED, GREEN, item->durability));
}
}
x += 64;
@ -114,7 +122,7 @@ void inventory_render_held_item(bool is_player){
if (!inv->is_inside && IsMouseButtonReleased(MOUSE_LEFT_BUTTON) && !inv2->is_inside) {
inv->drop_item = true;
inv->item_is_held = false;
inv_is_storage_action = true;
inv_is_storage_action = inv == &storage_inv;
}
}
}

View File

@ -10,6 +10,16 @@
static inline float lerp(float a, float b, float t) { return a * (1.0f - t) + b * t; }
static inline
Color BlendColor(Color a, Color b, float t) {
return (Color) {
.r = (uint8_t)(lerp((float)(a.r)/255.0f, (float)(b.r)/255.0f, t) * 255),
.g = (uint8_t)(lerp((float)(a.g)/255.0f, (float)(b.g)/255.0f, t) * 255),
.b = (uint8_t)(lerp((float)(a.b)/255.0f, (float)(b.b)/255.0f, t) * 255),
.a = (uint8_t)(lerp((float)(a.a)/255.0f, (float)(b.a)/255.0f, t) * 255),
};
}
static inline
Texture2D LoadTexEco(const char *name) {
static char filename[128];

View File

@ -31,6 +31,7 @@ pkt_desc pkt_entity_view_desc[] = {
{ PKT_KEEP_IF(entity_view, kind, EKIND_ITEM, 2) },
{ PKT_UINT(entity_view, asset) },
{ PKT_UINT(entity_view, quantity) },
{ PKT_HALF(entity_view, durability) },
{ PKT_KEEP_IF(entity_view, kind, EKIND_DEVICE, 3) },
{ PKT_UINT(entity_view, asset) },

View File

@ -62,6 +62,7 @@ typedef struct entity_view {
// NOTE(zaklaus): items, ...
asset_id asset;
uint32_t quantity;
float durability;
// NOTE(zaklaus): device progress bar
uint32_t progress_active;

View File

@ -63,6 +63,7 @@ entity_view *world_build_entity_view(int64_t e) {
Item const* dr = ecs_get(world_ecs(), e, Item);
view.asset = dr->kind;
view.quantity = dr->quantity;
view.durability = dr->durability;
}
if (ecs_get(world_ecs(), e, Device)) {

View File

@ -110,7 +110,15 @@ void DEBUG_draw_entities(uint64_t key, entity_view * data) {
float x = data->x - 32.f;
float y = data->y - 32.f;
DrawTexturePro(GetSpriteTexture2D(assets_find(data->asset)), ASSET_SRC_RECT(), ASSET_DST_RECT(x,y), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE));
if (data->quantity > 1) {
DrawTextEco(zpl_bprintf("%d", data->quantity), x, y, 10, ALPHA(RAYWHITE), 0.0f);
}
if (data->durability < 1.0f) {
DrawRectangleEco(x, y+32, 4, 32, BlendColor(RED, GREEN, data->durability));
DrawRectangleEco(x, y+32, 4, 32*(1.0f-data->durability), ColorAlpha(BLACK, data->tran_time));
}
}break;
default:break;
}