summaryrefslogtreecommitdiff
path: root/engine-ecs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-07-08 23:30:50 +0200
committerHampusM <hampus@hampusmat.com>2026-07-08 23:30:50 +0200
commit0b78c7da4980c90c2feca1ef7e0ee898db084486 (patch)
treeba2f033b004e44b73737e502274117357e022d24 /engine-ecs
parenta34ad021ab4d3330e28e5489b24be7dd23c89808 (diff)
feat(engine-ecs): add fns for spawning named entities
Diffstat (limited to 'engine-ecs')
-rw-r--r--engine-ecs/src/actions.rs34
-rw-r--r--engine-ecs/src/extension.rs13
-rw-r--r--engine-ecs/src/lib.rs44
-rw-r--r--engine-ecs/src/util.rs2
4 files changed, 91 insertions, 2 deletions
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<Comps: ComponentSequence>(
+ &mut self,
+ name: impl Into<Cow<'static, str>>,
+ 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<Comps>(
+ &mut self,
+ name: impl Into<Cow<'static, str>>,
+ 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<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(
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<Item>:
AsRef<[Item]>
+ AsMut<[Item]>
- + IntoIterator<Item = Item>
+ + IntoIterator<Item = Item, IntoIter: ExactSizeIterator>
+ Into<Vec<Item>>
+ Sortable<Item = Item>
+ sealed::Sealed