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 <noreply@anthropic.com>
This commit is contained in:
committed by
Ludovic Marie
parent
ccdc4fe682
commit
40fce014f1
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<f32>,
|
||||
/// 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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -589,6 +589,29 @@ pub fn nearest(at: (f32, f32), targets: &[(f32, f32)], radius: f32) -> Option<us
|
||||
.map(|(i, _)| i)
|
||||
}
|
||||
|
||||
/// Damage taken, from the player's health between polls. The baseline is
|
||||
/// forgotten whenever there is no player or a new player entity replaces
|
||||
/// the old one (a world loaded, a respawn, a server joined, a dimension
|
||||
/// change), so arriving with less health than before isn't damage.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct HealthWatch {
|
||||
last: Option<f32>,
|
||||
}
|
||||
impl HealthWatch {
|
||||
/// The health now (`None` without a player); returns the damage taken
|
||||
/// since the last call.
|
||||
pub fn observe(&mut self, health: Option<f32>) -> Option<f32> {
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user