summaryrefslogtreecommitdiff
path: root/ecs/src/lib.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-04-01 21:18:07 +0200
committerHampusM <hampus@hampusmat.com>2025-04-01 21:18:07 +0200
commitdd6bef9a9d04a56b088379468b4156057ca0efe9 (patch)
treefb9e7aa1c744943e3f83e1e89ccb49ee9d22d235 /ecs/src/lib.rs
parent727756c717bca951fc6f8e25d4d206b95d62fbb9 (diff)
refactor(ecs): make component storage more encapsulated
Diffstat (limited to 'ecs/src/lib.rs')
-rw-r--r--ecs/src/lib.rs32
1 files changed, 16 insertions, 16 deletions
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs
index 59129b0..3caaa6b 100644
--- a/ecs/src/lib.rs
+++ b/ecs/src/lib.rs
@@ -10,6 +10,7 @@ use std::sync::Arc;
use hashbrown::HashMap;
use crate::actions::Action;
+use crate::component::storage::archetype::EntityComponent as ArchetypeEntityComponent;
use crate::component::storage::Storage as ComponentStorage;
use crate::component::{Component, Sequence as ComponentSequence};
use crate::entity::CREATE_STATIC_ENTITIES;
@@ -467,7 +468,7 @@ impl World
{
let mut component_storage_lock = self.lock_component_storage_rw();
- let components = match component_storage_lock.remove_entity(entity_uid) {
+ let removed_entity = match component_storage_lock.remove_entity(entity_uid) {
Ok(components) => components,
Err(err) => {
tracing::error!("Failed to despawn entity: {err}");
@@ -475,16 +476,17 @@ impl World
}
};
- let component_removed_event_uids = components
+ let component_removed_event_uids = removed_entity
+ .components()
.iter()
.map(|component| {
component
- .component
+ .component()
.read_nonblock()
.unwrap_or_else(|_| {
panic!(
"Failed to acquire read-only {} component lock",
- component.name
+ component.name()
)
})
.get_event_uid(ComponentEventKind::Removed)
@@ -597,23 +599,21 @@ pub struct WorldData
}
#[derive(Debug)]
-#[non_exhaustive]
-pub struct EntityComponent
+pub struct EntityComponentRef<'a>
{
- pub id: Uid,
- pub name: &'static str,
- pub component: Lock<Box<dyn Component>>,
+ comp: &'a ArchetypeEntityComponent,
}
-impl EntityComponent
+impl<'a> EntityComponentRef<'a>
{
- pub fn new(id: Uid, component: Box<dyn Component>) -> Self
+ pub fn component(&self) -> &'a Lock<Box<dyn Component>>
{
- Self {
- id,
- name: component.type_name(),
- component: Lock::new(component),
- }
+ self.comp.component()
+ }
+
+ fn new(comp: &'a ArchetypeEntityComponent) -> Self
+ {
+ Self { comp }
}
}