bevy_sequential_actions/
commands.rs

1use super::*;
2
3impl ActionsProxy for Commands<'_, '_> {
4    fn actions(&mut self, agent: Entity) -> impl ModifyActions {
5        AgentCommands {
6            agent,
7            config: AddConfig::default(),
8            commands: self,
9        }
10    }
11}
12
13/// Modify actions using [`Commands`].
14pub struct AgentCommands<'c, 'w, 's> {
15    agent: Entity,
16    config: AddConfig,
17    commands: &'c mut Commands<'w, 's>,
18}
19
20impl ModifyActions for AgentCommands<'_, '_, '_> {
21    fn config(&mut self, config: AddConfig) -> &mut Self {
22        self.config = config;
23        self
24    }
25
26    fn start(&mut self, start: bool) -> &mut Self {
27        self.config.start = start;
28        self
29    }
30
31    fn order(&mut self, order: AddOrder) -> &mut Self {
32        self.config.order = order;
33        self
34    }
35
36    fn add(&mut self, action: impl IntoBoxedActions) -> &mut Self {
37        let mut actions = action.into_boxed_actions();
38
39        match actions.len() {
40            0 => {}
41            1 => {
42                let agent = self.agent;
43                let config = self.config;
44                let action = actions.next().unwrap();
45                self.commands.queue(move |world: &mut World| {
46                    SequentialActionsPlugin::add_action(agent, config, action, world);
47                });
48            }
49            _ => {
50                let agent = self.agent;
51                let config = self.config;
52                self.commands.queue(move |world: &mut World| {
53                    SequentialActionsPlugin::add_actions(agent, config, actions, world);
54                });
55            }
56        }
57
58        self
59    }
60
61    fn execute(&mut self) -> &mut Self {
62        let agent = self.agent;
63
64        self.commands.queue(move |world: &mut World| {
65            SequentialActionsPlugin::execute_actions(agent, world);
66        });
67
68        self
69    }
70
71    fn next(&mut self) -> &mut Self {
72        let agent = self.agent;
73
74        self.commands.queue(move |world: &mut World| {
75            SequentialActionsPlugin::stop_current_action(agent, StopReason::Canceled, world);
76            SequentialActionsPlugin::start_next_action(agent, world);
77        });
78
79        self
80    }
81
82    fn cancel(&mut self) -> &mut Self {
83        let agent = self.agent;
84
85        self.commands.queue(move |world: &mut World| {
86            SequentialActionsPlugin::stop_current_action(agent, StopReason::Canceled, world);
87        });
88
89        self
90    }
91
92    fn pause(&mut self) -> &mut Self {
93        let agent = self.agent;
94
95        self.commands.queue(move |world: &mut World| {
96            SequentialActionsPlugin::stop_current_action(agent, StopReason::Paused, world);
97        });
98
99        self
100    }
101
102    fn skip(&mut self, n: usize) -> &mut Self {
103        let agent = self.agent;
104
105        self.commands.queue(move |world: &mut World| {
106            SequentialActionsPlugin::skip_actions(agent, n, world);
107        });
108
109        self
110    }
111
112    fn clear(&mut self) -> &mut Self {
113        let agent = self.agent;
114
115        self.commands.queue(move |world: &mut World| {
116            SequentialActionsPlugin::clear_actions(agent, world);
117        });
118
119        self
120    }
121}