From 9424c9c2c9ca1148daf524bb8dccac8031cca898 Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 14 Aug 2024 18:40:00 -0500 Subject: [PATCH] correct bullet starting point --- src/entity.h | 3 +++ src/game.c | 75 +++++++++++++++++++++++++--------------------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/entity.h b/src/entity.h index 7fa59ab3..9985eb8d 100644 --- a/src/entity.h +++ b/src/entity.h @@ -138,6 +138,9 @@ struct entity { /* Bullet */ struct entity_handle bullet_src; + struct v2 bullet_src_pos; + struct v2 bullet_src_dir; + f32 bullet_impulse; /* ====================================================================== */ /* Testing */ diff --git a/src/game.c b/src/game.c index b809c2f5..63a3991e 100644 --- a/src/game.c +++ b/src/game.c @@ -156,8 +156,6 @@ INTERNAL void spawn_test_entities(void) e->trigger_delay = 1.0 / 10.0; player_ent->equipped = e->handle; - - //entity_enable_prop(e, ENTITY_PROP_KINEMATIC); } /* Camera */ @@ -490,55 +488,27 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) /* Fire weapon */ if (entity_has_prop(ent, ENTITY_PROP_WEAPON)) { - struct xform xf = entity_get_xform(ent); - struct sprite_tag sprite = ent->sprite; u32 animation_frame = ent->animation_frame; - struct xform sprite_xform = xform_mul(xf, ent->sprite_local_xform); struct sprite_sheet *sheet = sprite_sheet_from_tag_await(sprite_frame_scope, sprite); + struct xform sprite_local_xform = ent->sprite_local_xform; + struct sprite_sheet_slice out_slice = sprite_sheet_get_slice(sheet, STR("out"), animation_frame); - 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, 25); + struct v2 rel_pos = xform_mul_v2(sprite_local_xform, out_slice.center); + struct v2 rel_dir = xform_basis_mul_v2(sprite_local_xform, out_slice.dir); { - /* FIXME: Weapon velocity should affect bullet velocity - * (This should also make bullet_src_ent unnecessary for - * drawing bullet trails) */ - -#if 1 - /* Spawn bullet */ + /* Spawn bullet */ struct entity *bullet = entity_alloc(root); bullet->sprite = sprite_tag_from_path(STR("res/graphics/bullet.ase")); - struct xform bullet_xf = XFORM_TRS(.t = out_pos, .r = v2_angle(out_vec) + PI / 2); - entity_set_xform(bullet, bullet_xf); - bullet->velocity = out_vec; - bullet->bullet_src = ent->handle; - entity_enable_prop(bullet, ENTITY_PROP_BULLET); -#elif 1 - /* TODO: Remove this (testing) */ - struct v2 barrel_velocity = entity_from_handle(store, ent->parent)->velocity; - /* Create bullet */ - /* TODO: Move bullet infront of barrel by hitbox */ - struct entity *bullet = entity_alloc(root); - bullet->sprite = sprite_tag_from_path(STR("res/graphics/bullet.ase")); - struct xform bullet_xf = XFORM_TRS(.t = out_pos, .r = v2_angle(out_vec) + PI / 2); - entity_set_xform(bullet, bullet_xf); - entity_enable_prop(bullet, ENTITY_PROP_BULLET); bullet->bullet_src = ent->handle; - bullet->activation_tick = G.tick.tick_id + 1; - bullet->velocity = barrel_velocity; + bullet->bullet_src_pos = rel_pos; + bullet->bullet_src_dir = rel_dir; + bullet->bullet_impulse = 25; - /* Create impulse */ - struct entity *impulse = entity_alloc(root); - entity_enable_prop(impulse, ENTITY_PROP_IMPULSE); - impulse->impulse_target = bullet->handle; - impulse->impulse_vec = out_vec; - impulse->activation_tick = bullet->activation_tick + 1; /* Ensure bullet exists for 1 tick at the start of the barrel before impulse is applied */ -#endif + entity_enable_prop(bullet, ENTITY_PROP_BULLET); } } } @@ -633,6 +603,33 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) } } + /* ========================== * + * Initialize bullet kinematics from sources + * ========================== */ + + for (u64 entity_index = 0; entity_index < store->reserved; ++entity_index) { + struct entity *ent = &store->entities[entity_index]; + if (!(ent->valid && entity_has_prop(ent, ENTITY_PROP_ACTIVE))) continue; + + /* FIXME: Apply src entity velocity to bullet velocity */ + + if (entity_has_prop(ent, ENTITY_PROP_BULLET) && !entity_has_prop(ent, ENTITY_PROP_KINEMATIC)) { + struct entity *src = entity_from_handle(store, ent->bullet_src); + struct xform src_xf = entity_get_xform(src); + + struct v2 pos = xform_mul_v2(src_xf, ent->bullet_src_pos); + struct v2 vec = xform_basis_mul_v2(src_xf, ent->bullet_src_dir); + vec = v2_mul(v2_norm(vec), ent->bullet_impulse); + + struct xform xf = XFORM_TRS(.t = pos, .r = v2_angle(vec) + PI / 2); + entity_set_xform(ent, xf); + + ent->velocity = vec; + + entity_enable_prop(ent, ENTITY_PROP_KINEMATIC); + } + } + /* ========================== * * Update camera position * ========================== */