diff --git a/crates/horizon-client/src/game/fov.rs b/crates/horizon-client/src/game/fov.rs index 7825d6514..952a266d8 100644 --- a/crates/horizon-client/src/game/fov.rs +++ b/crates/horizon-client/src/game/fov.rs @@ -106,7 +106,7 @@ impl Game<'_> { let hurt = &p.health.hurt; let state = BobState { living: Some(HurtBob { - hurt_time: hurt.hurt_time as f32 - partial, + hurt_time: p.health.hurt_animation_time as f32 - partial, hurt_duration: hurt.hurt_duration, hurt_dir: 0.0, death_time: self.death_time().map(|t| t as f32 + partial), diff --git a/crates/horizon-client/src/game/perspective.rs b/crates/horizon-client/src/game/perspective.rs index bb463547f..f65a95438 100644 --- a/crates/horizon-client/src/game/perspective.rs +++ b/crates/horizon-client/src/game/perspective.rs @@ -319,7 +319,7 @@ impl Game<'_> { .map(|s| s.item.clone()); let off = self.offhand.as_ref().map(|s| s.item.clone()); let riding = self.riding.is_some(); - let hurt = p.health.hurt.hurt_time; + let hurt = p.health.hurt_animation_time; let Some(m) = self.player_model.as_mut() else { return; }; diff --git a/crates/horizon-client/src/health.rs b/crates/horizon-client/src/health.rs index db0677d5f..493488732 100644 --- a/crates/horizon-client/src/health.rs +++ b/crates/horizon-client/src/health.rs @@ -161,6 +161,8 @@ pub struct PlayerHealth { pub ids: PlayerFloatIds, pub food: FoodData, pub hurt: State, + /// Client hurt animation; fully blocked hits only refresh the server's hurt state. + pub hurt_animation_time: i32, pub dead: bool, pub sync_velocity: bool, pub game_time: i64, @@ -210,6 +212,7 @@ impl PlayerHealth { last_source: None, last_stamp: 0, }, + hurt_animation_time: 0, dead: false, sync_velocity: false, game_time: 0, @@ -242,6 +245,9 @@ impl PlayerHealth { if self.hurt.hurt_time > 0 { self.hurt.hurt_time -= 1; } + if self.hurt_animation_time > 0 { + self.hurt_animation_time -= 1; + } } } impl PlayerMotion { @@ -300,8 +306,11 @@ impl PlayerMotion { sound: environment.block_sound, }; if environment.totem_fired { - self.health.totem_used = true; - self.health.totem_ready = false; + h.totem_used = true; + h.totem_ready = false; + } + if outcome.hurt { + h.hurt_animation_time = h.hurt.hurt_time; } Ok(outcome) } diff --git a/crates/horizon-client/src/hunger.rs b/crates/horizon-client/src/hunger.rs index 90b11f18d..fa1b62a5e 100644 --- a/crates/horizon-client/src/hunger.rs +++ b/crates/horizon-client/src/hunger.rs @@ -55,6 +55,7 @@ impl PlayerMotion { data: &mut h.data, ids: h.ids, hurt: &mut h.hurt, + hurt_animation_time: &mut h.hurt_animation_time, dead: &mut h.dead, sync_velocity: &mut h.sync_velocity, game_time: h.game_time, @@ -70,6 +71,7 @@ struct TickWorld<'a> { data: &'a mut SyncedFloats, ids: PlayerFloatIds, hurt: &'a mut State, + hurt_animation_time: &'a mut i32, dead: &'a mut bool, sync_velocity: &'a mut bool, game_time: i64, @@ -142,8 +144,11 @@ impl FoodWorld for TickWorld<'_> { totem: false, totem_fired: false, }; - ServerPlayerCombat::new(self.attributes, self.data, food, self.ids, &mut environment)? - .hurt(self.hurt, amount)?; + if ServerPlayerCombat::new(self.attributes, self.data, food, self.ids, &mut environment)? + .hurt(self.hurt, amount)? + { + *self.hurt_animation_time = self.hurt.hurt_time; + } Ok(()) } } diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 0eb79d65c..2df66f6a5 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -2166,8 +2166,13 @@ mod tests { #[test] fn player_fire_damage_queues_hurt_sound_only_for_fresh_hits() { - let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3"); + let root = std::env::var_os("HORIZON_TEST_RESOURCES") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3") + }); if !root.join("movement-properties.nbt").exists() { + eprintln!("skipped damage regression: prepared resources unavailable"); return; } let assets = Assets::load(&root).unwrap(); @@ -2195,6 +2200,8 @@ mod tests { .count(), 1 ); + assert_eq!(player.health.hurt_animation_time, 10); + player.health.advance_clock(); player.events.clear(); assert!( !player @@ -2207,9 +2214,14 @@ mod tests { .iter() .any(|e| matches!(e, MotionEvent::HurtSound(_))) ); + assert_eq!( + player.health.hurt_animation_time, 9, + "cooldown hit must not restart animation" + ); for _ in 0..20 { player.health.advance_clock(); } + assert_eq!(player.health.hurt_animation_time, 0); player.invulnerable = true; player.events.clear(); assert!( @@ -2271,16 +2283,62 @@ mod tests { Default::default(), source, 4.0, - Some(shield), + Some(shield.clone()), ) .unwrap(); assert!(!outcome.hurt && outcome.sound && outcome.blocked == 4.0); + assert_eq!( + player.health.hurt.hurt_time, 10, + "preserve server damage bookkeeping" + ); + assert_eq!( + player.health.hurt_animation_time, 0, + "a fully blocked arrow must not animate hurt" + ); assert!( !player .events .iter() .any(|e| matches!(e, MotionEvent::HurtSound(_))) ); + // An unblocked hit after the shield block must still animate normally. + for _ in 0..20 { + player.health.advance_clock(); + } + assert!( + player + .hurt(&resources.health, Default::default(), source, 4.0) + .unwrap() + ); + assert_eq!(player.health.hurt_animation_time, 10); + player.health.advance_clock(); + assert!( + !player + .hurt_blocking( + &resources.health, + Default::default(), + source, + 4.0, + Some(shield) + ) + .unwrap() + .hurt + ); + assert_eq!( + player.health.hurt_animation_time, 9, + "blocking must not erase or restart an existing animation" + ); + let mut starving = PlayerMotion::new(&resources, Vec3::ZERO).unwrap(); + starving.health.food.food = 0; + starving.health.food.timer = 79; + starving + .food_tick(&resources.health, Default::default()) + .unwrap(); + assert_eq!(starving.health.health(), 19.0); + assert_eq!( + starving.health.hurt_animation_time, 10, + "starvation must still animate hurt" + ); } /// Footsteps on the prepared vanilla resources, when they are present: