From da5e137432613ee916618c0485d93bb0314edee4 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Tue, 8 Apr 2025 09:25:11 +0200 Subject: [PATCH] Expose now via Commands --- ecs/src/commands.rs | 18 ++++++++++++++++-- ecs/src/updates.rs | 12 ++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ecs/src/commands.rs b/ecs/src/commands.rs index 854c63a..e8a2851 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::{world::ComponentChange, *}; @@ -11,12 +14,23 @@ enum CommandsTypes { Event(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 8385a13..a3b551c 100644 --- a/ecs/src/updates.rs +++ b/ecs/src/updates.rs @@ -503,12 +503,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(()) } @@ -586,12 +588,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(()) } }