-
Notifications
You must be signed in to change notification settings - Fork 32
/
commands.rs
38 lines (34 loc) · 1.04 KB
/
commands.rs
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
use bevy::{
ecs::system::{Command, EntityCommands},
prelude::*,
};
/// Extension trait for [`EntityCommands`] that allows inserting a dynamic component.
pub trait DynamicEntityCommandsExt {
/// Insert a component onto the entity that is not known at compile time.
fn insert_dynamic(
&mut self,
reflect_component: ReflectComponent,
component_data: Box<dyn Reflect>,
) -> &mut Self;
}
struct InsertDynamicCommand(Entity, ReflectComponent, Box<dyn Reflect>);
impl Command for InsertDynamicCommand {
fn write(self, world: &mut World) {
self.1.insert(world, self.0, self.2.as_reflect());
}
}
impl<'w, 's, 'a> DynamicEntityCommandsExt for EntityCommands<'w, 's, 'a> {
fn insert_dynamic(
&mut self,
reflect_component: ReflectComponent,
component_data: Box<dyn Reflect>,
) -> &mut Self {
let entity = self.id();
self.commands().add(InsertDynamicCommand(
entity,
reflect_component,
component_data,
));
self
}
}