diff options
| author | HampusM <hampus@hampusmat.com> | 2026-07-08 23:30:50 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-07-08 23:30:50 +0200 |
| commit | 0b78c7da4980c90c2feca1ef7e0ee898db084486 (patch) | |
| tree | ba2f033b004e44b73737e502274117357e022d24 /engine-ecs/src/lib.rs | |
| parent | a34ad021ab4d3330e28e5489b24be7dd23c89808 (diff) | |
feat(engine-ecs): add fns for spawning named entities
Diffstat (limited to 'engine-ecs/src/lib.rs')
| -rw-r--r-- | engine-ecs/src/lib.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/engine-ecs/src/lib.rs b/engine-ecs/src/lib.rs index 190eab7..b682906 100644 --- a/engine-ecs/src/lib.rs +++ b/engine-ecs/src/lib.rs @@ -1,6 +1,7 @@ #![deny(clippy::all, clippy::pedantic)] use std::any::{Any, TypeId}; +use std::borrow::Cow; use std::fmt::Debug; use std::hint::cold_path; use std::rc::Rc; @@ -138,6 +139,26 @@ impl World entity_uid } + /// Creates a entity with the given components. A new unique [`Uid`] will be generated + /// for this entity. + /// + /// In addition to the given components, the entity will have a [`EntityName`] + /// component containing `name`. + pub fn spawn_named<Comps>( + &mut self, + name: impl Into<Cow<'static, str>>, + components: Comps, + ) -> Uid + where + Comps: ComponentSequence, + { + let entity_uid = Uid::new_unique(); + + self.spawn_named_with_uid(entity_uid, name, components); + + entity_uid + } + /// Creates a entity with the given components. The entity will have the specified /// [`Uid`]. #[tracing::instrument(skip_all)] @@ -148,6 +169,29 @@ impl World self.create_ent(entity_uid, components.into_parts_array()); } + /// Creates a entity with the given components. The entity will have the specified + /// [`Uid`]. + /// + /// In addition to the given components, the entity will have a [`EntityName`] + /// component containing `name`. + #[tracing::instrument(skip_all)] + pub fn spawn_named_with_uid<Comps>( + &mut self, + entity_uid: Uid, + name: impl Into<Cow<'static, str>>, + components: Comps, + ) where + Comps: ComponentSequence, + { + self.create_ent( + entity_uid, + components + .into_parts_array() + .into_iter() + .chain([EntityName { name: name.into() }.into_parts()]), + ); + } + pub fn add_component(&mut self, entity_id: Uid, component_parts: ComponentParts) { Self::add_entity_components( |
