use bevy::prelude::*;
#[derive(Component, Reflect)]
#[reflect(Component)]
pub struct AnimationLock {
pub timer: Timer,
pub description: String,
}
impl AnimationLock {
pub fn new(duration: f32, description: impl Into<String>) -> Self {
Self {
timer: Timer::from_seconds(duration, TimerMode::Once),
description: description.into(),
}
}
pub fn is_finished(&self) -> bool {
self.timer.is_finished()
}
pub fn remaining(&self) -> f32 {
self.timer.remaining_secs()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn test_animation_lock_new() {
let lock = AnimationLock::new(1.0, "test");
assert_eq!(lock.description, "test");
assert_eq!(lock.timer.duration(), Duration::from_secs_f32(1.0));
assert!(!lock.is_finished());
}
#[test]
fn test_animation_lock_timer() {
let mut lock = AnimationLock::new(0.5, "test");
assert!(!lock.is_finished());
assert!(lock.remaining() > 0.0);
lock.timer.tick(Duration::from_secs_f32(0.3));
assert!(!lock.is_finished());
assert!(lock.remaining() > 0.0);
lock.timer.tick(Duration::from_secs_f32(0.3));
assert!(lock.is_finished());
assert_eq!(lock.remaining(), 0.0);
}
#[test]
fn test_animation_lock_description() {
let lock1 = AnimationLock::new(1.0, "damage");
let lock2 = AnimationLock::new(2.0, String::from("move"));
assert_eq!(lock1.description, "damage");
assert_eq!(lock2.description, "move");
}
}