From 0b78c7da4980c90c2feca1ef7e0ee898db084486 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 8 Jul 2026 23:30:50 +0200 Subject: feat(engine-ecs): add fns for spawning named entities --- engine-ecs/src/actions.rs | 34 +++++++++++++++++++++++++++++++++- engine-ecs/src/extension.rs | 13 +++++++++++++ engine-ecs/src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ engine-ecs/src/util.rs | 2 +- 4 files changed, 91 insertions(+), 2 deletions(-) (limited to 'engine-ecs') diff --git a/engine-ecs/src/actions.rs b/engine-ecs/src/actions.rs index 9e88011..411a5d5 100644 --- a/engine-ecs/src/actions.rs +++ b/engine-ecs/src/actions.rs @@ -1,4 +1,11 @@ -use crate::component::{Parts as ComponentParts, Sequence as ComponentSequence}; +use std::borrow::Cow; + +use crate::component::{ + IntoParts, + Parts as ComponentParts, + Sequence as ComponentSequence, +}; +use crate::entity::Name as EntityName; use crate::event::component::Removed; use crate::pair::Pair; use crate::system::{Metadata as SystemMetadata, Param as SystemParam}; @@ -29,6 +36,31 @@ impl Actions<'_> new_entity_uid } + /// Queues up a entity to spawn at the end of the current tick, returning the [`Uid`] + /// that the entity will have. + /// + /// In addition to the given components, the entity will have a [`EntityName`] + /// component containing `name`. + pub fn spawn_named( + &mut self, + name: impl Into>, + components: Comps, + ) -> Uid + { + let new_entity_uid = Uid::new_unique(); + + self.action_queue.push(Action::Spawn( + new_entity_uid, + components + .into_parts_array() + .into_iter() + .chain([EntityName { name: name.into() }.into_parts()]) + .collect(), + )); + + new_entity_uid + } + /// Queues up despawning a entity at the end of the current tick. pub fn despawn(&mut self, entity_uid: Uid) { diff --git a/engine-ecs/src/extension.rs b/engine-ecs/src/extension.rs index 1975933..ac989c9 100644 --- a/engine-ecs/src/extension.rs +++ b/engine-ecs/src/extension.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use crate::component::Sequence as ComponentSequence; use crate::entity::Declaration as EntityDeclaration; use crate::sole::Sole; @@ -53,6 +55,17 @@ impl<'world> Collector<'world> self.world.spawn(components); } + /// Adds a entity to the [`World`]. + pub fn spawn_named( + &mut self, + name: impl Into>, + components: Comps, + ) where + Comps: ComponentSequence, + { + self.world.spawn_named(name, components); + } + /// Adds a declared entity to the [`World`]. pub fn spawn_declared_entity(&mut self, entity_decl: &EntityDeclaration) { 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( + &mut self, + name: impl Into>, + 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( + &mut self, + entity_uid: Uid, + name: impl Into>, + 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( diff --git a/engine-ecs/src/util.rs b/engine-ecs/src/util.rs index a6bd14b..f56a5c4 100644 --- a/engine-ecs/src/util.rs +++ b/engine-ecs/src/util.rs @@ -185,7 +185,7 @@ where pub trait Array: AsRef<[Item]> + AsMut<[Item]> - + IntoIterator + + IntoIterator + Into> + Sortable + sealed::Sealed -- cgit v1.2.3-18-g5258