summaryrefslogtreecommitdiff
path: root/engine-ecs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-06-20 16:35:45 +0200
committerHampusM <hampus@hampusmat.com>2026-06-23 00:25:39 +0200
commit7dad5922276687d284269316158ec7e742f6d1fc (patch)
tree28e2823dd619d7fdcf9ae032ba08b0978f4b9de2 /engine-ecs
parent7b3374ad9585f78c60e1b158126ab54384a83f32 (diff)
feat(engine-ecs): add entity name component
Diffstat (limited to 'engine-ecs')
-rw-r--r--engine-ecs/src/entity.rs22
-rw-r--r--engine-ecs/src/lib.rs13
2 files changed, 32 insertions, 3 deletions
diff --git a/engine-ecs/src/entity.rs b/engine-ecs/src/entity.rs
index d7b63be..9ef9da0 100644
--- a/engine-ecs/src/entity.rs
+++ b/engine-ecs/src/entity.rs
@@ -1,14 +1,15 @@
use std::any::{type_name, Any};
+use std::borrow::Cow;
use std::ops::Deref;
use std::sync::LazyLock;
+use crate::Component;
use crate::component::storage::archetype::{
Archetype,
Entity as ArchetypeEntity,
MatchingComponentIter as ArchetypeMatchingComponentIter,
};
use crate::component::{
- Component,
Handle as ComponentHandle,
HandleMut as ComponentHandleMut,
};
@@ -42,6 +43,18 @@ impl<'a> Handle<'a>
self.entity.uid()
}
+ /// Returns a reference to the [`Name`] component in this entity. `None` is
+ /// returned if the component isn't found in the entity.
+ ///
+ /// # Panics
+ /// Will panic if:
+ /// - The component is mutably borrowed elsewhere
+ #[must_use]
+ pub fn get_name(&self) -> Option<ComponentHandle<'a, Name>>
+ {
+ self.get::<Name>()
+ }
+
/// Returns a reference to the specified component in this entity. `None` is
/// returned if the component isn't found in the entity.
///
@@ -315,3 +328,10 @@ macro_rules! declare_entity {
});
}
}
+
+/// Entity name.
+#[derive(Debug, Clone, Component)]
+pub struct Name
+{
+ pub name: Cow<'static, str>
+}
diff --git a/engine-ecs/src/lib.rs b/engine-ecs/src/lib.rs
index 25866ca..28b6b06 100644
--- a/engine-ecs/src/lib.rs
+++ b/engine-ecs/src/lib.rs
@@ -16,7 +16,7 @@ use crate::component::{
Parts as ComponentParts,
Sequence as ComponentSequence,
};
-use crate::entity::{Declaration as EntityDeclaration, Handle as EntityHandle};
+use crate::entity::{Declaration as EntityDeclaration, Handle as EntityHandle, Name as EntityName};
use crate::error::{
err_handler_panic,
ErrorHandler,
@@ -160,7 +160,16 @@ impl World
where
SoleT: Sole,
{
- self.create_ent(SoleT::id(), [sole.into_parts()]);
+ let name = sole.name();
+
+ self.create_ent(
+ SoleT::id(),
+ [
+ sole.into_parts(),
+ EntityName {
+ name: name.into()
+ }.into_parts()
+ ]);
Ok(())
}