-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentity_macro.rs
More file actions
257 lines (222 loc) · 7.44 KB
/
entity_macro.rs
File metadata and controls
257 lines (222 loc) · 7.44 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#![allow(dead_code)]
#![allow(path_statements)]
use bevy::{ecs::{relationship::RelatedSpawnerCommands, world::DeferredWorld}, prelude::*};
use mevy::*;
pub fn main() {}
// Commands: Spawning \\
fn spawn_with_commands( mut commands: Commands ){
entity!{
<commands>
Transform!;
[spawned][Transform!]
}
}
/// same as [spawn_with_commands]
fn spawn_with_named_commands( mut world: Commands ){
entity!{
// implies <world>
Transform!;
[spawned][Transform!]
>}
//^ using '>' (pointing outside) at the end will leak the entities, e.g. here:
me; // <- the root entity
spawned; // <- the child named 'spawned'
let _me = entity!(
Transform!
<);
//^ using '<' (pointing inside) at the end will return the root entity
}
// Commands: Modify w/ Provided Entity \\
fn modify_with_commands( mut commands: Commands, entity: Entity ){
entity!{
<commands|entity>
Transform!;
[spawned][Transform!]
}
}
fn modify_with_commands_redir( mut commands: Commands, entity: Entity ){
entity!{
<commands|entity>
<Children.get(0).cloned()?> // select first child, if available
<Children.iter()> // select all children of selected
Visibility::Hidden; // hide all of them
.despawn(); // despawn all of them
}
}
/// same as [modify_with_commands]
fn modify_with_named_commands( mut world: Commands, entity: Entity ){
entity!{
// empty before '|' implies a [Commands] names 'world'
<|entity>
Transform!;
[spawned][Transform!]
}
}
/// same as [modify_with_commands]
fn modify_with_named_entity( mut commands: Commands, me: Entity ){
entity!{
// empty after '|' implies an [Entity] names 'me'
// NOTE: You have to type the '|', otherwise it's like [spawn_with_commands]
<commands|>
Transform!;
[spawned][Transform!]
}
}
/// same as [modify_with_commands]
fn modify_with_named_commands_entity( mut world: Commands, me: Entity ){
entity!{
// empty before '|' implies a [Commands] names 'world'
// empty after '|' implies an [Entity] names 'me'
<|>
Transform!;
[spawned][Transform!]
}
}
// Commands: Modify w/ Provided Resource \\
#[derive(Resource,Component)]
struct MultiEntities(Vec<Entity>);
#[derive(Resource,Component)]
struct AnEntity(Entity);
impl AnEntity {
fn get(&self) -> Option<Entity> {Some(self.0)}
}
fn modify_with_commands_resources( mut cmd: Commands ){
entity!{
// using '@' (SAFE MODE) tries to get an entity from a resource:
// - use a path that returns: Option<Entity>
// - does nothing if resource not available
// - does nothing if return value is 'None'
<cmd|@AnEntity.get()>
Transform!;
[spawned][Transform!]
}
// (SAFE MODE): 'spawned' entities CAN NOT be leaked here
}
fn modify_with_commands_resources_forced( mut cmd: Commands ){
entity!{
// using '@!' (RISKY MODE) to get an entity from a resource:
// - use a path that returns: Entity
// - panics if resource not available
// - panics if return value is 'None'
<cmd|@!AnEntity.0>
Transform!;
[spawned][Transform!]
>}
// (RISKY MODE): 'spawned' entities CAN be leaked here
}
fn modify_with_commands_resources_iter( mut cmd: Commands ){
entity!{
// using '@*' will target entities in a iterator of a resource:
// - use a path that returns an Iterator over [Entity]s
<cmd|@*MultiEntities.0.iter().cloned()>
Transform!;
[spawned][Transform!]
}
}
// Commands: Modify w/ Provided Component \\
fn modify_with_commands_marker( mut cmd: Commands ){
entity!{
// using '#' without a trailing path will target EVERY entity With<Component>
<cmd|#AnEntity>
Transform!;
[spawned][Transform!]
}
}
fn modify_with_commands_component( mut cmd: Commands ){
entity!{
// using '#' (SAFE MODE) will target EVERY component in the world:
// - use a path that returns: Option<Entity>
<cmd|#AnEntity.get()>
Transform!;
[spawned][Transform!]
}
}
fn modify_with_commands_component_forced( mut cmd: Commands ){
entity!{
// using '#!' (RISKY MODE) will target THE ONLY component in the world:
// - use a path that returns: Option<Entity>
// - panics if no entity has this component
// - panics if more than one entity has this component
<cmd|#!AnEntity.0>
Transform!;
[spawned][Transform!]
}
// (RISKY MODE): 'spawned' entities are available here
}
fn modify_with_commands_multi( mut cmd: Commands ){
entity!{
// using '#*' will target entities in a iterator of a component:
// - use a path that returns an Iterator over [Entity]s
<cmd|#*MultiEntities.0.iter().cloned()>
Transform!;
[spawned][Transform!]
}
}
// Other 'World Mutators' \\
// Only the first part of <..|..> changes, everything else is as described above
fn modify_with_entity_commands( mut ecmd: EntityCommands ){
entity!{
<*ecmd>
Transform!;
[spawned][Transform!]
}
}
fn modify_with_child_builder<'a>( mut cbuild: RelatedSpawnerCommands<'a,ChildOf> ){
entity!{
<^cbuild>
Transform!;
[spawned][Transform!]
}
}
fn modify_with_deferred_world( mut dworld: DeferredWorld, me: Entity ){
entity!{
<-dworld|>
Transform!;
[spawned][Transform!]
}
}
fn modify_with_world( mut world: World, me: Entity ){
entity!{
<+world|>
Transform!;
[spawned][Transform!]
}
}
// Other 'World Mutators' Implied Name \\
fn modify_with_entity_commands_implied_this( mut this: EntityCommands ){
entity!{<>
Transform!;
[spawned][Transform!]
}
}
fn modify_with_entity_commands_implied( mut world: EntityCommands ){
entity!{<*>
Transform!;
[spawned][Transform!]
}
}
fn modify_with_child_builder_implied<'a>( mut world: RelatedSpawnerCommands<'a,ChildOf> ){
entity!{<^>
Transform!;
[spawned][Transform!]
}
}
fn modify_with_deferred_world_implied( mut world: DeferredWorld, me: Entity ){
entity!{<-|>
Transform!;
[spawned][Transform!]
}
}
fn modify_with_world_implied( mut world: World, me: Entity ){
entity!{<+|>
Transform!;
[spawned][Transform!]
}
}
fn modify_with_entity_world_implied(mut world: EntityWorldMut ){
entity!{<+*|>
Transform!;
[spawned][Transform!]
}
}
// EOF \\