From e4818dd4f0a57a2c9af8859253f570607f64bb12 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 8 Apr 2025 16:28:46 +0200 Subject: refactor(ecs): store components as dyn Any --- ecs/src/component/storage.rs | 13 ++----------- ecs/src/component/storage/archetype.rs | 16 +++++----------- 2 files changed, 7 insertions(+), 22 deletions(-) (limited to 'ecs/src/component') 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, - ), + (component_id, component_name, component): (Uid, &'static str, Box), ) -> 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>, + component: Lock>, } impl EntityComponent { - pub fn new(component: Box, component_name: &'static str) -> Self + pub fn new(component: Box, 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> + pub fn component(&self) -> &Lock> { &self.component } -- cgit v1.2.3-18-g5258