From ca6c0586750524067d2bcbbad4e559851f6b49be Mon Sep 17 00:00:00 2001 From: jacob Date: Tue, 13 Aug 2024 19:29:03 -0500 Subject: [PATCH] bullet trail testing --- res/graphics/bullet.ase | 4 ++-- src/entity.h | 6 ++++++ src/game.c | 8 +++++++- src/user.c | 27 +++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/res/graphics/bullet.ase b/res/graphics/bullet.ase index 13eb940e..dfc5e1df 100644 --- a/res/graphics/bullet.ase +++ b/res/graphics/bullet.ase @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:45e5aaaab5cced39a4b4eee1cb4dad70f58dc2102d4fdcf9701c082eff41c464 -size 370 +oid sha256:1abb4e1ce9a682baa114c468381371d4d5e805231af6cde7e503ba02fad9dcd0 +size 361 diff --git a/src/entity.h b/src/entity.h index 0c75e344..1a0043c9 100644 --- a/src/entity.h +++ b/src/entity.h @@ -14,6 +14,7 @@ enum entity_prop { ENTITY_PROP_CAMERA, ENTITY_PROP_CAMERA_ACTIVE, + ENTITY_PROP_BULLET, ENTITY_PROP_WEAPON, ENTITY_PROP_TRIGGERING_EQUIPPED, ENTITY_PROP_TRIGGERED_THIS_TICK, @@ -122,6 +123,11 @@ struct entity { f32 trigger_delay; /* Minimum time between triggers */ f32 last_triggered; + /* ====================================================================== */ + /* Bullet */ + + struct entity_handle bullet_src_ent; + /* ====================================================================== */ /* Testing */ diff --git a/src/game.c b/src/game.c index 5fd5861c..ab2dd86a 100644 --- a/src/game.c +++ b/src/game.c @@ -557,15 +557,21 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) struct v2 out_pos = xform_mul_v2(sprite_xform, out_slice.center); struct v2 out_vec = xform_basis_mul_v2(sprite_xform, out_slice.dir); out_vec = v2_norm(out_vec); - out_vec = v2_mul(out_vec, 5); + out_vec = v2_mul(out_vec, 25); { + /* FIXME: Weapon velocity should affect bullet velocity + * (This should also make bullet_src_ent unnecessary for + * drawing bullet trails) */ + /* Spawn bullet */ struct entity *bullet = entity_alloc(root); bullet->sprite = sprite_tag_from_path(STR("res/graphics/bullet.ase")); struct xform bullet_xf = XFORM_POS(out_pos); entity_set_xform(bullet, bullet_xf); bullet->velocity = out_vec; + bullet->bullet_src_ent = ent->handle; + entity_enable_prop(bullet, ENTITY_PROP_BULLET); } } } diff --git a/src/user.c b/src/user.c index 6d105432..d99f47b6 100644 --- a/src/user.c +++ b/src/user.c @@ -809,6 +809,33 @@ INTERNAL void user_update(void) struct xform sprite_xform = xf; + /* Draw bullet trail */ + if (entity_has_prop(ent, ENTITY_PROP_BULLET)) { + f32 max_len = 0.75; + + f32 thickness = 0.0025; + u32 color = RGBA_32_F(1, 1, 1, 1); + + struct v2 end = xf.og; + + struct v2 start; + struct entity *src = entity_from_handle(store, ent->bullet_src_ent); + if (src->valid) { + start = xform_mul_v2(src->sprite_local_xform, sprite_sheet_get_slice(sprite_sheet_from_tag_await(sprite_frame_scope, src->sprite), STR("out"), src->animation_frame).center); + start = xform_mul_v2(entity_get_xform(src), start); + } else { + start = v2_sub(end, v2_div(ent->velocity, 50)); + } + + struct v2 rel = v2_sub(start, end); + if (v2_len(rel) > max_len) { + rel = v2_mul(v2_norm(rel), max_len); + start = v2_add(end, rel); + } + + draw_solid_line(G.world_canvas, start, end, thickness, color); + } + /* Draw sprite */ if (!sprite_tag_is_nil(sprite)) { /* Calculate sprite xform */