add device progress bar

isolation
Dominik Madarász 2022-09-29 18:06:08 +02:00
parent ed5fd927ab
commit 24e30dd90a
8 changed files with 66 additions and 41 deletions

View File

@ -148,6 +148,10 @@ typedef struct {
typedef struct {
uint16_t asset;
// progress bar
uint8_t progress_active;
float progress_value;
} Device;
typedef struct {

View File

@ -35,31 +35,31 @@ int32_t pkt_header_encode(pkt_messages id, uint16_t view_id, void *data, size_t
int32_t pkt_header_decode(pkt_header *table, void *data, size_t datalen) {
cw_unpack_context uc = {0};
pkt_unpack_msg_raw(&uc, data, (uint32_t)datalen, PKT_HEADER_ELEMENTS);
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER || uc.item.as.u64 > UINT16_MAX) {
return -1; // invalid packet id
}
uint16_t pkt_id = (uint16_t)uc.item.as.u64;
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER || uc.item.as.u64 > UINT16_MAX) {
return -1; // invalid view id
}
uint16_t view_id = (uint16_t)uc.item.as.u64;
cw_unpack_next(&uc);
const void *packed_blob = uc.item.as.bin.start;
uint32_t packed_size = uc.item.as.bin.length;
table->id = pkt_id;
table->view_id = view_id;
table->data = (void *)packed_blob;
table->datalen = packed_size;
table->ok = 1;
return pkt_validate_eof_msg(&uc) != -1;
}
@ -95,12 +95,12 @@ int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob,
zpl_memcopy(blob + field->offset, bin_buf, actual_size);
}break;
default: {
zpl_printf("[WARN] unsupported pkt field type %lld !\n", field->type);
zpl_printf("[WARN] unsupported pkt field type %lld !\n", field->type);
return -1; // unsupported field
}break;
}
}
return 0;
}
@ -117,10 +117,10 @@ int32_t pkt_pack_struct(cw_pack_context *pc, pkt_desc *desc, void *raw_blob, uin
} else {
cw_pack_unsigned(pc, 0);
}
continue;
}
switch (field->type) {
case CWP_ITEM_BIN: {
if (field->size >= PKT_BUFSIZ) return -1; // bin blob too big
@ -149,12 +149,12 @@ int32_t pkt_pack_struct(cw_pack_context *pc, pkt_desc *desc, void *raw_blob, uin
cw_pack_float(pc, num);
}break;
default: {
zpl_printf("[WARN] unsupported pkt field type %lld !\n", field->type);
zpl_printf("[WARN] unsupported pkt field type %lld !\n", field->type);
return -1; // unsupported field
}break;
}
}
return 0;
}
@ -168,7 +168,7 @@ void pkt_dump_struct(pkt_desc *desc, void* raw_blob, uint32_t blob_size) {
if (val == field->skip_eq) {
field += field->skip_count;
}
continue;
}
zpl_printf(" \"%s\": ", field->name);
@ -189,7 +189,7 @@ void pkt_dump_struct(pkt_desc *desc, void* raw_blob, uint32_t blob_size) {
zpl_printf("%f\n", *(double*)(blob + field->offset));
}break;
default: {
zpl_printf("[WARN] unsupported pkt field type %lld !\n", field->type);
zpl_printf("[WARN] unsupported pkt field type %lld !\n", field->type);
return; // unsupported field
}break;
}

View File

@ -19,7 +19,7 @@ void ProduceItems(ecs_iter_t *it) {
continue;
}
// if (producer[i].burn_time <= 0.0f) continue; TODO
// if (producer[i].energy_level <= 0.0f) continue;
if (producer[i].process_time < game_time()) {
if (producer[i].processed_item > 0) {
uint64_t e = item_spawn(producer[i].processed_item, 1);
@ -41,6 +41,9 @@ void ProduceItems(ecs_iter_t *it) {
}
}
}
d[i].progress_active = (producer[i].processed_item > 0);
d[i].progress_value = 1.0f-((producer[i].process_time - game_time()) / game_rules.furnace_cook_time);
}
}

View File

