@@ -26,24 +26,27 @@ impl CommandQueue {
26
26
}
27
27
28
28
#[ inline]
29
- pub fn push ( & mut self , command : Box < dyn Command > ) {
29
+ pub fn push_boxed ( & mut self , command : Box < dyn Command > ) {
30
30
self . commands . push ( command) ;
31
31
}
32
+
33
+ #[ inline]
34
+ pub fn push < T : Command > ( & mut self , command : T ) {
35
+ self . push_boxed ( Box :: new ( command) ) ;
36
+ }
32
37
}
33
38
34
39
/// A list of commands that will be run to modify a `World`
35
40
pub struct Commands < ' a > {
36
41
queue : & ' a mut CommandQueue ,
37
42
entities : & ' a Entities ,
38
- current_entity : Option < Entity > ,
39
43
}
40
44
41
45
impl < ' a > Commands < ' a > {
42
46
pub fn new ( queue : & ' a mut CommandQueue , world : & ' a World ) -> Self {
43
47
Self {
44
48
queue,
45
49
entities : world. entities ( ) ,
46
- current_entity : None ,
47
50
}
48
51
}
49
52
@@ -84,84 +87,74 @@ impl<'a> Commands<'a> {
84
87
/// }
85
88
/// # example_system.system();
86
89
/// ```
87
- pub fn spawn ( & mut self , bundle : impl Bundle ) -> & mut Self {
90
+ pub fn spawn ( & mut self ) -> EntityCommands < ' a , ' _ > {
88
91
let entity = self . entities . reserve_entity ( ) ;
89
- self . set_current_entity ( entity) ;
90
- self . insert_bundle ( entity, bundle) ;
91
- self
92
+ EntityCommands {
93
+ entity,
94
+ commands : self ,
95
+ }
96
+ }
97
+
98
+ pub fn spawn_bundle < ' b , T : Bundle > ( & ' b mut self , bundle : T ) -> EntityCommands < ' a , ' b > {
99
+ let mut e = self . spawn ( ) ;
100
+ e. insert_bundle ( bundle) ;
101
+ e
102
+ }
103
+
104
+ pub fn entity ( & mut self , entity : Entity ) -> EntityCommands < ' a , ' _ > {
105
+ EntityCommands {
106
+ entity,
107
+ commands : self ,
108
+ }
92
109
}
93
110
94
111
/// Equivalent to iterating `bundles_iter` and calling [`Self::spawn`] on each bundle, but
95
112
/// slightly more performant.
96
- pub fn spawn_batch < I > ( & mut self , bundles_iter : I ) -> & mut Self
113
+ pub fn spawn_batch < I > ( & mut self , bundles_iter : I )
97
114
where
98
115
I : IntoIterator + Send + Sync + ' static ,
99
116
I :: Item : Bundle ,
100
117
{
101
- self . add_command ( SpawnBatch { bundles_iter } )
102
- }
103
-
104
- /// Despawns only the specified entity, not including its children.
105
- pub fn despawn ( & mut self , entity : Entity ) -> & mut Self {
106
- self . add_command ( Despawn { entity } )
107
- }
108
-
109
- /// Inserts a bundle of components into `entity`.
110
- ///
111
- /// See [crate::world::EntityMut::insert_bundle].
112
- pub fn insert_bundle ( & mut self , entity : Entity , bundle : impl Bundle ) -> & mut Self {
113
- self . add_command ( InsertBundle { entity, bundle } )
118
+ self . queue . push ( SpawnBatch { bundles_iter } ) ;
114
119
}
115
120
116
- /// Inserts a single component into `entity`.
117
- ///
118
- /// See [crate::world::EntityMut::insert].
119
- pub fn insert ( & mut self , entity : Entity , component : impl Component ) -> & mut Self {
120
- self . add_command ( Insert { entity, component } )
121
+ /// See [World::insert_resource].
122
+ pub fn insert_resource < T : Component > ( & mut self , resource : T ) {
123
+ self . queue . push ( InsertResource { resource } )
121
124
}
122
125
123
- /// See [crate::world::EntityMut::remove].
124
- pub fn remove < T > ( & mut self , entity : Entity ) -> & mut Self
125
- where
126
- T : Component ,
127
- {
128
- self . add_command ( Remove :: < T > {
129
- entity,
126
+ pub fn remove_resource < T : Component > ( & mut self ) {
127
+ self . queue . push ( RemoveResource :: < T > {
130
128
phantom : PhantomData ,
131
- } )
129
+ } ) ;
132
130
}
133
131
134
- /// See [World::insert_resource].
135
- pub fn insert_resource < T : Component > ( & mut self , resource : T ) -> & mut Self {
136
- self . add_command ( InsertResource { resource } )
132
+ /// Adds a command directly to the command list. Prefer this to [`Self::add_command_boxed`] if
133
+ /// the type of `command` is statically known.
134
+ pub fn add < C : Command > ( & mut self , command : C ) {
135
+ self . queue . push ( command) ;
137
136
}
137
+ }
138
138
139
- /// See [crate::world::EntityMut::remove_bundle].
140
- pub fn remove_bundle < T > ( & mut self , entity : Entity ) -> & mut Self
141
- where
142
- T : Bundle ,
143
- {
144
- self . add_command ( RemoveBundle :: < T > {
145
- entity,
146
- phantom : PhantomData ,
147
- } )
148
- }
139
+ pub struct EntityCommands < ' a , ' b > {
140
+ entity : Entity ,
141
+ commands : & ' b mut Commands < ' a > ,
142
+ }
149
143
150
- pub fn remove_resource < T : Component > ( & mut self ) -> & mut Self {
151
- self . add_command ( RemoveResource :: < T > {
152
- phantom : PhantomData ,
153
- } )
144
+ impl < ' a , ' b > EntityCommands < ' a , ' b > {
145
+ # [ inline ]
146
+ pub fn id ( & self ) -> Entity {
147
+ self . entity
154
148
}
155
149
156
150
/// Adds a bundle of components to the current entity.
157
151
///
158
152
/// See [`Self::with`], [`Self::current_entity`].
159
- pub fn with_bundle ( & mut self , bundle : impl Bundle ) -> & mut Self {
160
- let current_entity = self . current_entity . expect ( "Cannot add bundle because the 'current entity' is not set. You should spawn an entity first." ) ;
161
- self . queue . push ( Box :: new ( InsertBundle {
162
- entity : current_entity,
153
+ pub fn insert_bundle ( & mut self , bundle : impl Bundle ) -> & mut Self {
154
+ self . commands . add ( InsertBundle {
155
+ entity : self . entity ,
163
156
bundle,
164
- } ) ) ;
157
+ } ) ;
165
158
self
166
159
}
167
160
@@ -205,47 +198,47 @@ impl<'a> Commands<'a> {
205
198
/// }
206
199
/// # example_system.system();
207
200
/// ```
208
- pub fn with ( & mut self , component : impl Component ) -> & mut Self {
209
- let current_entity = self . current_entity . expect ( "Cannot add component because the 'current entity' is not set. You should spawn an entity first." ) ;
210
- self . queue . push ( Box :: new ( Insert {
211
- entity : current_entity,
201
+ pub fn insert ( & mut self , component : impl Component ) -> & mut Self {
202
+ self . commands . add ( Insert {
203
+ entity : self . entity ,
212
204
component,
213
- } ) ) ;
205
+ } ) ;
214
206
self
215
207
}
216
208
217
- /// Adds a command directly to the command list. Prefer this to [`Self::add_command_boxed`] if
218
- /// the type of `command` is statically known.
219
- pub fn add_command < C : Command > ( & mut self , command : C ) -> & mut Self {
220
- self . queue . push ( Box :: new ( command) ) ;
209
+ /// See [crate::world::EntityMut::remove_bundle].
210
+ pub fn remove_bundle < T > ( & mut self ) -> & mut Self
211
+ where
212
+ T : Bundle ,
213
+ {
214
+ self . commands . add ( RemoveBundle :: < T > {
215
+ entity : self . entity ,
216
+ phantom : PhantomData ,
217
+ } ) ;
221
218
self
222
219
}
223
220
224
- /// See [`Self::add_command`].
225
- pub fn add_command_boxed ( & mut self , command : Box < dyn Command > ) -> & mut Self {
226
- self . queue . push ( command) ;
221
+ /// See [crate::world::EntityMut::remove].
222
+ pub fn remove < T > ( & mut self ) -> & mut Self
223
+ where
224
+ T : Component ,
225
+ {
226
+ self . commands . add ( Remove :: < T > {
227
+ entity : self . entity ,
228
+ phantom : PhantomData ,
229
+ } ) ;
227
230
self
228
231
}
229
232
230
- /// Returns the current entity, set by [`Self::spawn`] or with [`Self::set_current_entity`].
231
- pub fn current_entity ( & self ) -> Option < Entity > {
232
- self . current_entity
233
- }
234
-
235
- pub fn set_current_entity ( & mut self , entity : Entity ) {
236
- self . current_entity = Some ( entity) ;
237
- }
238
-
239
- pub fn clear_current_entity ( & mut self ) {
240
- self . current_entity = None ;
233
+ /// Despawns only the specified entity, not including its children.
234
+ pub fn despawn ( & mut self ) {
235
+ self . commands . add ( Despawn {
236
+ entity : self . entity ,
237
+ } )
241
238
}
242
239
243
- pub fn for_current_entity ( & mut self , f : impl FnOnce ( Entity ) ) -> & mut Self {
244
- let current_entity = self
245
- . current_entity
246
- . expect ( "The 'current entity' is not set. You should spawn an entity first." ) ;
247
- f ( current_entity) ;
248
- self
240
+ pub fn commands ( & mut self ) -> & mut Commands < ' a > {
241
+ self . commands
249
242
}
250
243
}
251
244
@@ -392,9 +385,8 @@ mod tests {
392
385
let mut world = World :: default ( ) ;
393
386
let mut command_queue = CommandQueue :: default ( ) ;
394
387
let entity = Commands :: new ( & mut command_queue, & world)
395
- . spawn ( ( 1u32 , 2u64 ) )
396
- . current_entity ( )
397
- . unwrap ( ) ;
388
+ . spawn_bundle ( ( 1u32 , 2u64 ) )
389
+ . id ( ) ;
398
390
command_queue. apply ( & mut world) ;
399
391
assert ! ( world. entities( ) . len( ) == 1 ) ;
400
392
let results = world
@@ -404,9 +396,11 @@ mod tests {
404
396
. collect :: < Vec < _ > > ( ) ;
405
397
assert_eq ! ( results, vec![ ( 1u32 , 2u64 ) ] ) ;
406
398
// test entity despawn
407
- Commands :: new ( & mut command_queue, & world)
408
- . despawn ( entity)
409
- . despawn ( entity) ; // double despawn shouldn't panic
399
+ {
400
+ let mut commands = Commands :: new ( & mut command_queue, & world) ;
401
+ commands. entity ( entity) . despawn ( ) ;
402
+ commands. entity ( entity) . despawn ( ) ; // double despawn shouldn't panic
403
+ }
410
404
command_queue. apply ( & mut world) ;
411
405
let results2 = world
412
406
. query :: < ( & u32 , & u64 ) > ( )
@@ -421,9 +415,9 @@ mod tests {
421
415
let mut world = World :: default ( ) ;
422
416
let mut command_queue = CommandQueue :: default ( ) ;
423
417
let entity = Commands :: new ( & mut command_queue, & world)
424
- . spawn ( ( 1u32 , 2u64 ) )
425
- . current_entity ( )
426
- . unwrap ( ) ;
418
+ . spawn ( )
419
+ . insert_bundle ( ( 1u32 , 2u64 ) )
420
+ . id ( ) ;
427
421
command_queue. apply ( & mut world) ;
428
422
let results_before = world
429
423
. query :: < ( & u32 , & u64 ) > ( )
@@ -434,8 +428,9 @@ mod tests {
434
428
435
429
// test component removal
436
430
Commands :: new ( & mut command_queue, & world)
437
- . remove :: < u32 > ( entity)
438
- . remove_bundle :: < ( u32 , u64 ) > ( entity) ;
431
+ . entity ( entity)
432
+ . remove :: < u32 > ( )
433
+ . remove_bundle :: < ( u32 , u64 ) > ( ) ;
439
434
command_queue. apply ( & mut world) ;
440
435
let results_after = world
441
436
. query :: < ( & u32 , & u64 ) > ( )
0 commit comments