diff options
author | HampusM <hampus@hampusmat.com> | 2025-04-08 16:28:46 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-04-08 16:28:46 +0200 |
commit | e4818dd4f0a57a2c9af8859253f570607f64bb12 (patch) | |
tree | 981e6e927b086882ebc2eb9399ee41fe4755d887 /ecs/src/component | |
parent | 89036adeeec7de82203c819c77950a7728cfe7f9 (diff) |
refactor(ecs): store components as dyn Any
Diffstat (limited to 'ecs/src/component')
-rw-r--r-- | ecs/src/component/storage.rs | 13 | ||||
-rw-r--r-- | ecs/src/component/storage/archetype.rs | 16 |
2 files changed, 7 insertions, 22 deletions
diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index 14aa191..53f51f2 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -1,3 +1,4 @@ +use std::any::Any; use std::array::IntoIter as ArrayIter; use std::cell::RefCell; use std::vec::IntoIter as VecIntoIter; @@ -16,7 +17,6 @@ use crate::component::storage::graph::{ ArchetypeEdges, Graph, }; -use crate::component::Component; use crate::uid::{Kind as UidKind, Uid}; use crate::util::{BorrowedOrOwned, Either, StreamingIterator, VecExt}; @@ -159,18 +159,9 @@ impl Storage pub fn add_entity_component( &mut self, entity_uid: Uid, - (component_id, component_name, component): ( - Uid, - &'static str, - Box<dyn Component>, - ), + (component_id, component_name, component): (Uid, &'static str, Box<dyn Any>), ) -> Result<(), Error> { - debug_assert!( - !component.self_is_optional(), - "Adding a optional component to a entity is not supported" - ); - let Some(archetype_id) = self.entity_archetype_lookup.get(&entity_uid) else { return Err(Error::EntityDoesNotExist(entity_uid)); }; diff --git a/ecs/src/component/storage/archetype.rs b/ecs/src/component/storage/archetype.rs index 8d48e13..58facc9 100644 --- a/ecs/src/component/storage/archetype.rs +++ b/ecs/src/component/storage/archetype.rs @@ -1,9 +1,10 @@ +use std::any::Any; use std::hash::{DefaultHasher, Hash, Hasher}; use std::slice::Iter as SliceIter; use hashbrown::HashMap; -use crate::component::{Component, Metadata as ComponentMetadata}; +use crate::component::Metadata as ComponentMetadata; use crate::lock::Lock; use crate::uid::{Kind as UidKind, Uid}; use crate::util::HashMapExt; @@ -209,26 +210,19 @@ impl Entity #[derive(Debug)] pub struct EntityComponent { - name: &'static str, - component: Lock<Box<dyn Component>>, + component: Lock<Box<dyn Any>>, } impl EntityComponent { - pub fn new(component: Box<dyn Component>, component_name: &'static str) -> Self + pub fn new(component: Box<dyn Any>, component_name: &'static str) -> Self { Self { - name: component_name, component: Lock::new(component, component_name), } } - pub fn name(&self) -> &str - { - self.name - } - - pub fn component(&self) -> &Lock<Box<dyn Component>> + pub fn component(&self) -> &Lock<Box<dyn Any>> { &self.component } |