From f71f967a8d804181038116fe2ad9776c08ddcfbc Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 2 Nov 2024 21:11:55 +0100 Subject: feat(ecs): add creating static entities --- ecs/src/lib.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'ecs/src/lib.rs') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 3c517dd..6725d81 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -11,7 +11,7 @@ use std::sync::Arc; use crate::actions::Action; use crate::component::storage::Storage as ComponentStorage; use crate::component::{Component, Id as ComponentId, Sequence as ComponentSequence}; -use crate::entity::Uid as EntityUid; +use crate::entity::{Uid as EntityUid, CREATE_STATIC_ENTITIES}; use crate::event::component::{ create_added_id as create_component_added_event_id, create_removed_id as create_component_removed_event_id, @@ -42,6 +42,9 @@ pub mod system; pub mod tuple; pub mod type_name; +#[doc(hidden)] +pub mod private; + mod archetype; mod util; @@ -75,7 +78,6 @@ impl World /// /// # Panics /// Will panic if mutable internal lock cannot be acquired. - #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] pub fn create_entity(&mut self, components: Comps) -> EntityUid where Comps: ComponentSequence + TupleReduce, @@ -83,6 +85,17 @@ impl World { let entity_uid = EntityUid::new_unique(); + self.create_entity_with_uid(components, entity_uid); + + entity_uid + } + + #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] + pub fn create_entity_with_uid(&self, components: Comps, entity_uid: EntityUid) + where + Comps: ComponentSequence + TupleReduce, + Comps::Out: EventSequence, + { #[allow(unused_variables)] if let Err(err) = self .data @@ -94,14 +107,12 @@ impl World #[cfg(feature = "debug")] tracing::error!("Failed to create entity: {err}"); - return entity_uid; + return; }; for component_added_event_id in ::ids().iter() { self.emit_event_by_id(*component_added_event_id); } - - entity_uid } /// Adds a globally shared singleton value. @@ -279,6 +290,10 @@ impl World /// Will panic if a internal lock cannot be acquired. pub fn event_loop(&self) { + for create_static_entity in CREATE_STATIC_ENTITIES { + create_static_entity(self); + } + self.emit(StartEvent); let event_seq = EventSeq::ids(); -- cgit v1.2.3-18-g5258