Provide entities mut

This commit is contained in:
hodasemi 2025-04-06 11:55:39 +02:00
parent a4c083f727
commit 9420cfa863
4 changed files with 32 additions and 46 deletions

View file

@ -5,7 +5,7 @@ members = ["ecs", "update_macros"]
[workspace.dependencies] [workspace.dependencies]
anyhow = { version = "1.0.86", features = ["backtrace"] } anyhow = { version = "1.0.86", features = ["backtrace"] }
indexmap = { version = "2.2.6", features = ["rayon"] } indexmap = { version = "2.9.0", features = ["rayon"] }
serde = { version = "1.0.203", features = ["derive"] } serde = { version = "1.0.203", features = ["derive"] }
paste = "1.0.15" paste = "1.0.15"
ron = "0.9.0" ron = "0.9.0"

View file

@ -1,10 +1,8 @@
use core::fmt; use core::fmt;
use std::any::TypeId; use std::any::TypeId;
use std::collections::HashSet;
use std::{num::ParseIntError, str::FromStr}; use std::{num::ParseIntError, str::FromStr};
use anyhow::Result; use anyhow::Result;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ComponentDebug, ComponentNotFoundError, EntityComponent, TypeMap}; use crate::{ComponentDebug, ComponentNotFoundError, EntityComponent, TypeMap};
@ -28,7 +26,7 @@ impl std::fmt::Display for EntityNotFoundError {
impl std::error::Error for EntityNotFoundError {} impl std::error::Error for EntityNotFoundError {}
#[derive(Default)] #[derive(Default, Debug)]
pub struct ActivationState { pub struct ActivationState {
activated: bool, activated: bool,
} }
@ -67,6 +65,22 @@ pub struct EntityObject {
component_names: Vec<String>, component_names: Vec<String>,
} }
impl std::fmt::Debug for EntityObject {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut t = f.debug_struct("EntityObject");
t.field("debug_name", &self.debug_name)
.field("gltf_file", &self.gltf_file)
.field("activation_state", &self.activation_state)
.field("entity_id", &self.entity_id);
#[cfg(debug_assertions)]
t.field("component_names", &self.component_names);
t.finish()
}
}
unsafe impl Send for EntityObject {} unsafe impl Send for EntityObject {}
unsafe impl Sync for EntityObject {} unsafe impl Sync for EntityObject {}
@ -310,40 +324,3 @@ impl FromStr for Entity {
Ok(Self { id: s.parse()? }) Ok(Self { id: s.parse()? })
} }
} }
pub struct EntityMultiMut<'a> {
entities: &'a mut IndexMap<Entity, EntityObject>,
buffer: HashSet<Entity>,
}
impl<'a> EntityMultiMut<'a> {
pub(crate) fn new(entities: &'a mut IndexMap<Entity, EntityObject>) -> Self {
Self {
entities,
buffer: HashSet::new(),
}
}
pub fn get(
&mut self,
entity: Entity,
) -> std::result::Result<&'a mut EntityObject, EntityNotFoundError> {
match self.entities.get_mut(&entity) {
Some(v) => {
let ptr = v as *mut EntityObject;
assert!(
self.buffer.get(&entity).is_none(),
"Entity ({}) already borrowed",
entity.id
);
self.buffer.insert(entity);
let e: Option<&'a mut EntityObject> = Some(unsafe { &mut *ptr });
e.ok_or_else(|| EntityNotFoundError::new(entity))
}
None => Err(EntityNotFoundError::new(entity)),
}
}
}

View file

@ -8,9 +8,7 @@ mod unsafe_component_store;
mod updates; mod updates;
mod world; mod world;
pub use crate::entity::{ pub use crate::entity::{Entity, EntityComponentDisjointMut, EntityNotFoundError, EntityObject};
Entity, EntityComponentDisjointMut, EntityMultiMut, EntityNotFoundError, EntityObject,
};
pub use crate::events::Events; pub use crate::events::Events;
pub use crate::resources::Resources; pub use crate::resources::Resources;
pub use crate::type_map::{ pub use crate::type_map::{

View file

@ -166,8 +166,19 @@ impl World {
.map(|e| unsafe { remove_life_time_mut(e) }) .map(|e| unsafe { remove_life_time_mut(e) })
} }
pub fn entities_multi_mut(&mut self) -> EntityMultiMut<'_> { pub fn entities_mut<const N: usize>(
EntityMultiMut::new(&mut self.entities) &mut self,
entities: [Entity; N],
) -> std::result::Result<[&mut EntityObject; N], EntityNotFoundError> {
Ok(self
.entities
.get_disjoint_mut::<Entity, N>(entities.iter().collect::<Vec<_>>().try_into().unwrap())
.into_iter()
.enumerate()
.map(|(i, entity_opt)| entity_opt.ok_or(EntityNotFoundError::new(entities[i])))
.collect::<Result<Vec<_>, _>>()?
.try_into()
.unwrap())
} }
pub fn add_entity(&mut self, entity_object: EntityObject) -> Result<Entity> { pub fn add_entity(&mut self, entity_object: EntityObject) -> Result<Entity> {