goud_engine/ecs/components/animation_controller/
ops.rs1use std::collections::HashMap;
4
5use crate::ecs::components::AnimationClip;
6
7use super::types::{
8 AnimParam, AnimationController, AnimationState, AnimationTransition, TransitionCondition,
9};
10
11impl AnimationController {
12 #[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 #[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 #[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 #[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 #[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 #[inline]
74 pub fn get_param(&self, name: &str) -> Option<&AnimParam> {
75 self.parameters.get(name)
76 }
77
78 #[inline]
80 pub fn current_state_name(&self) -> &str {
81 &self.current_state
82 }
83
84 #[inline]
87 pub fn current_clip(&self) -> Option<&AnimationClip> {
88 self.states.get(&self.current_state).map(|s| &s.clip)
89 }
90}