From d48017ab700147c91226ae14b1f3a0762e350fa4 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 12 Oct 2026 13:29:20 +0200 Subject: [PATCH] Build the player's attack strength model with its tests Player.attackStrengthTicker and itemSwapTicker: both advance each tick, a different main-hand item resets them, and getAttackStrengthScale divides by the ATTACK_SPEED delay. The game does not drive it yet; the attack indicator still shows full strength until combat uses the cooldown. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/attack_strength.rs | 41 ++++++++++++++++++++ crates/horizon-client/src/lib.rs | 1 + 2 files changed, 42 insertions(+) diff --git a/crates/horizon-client/src/attack_strength.rs b/crates/horizon-client/src/attack_strength.rs index 1862492bb..f8d8d5c34 100644 --- a/crates/horizon-client/src/attack_strength.rs +++ b/crates/horizon-client/src/attack_strength.rs @@ -63,3 +63,44 @@ impl AttackStrength { ((self.swap_ticker as f32 + a) / delay(attack_speed)).clamp(0.0, 1.0) } } + +#[cfg(test)] +mod tests { + use super::*; + fn held(item: &str, rest: &str) -> Option { + Some(HeldStack { + item: item.into(), + rest: rest.into(), + }) + } + #[test] + fn charges_over_the_attack_delay() { + let mut s = AttackStrength::default(); + // A sword's 1.6 attacks per second: 12.5 ticks to full strength. + assert_eq!(delay(1.6), 12.5); + for _ in 0..6 { + s.tick(None); + } + assert_eq!(s.scale(0.25, 1.6), 6.25 / 12.5); + for _ in 0..10 { + s.tick(None); + } + assert_eq!(s.scale(0.0, 1.6), 1.0); + } + #[test] + fn switching_items_resets_but_restacking_does_not() { + let mut s = AttackStrength::default(); + s.tick(held("minecraft:stone", "64")); + assert_eq!(s.ticker, 0, "picking up an item resets"); + s.tick(held("minecraft:stone", "63")); + assert_eq!( + s.ticker, 1, + "the same item with another count keeps charging" + ); + s.tick(held("minecraft:diamond_sword", "")); + assert_eq!((s.ticker, s.swap_ticker), (0, 0)); + s.tick(held("minecraft:diamond_sword", "")); + s.reset_attack(); + assert_eq!((s.ticker, s.swap_ticker), (0, 1)); + } +} diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index ea6278878..d80ed6be6 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -6,6 +6,7 @@ pub mod ambience; pub mod animate_tick; pub mod assets; pub mod atlas_sources; +pub mod attack_strength; pub mod attribute_updates; pub mod audio; pub mod auto_jump;