correct bullet starting point

This commit is contained in:
jacob 2024-08-14 18:40:00 -05:00
parent 38075e5efd
commit 9424c9c2c9
2 changed files with 39 additions and 39 deletions

View File

@ -138,6 +138,9 @@ struct entity {
/* Bullet */ /* Bullet */
struct entity_handle bullet_src; struct entity_handle bullet_src;
struct v2 bullet_src_pos;
struct v2 bullet_src_dir;
f32 bullet_impulse;
/* ====================================================================== */ /* ====================================================================== */
/* Testing */ /* Testing */

View File

@ -156,8 +156,6 @@ INTERNAL void spawn_test_entities(void)
e->trigger_delay = 1.0 / 10.0; e->trigger_delay = 1.0 / 10.0;
player_ent->equipped = e->handle; player_ent->equipped = e->handle;
//entity_enable_prop(e, ENTITY_PROP_KINEMATIC);
} }
/* Camera */ /* Camera */
@ -490,55 +488,27 @@ INTERNAL void game_update(struct game_cmd_array game_cmds)
/* Fire weapon */ /* Fire weapon */
if (entity_has_prop(ent, ENTITY_PROP_WEAPON)) { if (entity_has_prop(ent, ENTITY_PROP_WEAPON)) {
struct xform xf = entity_get_xform(ent);
struct sprite_tag sprite = ent->sprite; struct sprite_tag sprite = ent->sprite;
u32 animation_frame = ent->animation_frame; 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 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 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 rel_pos = xform_mul_v2(sprite_local_xform, out_slice.center);
struct v2 out_vec = xform_basis_mul_v2(sprite_xform, out_slice.dir); struct v2 rel_dir = xform_basis_mul_v2(sprite_local_xform, out_slice.dir);
out_vec = v2_norm(out_vec);
out_vec = v2_mul(out_vec, 25);
{ {
/* FIXME: Weapon velocity should affect bullet velocity /* Spawn bullet */
* (This should also make bullet_src_ent unnecessary for
* drawing bullet trails) */
#if 1
/* Spawn bullet */
struct entity *bullet = entity_alloc(root); struct entity *bullet = entity_alloc(root);
bullet->sprite = sprite_tag_from_path(STR("res/graphics/bullet.ase")); 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->bullet_src = ent->handle;
bullet->activation_tick = G.tick.tick_id + 1; bullet->bullet_src_pos = rel_pos;
bullet->velocity = barrel_velocity; bullet->bullet_src_dir = rel_dir;
bullet->bullet_impulse = 25;
/* Create impulse */ entity_enable_prop(bullet, ENTITY_PROP_BULLET);
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
} }
} }
} }
@ -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 * Update camera position
* ========================== */ * ========================== */