cranium_core/
action_state.rs1use bevy::prelude::*;
10use bevy::{platform::collections::Equivalent, reflect::Reflect};
11
12use crate::{types, action_runtime::ActionTrackerState};
13
14#[cfg(any(feature = "actionset_loader"))]
15use serde::{Deserialize, Serialize};
16
17#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Reflect)]
29#[cfg_attr(any(feature = "actionset_loader"), derive(Serialize, Deserialize))]
30pub enum ActionState {
31 Queued,
36 Ready,
38
39 Running,
41 Paused,
43
44 Succeeded,
46
47 Failed,
52
53 Cancelled,
59}
60
61impl ActionState {
62 pub fn is_initial(&self) -> bool {
64 match self {
65 Self::Queued => true,
66 Self::Ready => true,
67 _ => false,
68 }
69 }
70
71 pub fn is_progressed(&self) -> bool {
73 match self {
74 Self::Running => true,
75 Self::Paused => true,
76 _ => false,
77 }
78 }
79
80 pub fn is_terminal(&self) -> bool {
82 match self {
83 Self::Succeeded => true,
84 Self::Failed => true,
85 Self::Cancelled => true,
86 _ => false,
87 }
88 }
89
90 pub fn should_process(&self) -> bool {
92 match self {
93 Self::Ready => true,
94 Self::Running => true,
95 _ => false,
96 }
97 }
98}
99
100impl Equivalent<ActionState> for &ActionState {
101 fn equivalent(&self, key: &ActionState) -> bool {
102 self == &key
103 }
104}
105
106
107#[derive(Message)]
109pub struct AiActionStateChangeRequest {
110 pub entity: Entity,
111 pub action: types::ActionKey,
112 pub to_state: crate::action_state::ActionState,
113}
114
115
116#[derive(EntityEvent)]
118pub struct AiActionStateChange {
119 pub entity: Entity,
120 pub action: types::ActionKey,
121 pub from_state: Option<crate::action_state::ActionState>,
122 pub to_state: crate::action_state::ActionState,
123}
124
125pub fn action_state_update_handler(
128 mut request_reader: MessageReader<AiActionStateChangeRequest>,
129 mut tracker_state_qry: Query<&mut ActionTrackerState>,
130 mut commands: Commands,
131) {
132 request_reader.read().for_each(|msg| {
133 let maybe_tracker_state = tracker_state_qry.get_mut(msg.entity);
134
135 match maybe_tracker_state {
136 Err(err) => {
137 #[cfg(feature = "logging")]
138 bevy::log::debug!("{:?}: ActionTracker does not exist: {:?}", &msg.action, err);
139 match commands.get_entity(msg.entity) {
140 Err(err) => {
141 #[cfg(feature = "logging")]
142 bevy::log::error!("{:?}: AI {:?} does not exist??? - {:?}", &msg.action, msg.entity, err);
143 }
144 Ok(mut cmds) => {
145 #[cfg(feature = "logging")]
146 bevy::log::debug!("{:?}: Inserting new ActionState for AI {:?} - {:?}", &msg.action, msg.entity, &msg.to_state);
147 cmds.trigger(|ent| AiActionStateChange {
148 action: msg.action.clone(),
149 entity: ent,
150 from_state: None,
151 to_state: msg.to_state.clone(),
152 });
153 cmds.insert(ActionTrackerState(msg.to_state));
154 }
155 }
156 }
157 Ok(mut state) => {
158 #[cfg(feature = "logging")]
159 bevy::log::debug!("example_action for AI {:?}: Updating the state to new value {:?}", msg.entity, msg.to_state);
160 let current = state.get_state().clone();
161 commands.trigger(AiActionStateChange {
162 action: msg.action.clone(),
163 entity: msg.entity,
164 from_state: Some(current),
165 to_state: msg.to_state.clone(),
166 });
167 state.set_state(msg.to_state);
168 },
169 }
170 });
171}
172
173
174#[derive(Event)]
176pub struct ProcessActionStateUpdatesSignal;
177
178
179pub fn action_state_update_handler_observer(
183 _trigger: On<ProcessActionStateUpdatesSignal>,
184 request_reader: MessageReader<AiActionStateChangeRequest>,
185 tracker_state_qry: Query<&mut ActionTrackerState>,
186 commands: Commands,
187) {
188 action_state_update_handler(request_reader, tracker_state_qry, commands);
189}
190
191
192pub struct ActionStateUpdatesPlugin;
193
194impl Plugin for ActionStateUpdatesPlugin {
195 fn build(&self, app: &mut App) {
196 app
197 .add_message::<AiActionStateChangeRequest>()
198 .add_observer(action_state_update_handler_observer)
199 .add_systems(FixedUpdate, crate::action_state::action_state_update_handler)
200 ;
201 }
202}