Skip to main content

goud_engine/ecs/components/animation_controller/
ops.rs

1//! Builder and helper methods for [`AnimationController`].
2
3use std::collections::HashMap;
4
5use crate::ecs::components::AnimationClip;
6
7use super::types::{
8    AnimParam, AnimationController, AnimationState, AnimationTransition, TransitionCondition,
9};
10
11impl AnimationController {
12    /// Creates a new controller with the given initial state name.
13    ///
14    /// The controller starts with no states, transitions, or parameters.
15    /// Use the builder methods to add them.
16    ///
17    /// **Precondition:** `initial_state` should name a state that will be
18    /// registered via [`with_state`](Self::with_state) before the controller
19    /// is used. If the state is never added, lookups such as
20    /// [`current_clip`](Self::current_clip) will return `None`.
21    #[inline]
22    pub fn new(initial_state: &str) -> Self {
23        Self {
24            states: HashMap::new(),
25            transitions: Vec::new(),
26            parameters: HashMap::new(),
27            current_state: initial_state.to_string(),
28            transition_progress: None,
29        }
30    }
31
32    /// Adds an animation state with the given name and clip (builder pattern).
33    #[inline]
34    pub fn with_state(mut self, name: &str, clip: AnimationClip) -> Self {
35        self.states
36            .insert(name.to_string(), AnimationState { clip });
37        self
38    }
39
40    /// Adds a transition between two states (builder pattern).
41    #[inline]
42    pub fn with_transition(
43        mut self,
44        from: &str,
45        to: &str,
46        blend_duration: f32,
47        conditions: Vec<TransitionCondition>,
48    ) -> Self {
49        self.transitions.push(AnimationTransition {
50            from: from.to_string(),
51            to: to.to_string(),
52            conditions,
53            blend_duration,
54        });
55        self
56    }
57
58    /// Sets a boolean parameter value.
59    #[inline]
60    pub fn set_bool(&mut self, name: &str, value: bool) {
61        self.parameters
62            .insert(name.to_string(), AnimParam::Bool(value));
63    }
64
65    /// Sets a float parameter value.
66    #[inline]
67    pub fn set_float(&mut self, name: &str, value: f32) {
68        self.parameters
69            .insert(name.to_string(), AnimParam::Float(value));
70    }
71
72    /// Returns a reference to the parameter with the given name, if it exists.
73    #[inline]
74    pub fn get_param(&self, name: &str) -> Option<&AnimParam> {
75        self.parameters.get(name)
76    }
77
78    /// Returns the name of the current state.
79    #[inline]
80    pub fn current_state_name(&self) -> &str {
81        &self.current_state
82    }
83
84    /// Returns a reference to the current state's animation clip, if the
85    /// current state exists in the states map.
86    #[inline]
87    pub fn current_clip(&self) -> Option<&AnimationClip> {
88        self.states.get(&self.current_state).map(|s| &s.clip)
89    }
90}