Skip to main content

gizmo_animation/
player.rs

1use std::sync::Arc;
2use crate::clip::AnimationClip;
3use std::collections::HashMap;
4use gizmo_core::entity::Entity;
5
6#[derive(Clone, Copy, Debug, Default)]
7pub struct Animated;
8
9impl gizmo_core::component::Component for Animated {
10    fn storage_type() -> gizmo_core::component::StorageType {
11        gizmo_core::component::StorageType::Table
12    }
13}
14
15
16#[derive(Clone)]
17pub struct AnimationPlayer {
18    pub clip: Option<Arc<AnimationClip>>,
19    pub elapsed_time: f32,
20    pub speed: f32,
21    pub playing: bool,
22    pub looping: bool,
23    pub target_entities: HashMap<String, Entity>,
24}
25
26impl Default for AnimationPlayer {
27    fn default() -> Self {
28        Self {
29            clip: None,
30            elapsed_time: 0.0,
31            speed: 1.0,
32            playing: true,
33            looping: true,
34            target_entities: HashMap::new(),
35        }
36    }
37}
38
39impl gizmo_core::component::Component for AnimationPlayer {
40    fn storage_type() -> gizmo_core::component::StorageType {
41        gizmo_core::component::StorageType::Table
42    }
43}
44
45impl AnimationPlayer {
46    pub fn new() -> Self {
47        Self::default()
48    }
49
50    pub fn play(&mut self, clip: Arc<AnimationClip>) -> &mut Self {
51        self.clip = Some(clip);
52        self.elapsed_time = 0.0;
53        self.playing = true;
54        self.target_entities.clear(); // Need to re-resolve targets for the new clip
55        self
56    }
57
58    pub fn pause(&mut self) -> &mut Self {
59        self.playing = false;
60        self
61    }
62
63    pub fn resume(&mut self) -> &mut Self {
64        self.playing = true;
65        self
66    }
67
68    pub fn with_speed(mut self, speed: f32) -> Self {
69        self.speed = speed;
70        self
71    }
72
73    pub fn looping(mut self, looping: bool) -> Self {
74        self.looping = looping;
75        self
76    }
77}