Skip to content

Commit

Permalink
Fix player contact knockback
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Jul 29, 2024
1 parent 7586143 commit 925cca3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/game/actor/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn enemy(key: impl Into<String>) -> impl EntityCommand {
let config = r!(config.get());
let actor = r!(config.enemies.get(&key)).clone();

(actor, game_root.players, assets.sfx_kick.clone())
(actor, game_root.enemies, assets.sfx_kick.clone())
};

world
Expand Down
2 changes: 1 addition & 1 deletion src/game/actor/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn player(key: impl Into<String>) -> impl EntityCommand {
// Contact hitbox was for testing, but it's funny, so I'm leaving it in.
Hitbox,
HitboxDamage(0.0),
HitboxKnockback(0.0),
HitboxKnockback(150.0, false),
HurtSfx(sfx_hurt, 1.8),
DeathSfx(sfx_death, 1.0),
))
Expand Down
2 changes: 1 addition & 1 deletion src/game/combat/damage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ fn apply_hitbox_damage(
hitbox_query: Query<&HitboxDamage>,
) {
let &OnHit(hitbox, hurtbox) = trigger.event();
let damage = r!(hitbox_query.get(hitbox));
let damage = rq!(hitbox_query.get(hitbox));
commands.entity(hurtbox).trigger(OnDamage(damage.0));
}
2 changes: 1 addition & 1 deletion src/game/combat/hit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Configure for HurtSfx {
}

fn play_hurt_sfx(trigger: Trigger<OnHit>, sfx_query: Query<&HurtSfx>, audio: Res<Audio>) {
let sfx = r!(sfx_query.get(trigger.event().1));
let sfx = rq!(sfx_query.get(trigger.event().1));
audio
.play(sfx.0.clone())
.with_volume(sfx.1)
Expand Down
18 changes: 14 additions & 4 deletions src/game/combat/knockback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(super) fn plugin(app: &mut App) {
/// Scales with projectile speed.
#[derive(Component, Reflect)]
#[reflect(Component)]
pub struct HitboxKnockback(pub f32);
pub struct HitboxKnockback(pub f32, pub bool);

impl Configure for HitboxKnockback {
fn configure(app: &mut App) {
Expand All @@ -24,11 +24,21 @@ fn apply_hitbox_knockback(
trigger: Trigger<OnHit>,
knockback_query: Query<&HitboxKnockback>,
mut velocity_query: Query<&mut LinearVelocity>,
gt_query: Query<&GlobalTransform>,
) {
let &OnHit(hitbox, hurtbox) = trigger.event();
let hitbox_velocity = r!(velocity_query.get(hitbox)).0;
let knockback = hitbox_velocity * r!(knockback_query.get(hitbox)).0;
let mut velocity = r!(velocity_query.get_mut(hurtbox));
let knockback = r!(knockback_query.get(hitbox));

let knockback = if knockback.1 {
let hitbox_velocity = r!(velocity_query.get(hitbox)).0;
hitbox_velocity * knockback.0
} else {
let hitbox_pos = r!(gt_query.get(hitbox)).translation().xy();
let hurtbox_pos = r!(gt_query.get(hurtbox)).translation().xy();
let direction = r!(Dir2::new(hurtbox_pos - hitbox_pos));
direction * knockback.0
};

let mut velocity = r!(velocity_query.get_mut(hurtbox));
velocity.0 += knockback;
}
2 changes: 1 addition & 1 deletion src/game/combat/projectile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub fn projectile(
(
Hitbox,
HitboxDamage(power * projectile.damage),
HitboxKnockback(power * projectile.knockback),
HitboxKnockback(power * projectile.knockback, true),
),
// TODO: Additional cleanup conditions that could be added: entity cap.
// Cleanup:
Expand Down

0 comments on commit 925cca3

Please sign in to comment.