Expose now via Commands

This commit is contained in:
hodasemi 2025-04-08 09:25:11 +02:00
parent a72a72f8c1
commit da5e137432
2 changed files with 24 additions and 6 deletions

View file

@ -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<dyn Any + Send + Sync>),
}
#[derive(Default)]
pub struct Commands {
commands: Vec<CommandsTypes>,
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();

View file

@ -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(())
}
}