Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mvp-effects.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
- [ ] `CommandRunSchedule`

# Entity command effects:
- [ ] `EntityCommandQueue<C>`
- [x] `EntityCommandQueue<C>`

- [ ] `EntityCommandInsert`
- [ ] `EntityCommandRemove`
Expand Down
86 changes: 86 additions & 0 deletions src/effects/entity_command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::marker::PhantomData;

use bevy::ecs::error::CommandWithEntity;
use bevy::prelude::*;

use crate::Effect;

/// [`Effect`] that pushes a generic entity command to the command queue.
#[doc = include_str!("defer_command_note.md")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct EntityCommandQueue<C, T, M>
where
C: EntityCommand<T> + CommandWithEntity<M>,
{
entity: Entity,
command: C,
entity_command_out: PhantomData<T>,
command_with_entity_out: PhantomData<M>,
}

impl<C, T, M> EntityCommandQueue<C, T, M>
where
C: EntityCommand<T> + CommandWithEntity<M>,
{
/// Construct a new [`EntityCommandQueue`]
pub fn new(entity: Entity, command: C) -> Self {
EntityCommandQueue {
entity,
command,
entity_command_out: PhantomData,
command_with_entity_out: PhantomData,
}
}
}

impl<C, T, M> Effect for EntityCommandQueue<C, T, M>
where
C: EntityCommand<T> + CommandWithEntity<M>,
{
type MutParam = Commands<'static, 'static>;

fn affect(self, param: &mut <Self::MutParam as bevy::ecs::system::SystemParam>::Item<'_, '_>) {
param.entity(self.entity).queue(self.command);
}
}

#[cfg(test)]
mod tests {
use proptest::prelude::*;

use super::*;
use crate::effects::number_data::NumberComponent;
use crate::prelude::affect;

proptest! {
#[test]
fn entity_command_queue_can_insert_component_non_exclusively(component in any::<NumberComponent<0>>()) {
let mut app = App::new();

let entity = app.world_mut().spawn(()).id();

let actual_component = app.world().get_entity(entity).unwrap().get_components::<&NumberComponent<0>>();

assert!(actual_component.is_none());

let insert_component_system = move || {
EntityCommandQueue::new(entity, move |mut entity_world: EntityWorldMut<'_>| {
entity_world.insert(component.clone());
})
};

assert!(!IntoSystem::into_system(insert_component_system.pipe(affect)).is_exclusive());

app.add_systems(
Update,
insert_component_system.pipe(affect),
);

app.update();

let actual_component = app.world().get_entity(entity).unwrap().get_components::<&NumberComponent<0>>().unwrap();

assert_eq!(actual_component, &component);
}
}
}
3 changes: 3 additions & 0 deletions src/effects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub use entity_components::{EntityComponentsPut, EntityComponentsWith};
mod command;
pub use command::{CommandInsertResource, CommandQueue, CommandRemoveResource};

mod entity_command;
pub use entity_command::EntityCommandQueue;

mod variadic;

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub use crate::effects::{
CommandRemoveResource,
ComponentsPut,
ComponentsWith,
EntityCommandQueue,
EntityComponentsPut,
EntityComponentsWith,
EventWrite,
Expand Down