diff --git a/ecs/src/commands.rs b/ecs/src/commands.rs index 5c20f34..0549307 100644 --- a/ecs/src/commands.rs +++ b/ecs/src/commands.rs @@ -1,5 +1,8 @@ use anyhow::Result; -use std::any::{Any, TypeId}; +use std::{ + any::{Any, TypeId}, + time::Duration, +}; use crate::{resources::Resource, world::ComponentChange, *}; @@ -13,12 +16,23 @@ enum CommandTypes { InsertResource(TypeId, Box), } -#[derive(Default)] pub struct Commands { commands: Vec, + now: Duration, } impl Commands { + pub(crate) fn new(now: Duration) -> Self { + Self { + commands: Vec::new(), + now, + } + } + + pub fn now(&self) -> Duration { + self.now + } + pub fn insert_entity(&mut self, entity_object: EntityObject) -> Entity { let entity = entity_object.as_entity(); diff --git a/ecs/src/updates.rs b/ecs/src/updates.rs index e07bd79..55ffdba 100644 --- a/ecs/src/updates.rs +++ b/ecs/src/updates.rs @@ -493,12 +493,14 @@ impl Archetype { } pub fn execute(&self, world: &mut World) -> Result<()> { + let mut commands = Commands::new(world.now()); + for (entity, callback) in self.entities.iter() { - let mut commands = Commands::default(); callback(*entity, &mut commands, world)?; - commands.apply_deferred(world)?; } + commands.apply_deferred(world)?; + Ok(()) } @@ -576,12 +578,14 @@ impl ArchetypePair { } pub(crate) fn execute(&self, world: &mut World) -> Result<()> { + let mut commands = Commands::new(world.now()); + for ((left_entity, right_entity), callback) in self.entities.iter() { - let mut commands = Commands::default(); callback(*left_entity, *right_entity, &mut commands)?; - commands.apply_deferred(world)?; } + commands.apply_deferred(world)?; + Ok(()) } }