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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! Each [`Behavior`] [`Transition`](crate::transition::Transition) triggers an [`Event`].
//!
//! You may use these events to react to transitions and define the behavior logic.
//!
//! ## Behavior Indices
//!
//! Most events contain a [`BehaviorIndex`] which may be used to identify the exact state associated with the event.
//! You may use [`Index`](std::ops::Index) operator to access the associated state.
//!
//! ## Initialization
//!
//! On spawn, several [`Behavior`] states may be activated at the same time. This can happen if the entity is reloaded from
//! disk or synchronized from the network, for example.
//!
//! For technical reasons, you may need to perform different logic in such cases.
//!
//! For example, considering the following scenario:
//! Let's imagine we're trying to model a car. When the car starts, we want to play an engine start audio clip.
//! However, if the car has been turned on, and is now being loaded from disk, we should skip the engine start audio clip.
//! You may use the `.initial` flag on the behavior events to branch your logic.
//!
//! # Example
//! ```rust
//! use bevy::prelude::*;
//! use moonshine_behavior::prelude::*;
//!
//! #[derive(Component, Debug, Reflect)]
//! #[reflect(Component)]
//! struct B;
//!
//! impl Behavior for B {}
//!
//! fn on_start(event: OnStart<B>, query: Query<BehaviorRef<B>>) {
//! let behavior = query.get(*event.instance).unwrap();
//! let state = &behavior[event.index];
//!
//! if event.initial {
//! /* Custom initialization logic */
//! }
//!
//! /* ... */
//! }
//!
//! fn on_pause(event: OnPause<B>, query: Query<BehaviorRef<B>>) {
//! let behavior = query.get(*event.instance).unwrap();
//! let state = &behavior[event.index];
//! /* ... */
//! }
//!
//! fn on_resume(event: OnResume<B>, query: Query<BehaviorRef<B>>) {
//! let behavior = query.get(*event.instance).unwrap();
//! let state = &behavior[event.index];
//! /* ... */
//! }
//!
//! fn on_activate(event: OnActivate<B>, query: Query<BehaviorRef<B>>) {
//! let behavior = query.get(*event.instance).unwrap();
//! let state = &behavior[event.index];
//! /* ... */
//! }
//!
//! fn on_stop(event: OnStop<B>) {
//! let state = &event.behavior;
//! /* ... */
//! }
//! ```
//!
//! ##
use EntityTrigger;
use *;
use ;
use crateTransitionError;
use crate::;
/// An event which is triggered when a [`Behavior`] starts.
///
/// See [`OnStart`] for a more ergonomic type alias for use in systems.
impl_entity_event_from_instance!;
/// An event which is triggered when a [`Behavior`] is paused.
///
/// See [`OnPause`] for a more ergonomic type alias for use in systems.
impl_entity_event_from_instance!;
/// An event which is triggered when a [`Behavior`] is resumed.
///
/// See [`OnResume`] for a more ergonomic type alias for use in systems.
impl_entity_event_from_instance!;
/// An event which is triggered when a [`Behavior`] is started OR resumed.
///
/// See [`OnActivate`] for a more ergonomic type alias for use in systems.
impl_entity_event_from_instance!;
// TODO: OnSuspend, This gets tricky because the behavior state is already gone in `OnStop` ...
/// An event which is triggered when a [`Behavior`] is stopped.
///
/// Unlike other events, this one does not provide a behavior index.
///
/// See [`OnStop`] for a more ergonomic type alias for use in systems.
impl_entity_event_from_instance!;
/// An event which is triggered when a [`TransitionError`] occurs.
///
/// See [`OnError`] for a more ergonomic type alias for use in systems.
impl_entity_event_from_instance!;
/// Alias for [`On<Start<T>>`](`Start`)
pub type OnStart<'w, 't, T> = ;
/// Alias for [`On<Pause<T>>`](`Pause`)
pub type OnPause<'w, 't, T> = ;
/// Alias for [`On<Resume<T>>`](`Resume`)
pub type OnResume<'w, 't, T> = ;
/// Alias for [`On<Activate<T>>`](`Activate`)
pub type OnActivate<'w, 't, T> = ;
/// Alias for [`On<Stop<T>>`](`Stop`)
pub type OnStop<'w, 't, T> = ;
/// Alias for [`On<Error<T>>`](`Error`)
pub type OnError<'w, 't, T> = ;