summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-03-18 14:35:11 +0100
committerHampusM <hampus@hampusmat.com>2025-03-18 14:35:11 +0100
commitc1cf1b779e66e985774dad29867a57733947b0e8 (patch)
treefdee6b1aeb24b8f5fdb645dbd2e422ef3cba719f
parent7a7d3a350b22b5555c27debff6fee4fc6100fa38 (diff)
refactor(ecs): remove Component::self_id method
-rw-r--r--ecs-macros/src/lib.rs5
-rw-r--r--ecs/src/actions.rs4
-rw-r--r--ecs/src/component.rs25
-rw-r--r--ecs/src/component/storage.rs23
-rw-r--r--ecs/src/lib.rs14
5 files changed, 27 insertions, 44 deletions
diff --git a/ecs-macros/src/lib.rs b/ecs-macros/src/lib.rs
index b178022..b58671d 100644
--- a/ecs-macros/src/lib.rs
+++ b/ecs-macros/src/lib.rs
@@ -137,11 +137,6 @@ pub fn component_derive(input: TokenStream) -> TokenStream
#get_id
}
- fn self_id(&self) -> Uid
- {
- Self::id()
- }
-
fn get_event_uid(&self, event_kind: ComponentEventKind) -> Uid
{
match event_kind {
diff --git a/ecs/src/actions.rs b/ecs/src/actions.rs
index 184f811..7dff3a5 100644
--- a/ecs/src/actions.rs
+++ b/ecs/src/actions.rs
@@ -169,9 +169,9 @@ pub(crate) struct EventIds
#[derive(Debug)]
pub(crate) enum Action
{
- Spawn(Vec<Box<dyn Component>>, EventIds),
+ Spawn(Vec<(Uid, Box<dyn Component>)>, EventIds),
Despawn(Uid),
- AddComponents(Uid, Vec<Box<dyn Component>>, EventIds),
+ AddComponents(Uid, Vec<(Uid, Box<dyn Component>)>, EventIds),
RemoveComponents(Uid, Vec<ComponentMetadata>, EventIds),
Stop,
}
diff --git a/ecs/src/component.rs b/ecs/src/component.rs
index 77046d0..dc60995 100644
--- a/ecs/src/component.rs
+++ b/ecs/src/component.rs
@@ -39,9 +39,6 @@ pub trait Component: SystemInput + Any + TypeName
where
Self: Sized;
- /// The ID of the component `self`. Returns the same value as [`Component::id`].
- fn self_id(&self) -> Uid;
-
/// Returns the component UID of a component event for this component.
fn get_event_uid(&self, event_kind: ComponentEventKind) -> Uid;
@@ -116,11 +113,6 @@ where
ComponentT::id()
}
- fn self_id(&self) -> Uid
- {
- Self::id()
- }
-
fn get_event_uid(&self, event_kind: ComponentEventKind) -> Uid
{
match event_kind {
@@ -167,7 +159,7 @@ pub trait Sequence
/// The number of components in this component sequence.
const COUNT: usize;
- type Array: Array<Box<dyn Component>>;
+ type Array: Array<(Uid, Box<dyn Component>)>;
fn into_array(self) -> Self::Array;
@@ -235,15 +227,6 @@ impl Metadata
}
#[must_use]
- pub fn get<ComponentT: Component + ?Sized>(component: &ComponentT) -> Self
- {
- Self {
- id: component.self_id(),
- is_optional: component.self_is_optional(),
- }
- }
-
- #[must_use]
pub fn of<ComponentT: Component>() -> Self
{
Self {
@@ -291,11 +274,11 @@ macro_rules! inner {
{
const COUNT: usize = $c + 1;
- type Array = [Box<dyn Component>; $c + 1];
+ type Array = [(Uid, Box<dyn Component>); $c + 1];
fn into_array(self) -> Self::Array
{
- [#(Box::new(self.I) as Box<dyn Component>,)*]
+ [#((Comp~I::id(), Box::new(self.I) as Box<dyn Component>),)*]
}
fn metadata() -> impl Array<Metadata>
@@ -372,7 +355,7 @@ seq!(C in 0..=16 {
impl Sequence for ()
{
- type Array = [Box<dyn Component>; 0];
+ type Array = [(Uid, Box<dyn Component>); 0];
const COUNT: usize = 0;
diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs
index f2306b7..5b7b101 100644
--- a/ecs/src/component/storage.rs
+++ b/ecs/src/component/storage.rs
@@ -138,9 +138,11 @@ impl Storage
pub fn add_entity_component(
&mut self,
entity_uid: Uid,
- component: Box<dyn Component>,
+ component: (Uid, Box<dyn Component>),
) -> Result<(), Error>
{
+ let (component_id, component) = component;
+
debug_assert!(
!component.self_is_optional(),
"Adding a optional component to a entity is not supported"
@@ -159,16 +161,16 @@ impl Storage
if archetype_node
.archetype()
- .has_component_with_id(component.self_id())
+ .has_component_with_id(component_id)
{
return Err(Error::ComponentAlreadyInEntity {
entity: entity_uid,
- component: component.self_id(),
+ component: component_id,
});
}
let add_edge_archetype_id = archetype_node
- .get_or_insert_edges(component.self_id(), ArchetypeEdges::default)
+ .get_or_insert_edges(component_id, ArchetypeEdges::default)
.add
.unwrap_or_else(|| {
let archetype_node = self
@@ -177,10 +179,10 @@ impl Storage
.expect("Archetype should exist");
let (add_edge_id, add_edge_comp_ids) =
- archetype_node.make_add_edge(component.self_id());
+ archetype_node.make_add_edge(component_id);
archetype_node
- .get_edges_mut(component.self_id())
+ .get_edges_mut(component_id)
.expect("Edges for component in archetype should exist")
.add = Some(add_edge_id);
@@ -198,7 +200,7 @@ impl Storage
.expect("Add edge archetype should exist");
let add_edge_archetype_edges = add_edge_archetype_node
- .get_or_insert_edges(component.self_id(), ArchetypeEdges::default);
+ .get_or_insert_edges(component_id, ArchetypeEdges::default);
add_edge_archetype_edges.remove = Some(archetype_id);
}
@@ -220,10 +222,13 @@ impl Storage
.archetype_mut();
let component_index = add_edge_archetype
- .get_index_for_component(component.self_id())
+ .get_index_for_component(component_id)
.expect("Archetype should have index for component");
- entity.components.insert(component_index, component.into());
+ entity.components.insert(
+ component_index,
+ EntityComponent::new(component_id, component),
+ );
add_edge_archetype.push_entity(entity);
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs
index 504a106..ad83d1e 100644
--- a/ecs/src/lib.rs
+++ b/ecs/src/lib.rs
@@ -452,13 +452,13 @@ impl World
fn add_entity_components(
entity_uid: Uid,
- components: impl IntoIterator<Item = Box<dyn Component>>,
+ components: impl IntoIterator<Item = (Uid, Box<dyn Component>)>,
component_storage: &mut ComponentStorage,
)
{
- for component in components {
- if let Err(err) =
- component_storage.add_entity_component(entity_uid, component)
+ for (component_id, component) in components {
+ if let Err(err) = component_storage
+ .add_entity_component(entity_uid, (component_id, component))
{
tracing::error!("Failed to add component to entity: {err}");
}
@@ -553,12 +553,12 @@ pub struct EntityComponent
pub component: Lock<Box<dyn Component>>,
}
-impl From<Box<dyn Component>> for EntityComponent
+impl EntityComponent
{
- fn from(component: Box<dyn Component>) -> Self
+ pub fn new(id: Uid, component: Box<dyn Component>) -> Self
{
Self {
- id: component.self_id(),
+ id,
name: component.type_name(),
component: Lock::new(component),
}