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 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-12 13:29:20 +02:00
parent 5b201965af
commit d48017ab70
2 changed files with 42 additions and 0 deletions
@@ -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<HeldStack> {
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));
}
}
+1
View File
@@ -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;