Tilt a server player's camera towards the hit

The server never sent ClientboundHurtAnimationPacket, and the client
ignored the yaw it carried, so a player on a server got the flash but the
camera never tilted towards where the hit came from.

ServerPlayer.indicateDamage now runs where the hit's knockback is dealt
(and with no direction for hits that have none), the packet goes out when
the guest's turn ends, and the client keeps the yaw as Player.hurtDir for
GameRenderer.bobHurt.
This commit is contained in:
Kacpherek
2026-10-26 13:05:51 +01:00
parent f362f15212
commit 4825e537cc
5 changed files with 41 additions and 2 deletions
@@ -1528,6 +1528,10 @@ impl Game<'_> {
// hurt cooldown (a stronger hit there only adds its difference).
if o.knockback {
let (xd, zd) = (from[0] - player.position.x, from[2] - player.position.z);
// dealDefaultKnockback's `indicateDamage`, unless blocked.
if o.blocked <= 0.0 {
player.health.indicate_damage(xd, zd, player.yaw);
}
self.knock_back_player(f64::from(0.4_f32), xd, zd);
if let Some(p) = self.player.as_mut()
&& let Some(r) = p.hurt_log.last_mut()
+1 -1
View File
@@ -114,7 +114,7 @@ impl Game<'_> {
living: Some(HurtBob {
hurt_time: p.health.hurt_animation_time as f32 - partial,
hurt_duration: hurt.hurt_duration,
hurt_dir: 0.0,
hurt_dir: p.health.hurt_dir,
death_time: self.death_time().map(|t| t as f32 + partial),
}),
player: Some((
@@ -650,6 +650,13 @@ impl Game<'_> {
g.sprinting = false;
}
m.sprinting = false;
// ServerPlayer.indicateDamage: the hit's direction to the player.
if let Some(yaw) = m.health.indicated_dir.take() {
g.send(&C::HurtAnimation(play::HurtAnimation {
id: g.entity_id,
yaw,
}));
}
let teleported = std::mem::take(&mut m.teleported);
let moved: f64 = (0..3).map(|k| (at[k] - g.position[k]).powi(2)).sum();
g.motion = Some(Box::new(m));
+2 -1
View File
@@ -2042,8 +2042,9 @@ impl Game<'_> {
}
C::HurtAnimation(p) => {
if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) {
// Player.animateHurt: the tilt turns around the yaw.
if let Some(pl) = self.player.as_mut() {
pl.health.animate_hurt();
pl.health.animate_hurt_dir(p.yaw);
}
} else if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) {
m.hurt_time = 10;
+27
View File
@@ -163,6 +163,12 @@ pub struct PlayerHealth {
pub hurt: State,
/// Client hurt animation; fully blocked hits only refresh the server's hurt state.
pub hurt_animation_time: i32,
/// `Player.hurtDir` on a client: the yaw `ClientboundHurtAnimationPacket`
/// gave, which `GameRenderer.bobHurt` tilts the camera around.
pub hurt_dir: f32,
/// `ServerPlayer.hurtDir` of a hit the player's client hasn't been told
/// of yet (`indicateDamage`).
pub indicated_dir: Option<f32>,
pub dead: bool,
pub sync_velocity: bool,
pub game_time: i64,
@@ -213,6 +219,8 @@ impl PlayerHealth {
last_stamp: 0,
},
hurt_animation_time: 0,
hurt_dir: 0.0,
indicated_dir: None,
dead: false,
sync_velocity: false,
game_time: 0,
@@ -260,6 +268,18 @@ impl PlayerHealth {
self.hurt.hurt_time = 10;
self.hurt_animation_time = 10;
}
/// `LivingEntity.animateHurt(yaw)` on a player: the tilt restarts around
/// `yaw` (`Player.animateHurt` keeps it as `hurtDir`).
pub fn animate_hurt_dir(&mut self, yaw: f32) {
self.animate_hurt();
self.hurt_dir = yaw;
}
/// `ServerPlayer.indicateDamage`: the hit's direction relative to where
/// the player looks, kept for the packet to its client.
pub fn indicate_damage(&mut self, xd: f64, zd: f64, yaw: f32) {
use horizon_core::geometry::trig::{RAD_TO_DEG, atan2};
self.indicated_dir = Some((atan2(zd, xd) * f64::from(RAD_TO_DEG) - f64::from(yaw)) as f32);
}
/// `LocalPlayer.hurtTo` with `flashOnSetHealth`: a drop below the current
/// health starts the hurt animation; `damageCooldownTime` (20 on a drop,
/// 10 on a gain) is what lets `Hud.extractPlayerHealth` blink the hearts.
@@ -348,6 +368,13 @@ impl PlayerMotion {
if outcome.hurt {
h.hurt_animation_time = h.hurt.hurt_time;
}
// `dealDefaultKnockback` ends in `indicateDamage` unless blocked: a
// hit with no direction of its own (`xd` and `zd` zero) is sent as
// the opposite of the player's yaw. A hit that has a direction
// replaces this (`mob_hits_player`).
if outcome.knockback && outcome.blocked <= 0.0 {
h.indicate_damage(0.0, 0.0, self.yaw);
}
let after = h.health() + h.absorption();
if self.hurt_log.len() >= HURT_LOG {
self.hurt_log.remove(0);