Merge branch 'main' of ssh://gavania.de:62/hodasemi/ecs

This commit is contained in:
hodasemi 2025-04-08 18:00:17 +02:00
commit 540be64f6a
2 changed files with 24 additions and 6 deletions

View file

@ -1,5 +1,8 @@
use anyhow::Result; use anyhow::Result;
use std::any::{Any, TypeId}; use std::{
any::{Any, TypeId},
time::Duration,
};
use crate::{resources::Resource, world::ComponentChange, *}; use crate::{resources::Resource, world::ComponentChange, *};
@ -13,12 +16,23 @@ enum CommandTypes {
InsertResource(TypeId, Box<dyn Resource>), InsertResource(TypeId, Box<dyn Resource>),
} }
#[derive(Default)]
pub struct Commands { pub struct Commands {
commands: Vec<CommandTypes>, commands: Vec<CommandTypes>,
now: Duration,
} }
impl Commands { 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 { pub fn insert_entity(&mut self, entity_object: EntityObject) -> Entity {
let entity = entity_object.as_entity(); let entity = entity_object.as_entity();

View file

@ -493,12 +493,14 @@ impl Archetype {
} }
pub fn execute(&self, world: &mut World) -> Result<()> { pub fn execute(&self, world: &mut World) -> Result<()> {
let mut commands = Commands::new(world.now());
for (entity, callback) in self.entities.iter() { for (entity, callback) in self.entities.iter() {
let mut commands = Commands::default();
callback(*entity, &mut commands, world)?; callback(*entity, &mut commands, world)?;
commands.apply_deferred(world)?;
} }
commands.apply_deferred(world)?;
Ok(()) Ok(())
} }
@ -576,12 +578,14 @@ impl ArchetypePair {
} }
pub(crate) fn execute(&self, world: &mut World) -> Result<()> { 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() { for ((left_entity, right_entity), callback) in self.entities.iter() {
let mut commands = Commands::default();
callback(*left_entity, *right_entity, &mut commands)?; callback(*left_entity, *right_entity, &mut commands)?;
commands.apply_deferred(world)?;
} }
commands.apply_deferred(world)?;
Ok(()) Ok(())
} }
} }