1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use bevy_ecs::prelude::*;

use crate::*;

impl<'w: 'a, 's: 'a, 'a> ActionsProxy<'a> for Commands<'w, 's> {
    type Modifier = EntityCommandsActions<'w, 's, 'a>;

    fn actions(&'a mut self, entity: Entity) -> EntityCommandsActions<'w, 's, 'a> {
        EntityCommandsActions {
            entity,
            config: AddConfig::default(),
            commands: self,
        }
    }
}

/// Modify actions using [`Commands`].
pub struct EntityCommandsActions<'w, 's, 'a> {
    entity: Entity,
    config: AddConfig,
    commands: &'a mut Commands<'w, 's>,
}

impl<'w, 's, 'a> ModifyActions for EntityCommandsActions<'w, 's, 'a> {
    type Builder = ActionCommandsBuilder<'w, 's, 'a>;

    fn config(mut self, config: AddConfig) -> Self {
        self.config = config;
        self
    }

    fn add<T: IntoAction>(self, action: T) -> Self {
        self.commands.add(move |world: &mut World| {
            world.actions(self.entity).config(self.config).add(action);
        });
        self
    }

    fn next(self) -> Self {
        self.commands.add(move |world: &mut World| {
            world.actions(self.entity).next();
        });
        self
    }

    fn finish(self) -> Self {
        self.commands.add(move |world: &mut World| {
            world.actions(self.entity).finish();
        });
        self
    }

    fn pause(self) -> Self {
        self.commands.add(move |world: &mut World| {
            world.actions(self.entity).pause();
        });
        self
    }

    fn stop(self, reason: StopReason) -> Self {
        self.commands.add(move |world: &mut World| {
            world.actions(self.entity).stop(reason);
        });
        self
    }

    fn skip(self) -> Self {
        self.commands.add(move |world: &mut World| {
            world.actions(self.entity).skip();
        });
        self
    }

    fn clear(self) -> Self {
        self.commands.add(move |world: &mut World| {
            world.actions(self.entity).clear();
        });
        self
    }

    fn builder(self) -> Self::Builder {
        ActionCommandsBuilder {
            config: AddConfig::default(),
            actions: Vec::new(),
            modifier: self,
        }
    }
}

/// Build a list of actions using [`Commands`].
pub struct ActionCommandsBuilder<'w, 's, 'a> {
    config: AddConfig,
    actions: Vec<(Box<dyn Action>, AddConfig)>,
    modifier: EntityCommandsActions<'w, 's, 'a>,
}

impl<'w, 's, 'a> ActionBuilder for ActionCommandsBuilder<'w, 's, 'a> {
    type Modifier = EntityCommandsActions<'w, 's, 'a>;

    fn config(mut self, config: AddConfig) -> Self {
        self.config = config;
        self
    }

    fn push<T: IntoAction>(mut self, action: T) -> Self {
        self.actions.push((action.into_boxed(), self.config));
        self
    }

    fn reverse(mut self) -> Self {
        self.actions.reverse();
        self
    }

    fn submit(self) -> Self::Modifier {
        self.modifier.commands.add(move |world: &mut World| {
            let mut wa = world.actions(self.modifier.entity);
            for (action, config) in self.actions {
                wa = wa.config(config).add(action);
            }
        });

        self.modifier
    }
}