feat: Animated sprite

This commit is contained in:
Alexey 2026-04-20 09:34:40 +03:00
commit 4646a27978
3 changed files with 80 additions and 0 deletions

3
src/anim/mod.rs Normal file
View file

@ -0,0 +1,3 @@
//! Animation-related components
pub mod sprite;

76
src/anim/sprite.rs Normal file
View file

@ -0,0 +1,76 @@
//! Animated sprite module
use std::{collections::HashMap, time::Duration};
use bevy::prelude::*;
use crate::timer::duration_from_bpm;
/// Data for [AnimatedSprite]
#[derive(Clone, PartialEq, Debug, Reflect)]
#[reflect(Clone, PartialEq, Debug)]
pub struct AnimationData {
/// Handle to sprite image
pub image: Handle<Image>,
/// Animation duration in beats
pub duration: f32,
}
impl AnimationData {
/// Construct new [AnimationData]
pub fn new(image: Handle<Image>, duration: f32) -> Self {
Self { image, duration }
}
}
/// Animated sprite with states
#[derive(Clone, PartialEq, Debug, Reflect)]
#[reflect(Clone, PartialEq, Debug, Default)]
pub struct AnimatedSprite {
data: HashMap<String, AnimationData>,
current: Option<AnimationData>,
timer: Timer,
bpm: Duration,
}
impl Default for AnimatedSprite {
fn default() -> Self {
Self {
data: HashMap::new(),
current: None,
timer: Timer::new(Duration::default(), TimerMode::Repeating),
bpm: Duration::default(),
}
}
}
impl AnimatedSprite {
/// Add new animation to sprite
pub fn add(&mut self, data: AnimationData, label: String) {
self.data.insert(label, data);
}
/// Sets internal BPM state
pub fn set_bpm(&mut self, bpm: f32) {
self.bpm = duration_from_bpm(bpm);
}
/// Finds animation by its label and returns true if it started playing
pub fn play(&mut self, label: &str, mode: TimerMode) -> bool {
match self.data.get(label) {
Some(data) => {
self.current = Some(data.clone());
self.timer.set_duration(self.bpm.mul_f32(data.duration));
self.timer.set_mode(mode);
true
},
None => false,
}
}
/// Updates animation and returns true if animation should stop
pub fn tick(&mut self, dt: Duration) -> bool {
self.timer.tick(dt).just_finished()
}
}

View file

@ -5,6 +5,7 @@
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub mod anim;
pub mod combat; pub mod combat;
pub mod graph; pub mod graph;
pub mod input; pub mod input;