From 40fce014f1532dd090bb2aa0074b15e851493aa2 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 14 Oct 2026 19:14:46 +0200 Subject: [PATCH] Controllers: hurt rumble only within one player's life The health baseline (HealthWatch) is forgotten when there is no player, on the title screen, and whenever a new player entity takes over (spawn and respawn, joining a server, End and Nether dimension changes), so arriving with less health than the last player had isn't damage. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/game/end.rs | 1 + .../horizon-client/src/game/nether_portal.rs | 1 + crates/horizon-client/src/game/pad.rs | 27 ++++++++----- crates/horizon-client/src/game/remote.rs | 1 + crates/horizon-client/src/gamepad.rs | 38 +++++++++++++++++++ 6 files changed, 59 insertions(+), 10 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 6fbabb375..5b9dac9ec 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2653,6 +2653,7 @@ impl Game<'_> { } self.previous_position = feet; self.player = Some(player); + self.pad.player_replaced(); self.sway = Default::default(); self.apply_equipment(); self.eye = 1.62; diff --git a/crates/horizon-client/src/game/end.rs b/crates/horizon-client/src/game/end.rs index fa60228de..e6be63093 100644 --- a/crates/horizon-client/src/game/end.rs +++ b/crates/horizon-client/src/game/end.rs @@ -485,6 +485,7 @@ impl Game<'_> { player.fall_distance = 0.0; self.previous_position = player.position; self.player = Some(player); + self.pad.player_replaced(); self.sway = Default::default(); self.apply_equipment(); let pitch = self.world.random.next_float() * 0.4 + 0.8; diff --git a/crates/horizon-client/src/game/nether_portal.rs b/crates/horizon-client/src/game/nether_portal.rs index a6296d09b..829f1e6ab 100644 --- a/crates/horizon-client/src/game/nether_portal.rs +++ b/crates/horizon-client/src/game/nether_portal.rs @@ -361,6 +361,7 @@ impl Game<'_> { player.velocity = rotate_y(player.velocity, -rotation); self.previous_position = player.position; self.player = Some(player); + self.pad.player_replaced(); self.sway = Default::default(); self.apply_equipment(); let pitch = self.world.random.next_float() * 0.4 + 0.8; diff --git a/crates/horizon-client/src/game/pad.rs b/crates/horizon-client/src/game/pad.rs index 1c472dc31..2f050fdec 100644 --- a/crates/horizon-client/src/game/pad.rs +++ b/crates/horizon-client/src/game/pad.rs @@ -6,7 +6,7 @@ use super::{Game, Screen}; use horizon_client::{ Result, - gamepad::{self, Button, Edges, GlyphStyle, PadState, Rumble}, + gamepad::{self, Button, Edges, GlyphStyle, HealthWatch, PadState, Rumble}, keybinds::{KeyBinds, key_code, mouse_button}, mouse::Rotation, }; @@ -52,8 +52,8 @@ pub(super) struct Pads { rumbles: Vec<(gilrs::ff::Effect, Instant)>, /// The last vibration asked for, for `pad status`. last_rumble: Option<(&'static str, gamepad::Pulse)>, - /// The player's health at the last poll (a drop is damage taken). - last_health: Option, + /// The player's health between polls (a drop is damage taken). + health: HealthWatch, } fn gilrs_button(button: Button) -> gilrs::Button { @@ -79,6 +79,11 @@ fn gilrs_button(button: Button) -> gilrs::Button { } impl Pads { + /// A new player entity took over (spawn, respawn, a server joined, a + /// dimension change): its health is not compared with the old one's. + pub(super) fn player_replaced(&mut self) { + self.health.forget(); + } /// Open gilrs on first use. A platform without it (or a Linux without /// udev) leaves controllers off; the game runs as before. fn start(&mut self) { @@ -187,14 +192,16 @@ impl Game<'_> { self.pad.in_use = true; } self.pad.state = state; - // Damage from any source, here or on a server. - let health = self.player.as_ref().map(|p| p.health.health()); - if let (Some(before), Some(now)) = (self.pad.last_health, health) - && now < before - { - self.pad_rumble(Rumble::Hurt(before - now)); + // Damage from any source, here or on a server; nothing counts on + // the title screen. + let health = self + .player + .as_ref() + .filter(|_| self.title.is_none()) + .map(|p| p.health.health()); + if let Some(damage) = self.pad.health.observe(health) { + self.pad_rumble(Rumble::Hurt(damage)); } - self.pad.last_health = health; let now = Instant::now(); self.pad.rumbles.retain(|(_, end)| *end > now); let ticking = self.title.is_none() && self.screen == Screen::Playing && !self.pause; diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 703541c5b..73edbaca6 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1571,6 +1571,7 @@ impl Game<'_> { let mut player = PlayerMotion::new(self.motion, position)?; player.velocity = delta; self.player = Some(player); + self.pad.player_replaced(); self.sway = Default::default(); self.eye = 1.62; self.previous_eye = 1.62; diff --git a/crates/horizon-client/src/gamepad.rs b/crates/horizon-client/src/gamepad.rs index 0cfed55e9..6ec425c82 100644 --- a/crates/horizon-client/src/gamepad.rs +++ b/crates/horizon-client/src/gamepad.rs @@ -589,6 +589,29 @@ pub fn nearest(at: (f32, f32), targets: &[(f32, f32)], radius: f32) -> Option, +} +impl HealthWatch { + /// The health now (`None` without a player); returns the damage taken + /// since the last call. + pub fn observe(&mut self, health: Option) -> Option { + let before = std::mem::replace(&mut self.last, health); + match (before, health) { + (Some(before), Some(now)) if now < before => Some(before - now), + _ => None, + } + } + pub fn forget(&mut self) { + self.last = None; + } +} + /// What makes the pad vibrate (Bedrock-like: short, light pulses). #[derive(Clone, Copy, Debug, PartialEq)] pub enum Rumble { @@ -821,6 +844,21 @@ mod tests { assert_eq!(action_caption("key.jump"), "key.jump"); } + #[test] + fn health_watch_only_counts_damage_in_one_life() { + let mut w = HealthWatch::default(); + assert_eq!(w.observe(Some(20.0)), None); + assert_eq!(w.observe(Some(15.0)), Some(5.0)); + assert_eq!(w.observe(Some(18.0)), None, "healing"); + // A new player entity (world, respawn, server, dimension). + w.forget(); + assert_eq!(w.observe(Some(6.0)), None); + // No player in between (title, loading) forgets too. + assert_eq!(w.observe(None), None); + assert_eq!(w.observe(Some(2.0)), None); + assert_eq!(w.observe(Some(1.0)), Some(1.0)); + } + #[test] fn rumble_pulses() { let small = Rumble::Hurt(1.0).pulse().unwrap();