@ -5,50 +5,52 @@
ZPL_TABLE_DEFINE(entity_view_tbl, entity_view_tbl_, entity_view);
pkt_desc pkt_entity_view_desc[] = {
{ PKT_UINT(entity_view, kind) },
{ PKT_UINT(entity_view, flag) },
{ PKT_HALF(entity_view, x) },
{ PKT_HALF(entity_view, y) },
{ PKT_KEEP_IF(entity_view, blocks_used, 0, 2) }, // NOTE(zaklaus): skip velocity for chunks
{ PKT_HALF(entity_view, vx) },
{ PKT_HALF(entity_view, vy) },
{ PKT_SKIP_IF(entity_view, blocks_used, 0, 2) }, // NOTE(zaklaus): skip blocks for anything else
{ PKT_ARRAY(entity_view, blocks) },
{ PKT_ARRAY(entity_view, outer_blocks) },
{ PKT_KEEP_IF(entity_view, blocks_used, 0, 2) }, // NOTE(zaklaus): skip hp for chunks
{ PKT_HALF(entity_view, hp) },
{ PKT_HALF(entity_view, max_hp) },
{ PKT_KEEP_IF(entity_view, kind, EKIND_VEHICLE, 1) }, // NOTE(zaklaus): keep for vehicles
{ PKT_HALF(entity_view, heading) },
{ PKT_UINT(entity_view, inside_vehicle) },
{ PKT_UINT(entity_view, veh_kind) },
{ PKT_KEEP_IF(entity_view, kind, EKIND_ITEM, 2) },
{ PKT_UINT(entity_view, asset) },
{ PKT_UINT(entity_view, quantity) },
{ PKT_KEEP_IF(entity_view, kind, EKIND_DEVICE, 1) },
{ PKT_KEEP_IF(entity_view, kind, EKIND_DEVICE, 3) },
{ PKT_UINT(entity_view, asset) },
{ PKT_UINT(entity_view, progress_active) },
{ PKT_HALF(entity_view, progress_value) },
{ PKT_KEEP_IF(entity_view, has_items, true, 3) },
{ PKT_UINT(entity_view, has_items) },
{ PKT_UINT(entity_view, selected_item) },
{ PKT_ARRAY(entity_view, items) },
{ PKT_UINT(entity_view, pick_ent) },
{ PKT_UINT(entity_view, sel_ent) },
{ PKT_KEEP_IF(entity_view, has_storage_items, true, 3) },
{ PKT_UINT(entity_view, has_storage_items) },
{ PKT_UINT(entity_view, storage_selected_item) },
{ PKT_ARRAY(entity_view, storage_items) },
{ PKT_END },
{ PKT_END },
};
size_t entity_view_pack_struct(void *data, size_t len, entity_view *view) {
@ -61,10 +63,10 @@ size_t entity_view_pack_struct(void *data, size_t len, entity_view *view) {
entity_view entity_view_unpack_struct(void *data, size_t len) {
cw_unpack_context uc = {0};
cw_unpack_context_init(&uc, data, (unsigned long)len, 0);
entity_view view = {0};
pkt_unpack_struct(&uc, pkt_entity_view_desc, PKT_STRUCT_PTR(&view));
return view;
}

View File

@ -63,6 +63,10 @@ typedef struct entity_view {
asset_id asset;
uint32_t quantity;
// NOTE(zaklaus): device progress bar
uint32_t progress_active;
float progress_value;
// NOTE(zaklaus): inventory
uint8_t has_items;
Item items[ITEMS_INVENTORY_SIZE];

View File

@ -22,25 +22,25 @@ static inline float spherical_lerp(float a, float b, float t) {
a = base_angle(a);
b = base_angle(b);
float d = b - a;
if (d < -ZPL_PI) {
b += ZPL_TAU;
} else if (d > ZPL_PI) {
b -= ZPL_TAU;
}
return base_angle(zpl_lerp(a, b, t));
}
float smooth_val(float cur, float tgt, float dt) {
float factor = zpl_clamp01(map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_MP_FAST_MS, WORLD_TRACKER_UPDATE_MP_SLOW_MS)));
return zpl_lerp(cur, tgt, zpl_clamp01(zpl_lerp(PREDICT_SMOOTH_FACTOR_LO, PREDICT_SMOOTH_FACTOR_HI, factor)*platform_frametime()));
}
float smooth_val_spherical(float cur, float tgt, float dt) {
float factor = zpl_clamp01(map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_MP_FAST_MS, WORLD_TRACKER_UPDATE_MP_SLOW_MS)));
return spherical_lerp(cur, tgt, zpl_clamp01(zpl_lerp(PREDICT_SMOOTH_FACTOR_LO, PREDICT_SMOOTH_FACTOR_HI, factor)*platform_frametime()));
}
@ -57,7 +57,7 @@ void predict_receive_update(entity_view *d, entity_view *data) {
data->ty = ty;
data->theading = theading;
}
data->tran_effect = d->tran_effect;
data->tran_time = d->tran_time;
}
@ -67,7 +67,7 @@ void predict_receive_update(entity_view *d, entity_view *data) {
void lerp_entity_positions(uint64_t key, entity_view *data) {
(void)key;
world_view *view = game_world_view_get_active();
if (data->flag == EFLAG_INTERP) {
#if ENTITY_DO_LERP_SP==0
if (game_get_kind() == GAMEKIND_CLIENT)
@ -92,22 +92,22 @@ void do_entity_fadeinout(uint64_t key, entity_view * data) {
switch (data->tran_effect) {
case ETRAN_FADEIN: {
data->tran_time += platform_frametime();
if (data->tran_time > 1.0f) {
data->tran_effect = ETRAN_NONE;
data->tran_time = 1.0f;
}
}break;
case ETRAN_FADEOUT: {
data->tran_time -= platform_frametime();
if (data->tran_time < 0.0f) {
data->tran_effect = ETRAN_REMOVE;
data->tran_time = 0.0f;
}
}break;
default: break;
}
}

View File

@ -68,6 +68,8 @@ entity_view *world_build_entity_view(int64_t e) {
if (ecs_get(world_ecs(), e, Device)) {
Device const* dev = ecs_get(world_ecs(), e, Device);
view.asset = dev->asset;
view.progress_active = dev->progress_active;
view.progress_value = dev->progress_value;
}
view.inside_vehicle = ecs_get(world_ecs(), e, IsInVehicle) != 0 ? true : false;

View File

@ -132,6 +132,16 @@ void DEBUG_draw_entities_low(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->progress_active) {
float w = 64.f;
float h = 8.f;
float p = data->progress_value;
float x = data->x - w/2.f;
float y = data->y - 32.f - h;
DrawRectangleEco(x, y, w, h, ColorAlpha(BLACK, data->tran_time));
DrawRectangleEco(x, y, w*p, h, ColorAlpha(GREEN, data->tran_time));
}
}break;
default:break;
}