summaryrefslogtreecommitdiff
path: root/ecs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-03-06 00:23:07 +0100
committerHampusM <hampus@hampusmat.com>2024-03-06 00:23:07 +0100
commitdba19db5f649f73a5abb1bc3589580721a9a4e32 (patch)
tree7fe133d807a834919c983ce1e6535b920a46b4bb /ecs
parente246f90bc33fd489e300366e30354e34a05b8107 (diff)
refactor(ecs): pass around all world data and not component storage
Diffstat (limited to 'ecs')
-rw-r--r--ecs/src/component.rs4
-rw-r--r--ecs/src/lib.rs53
-rw-r--r--ecs/src/system.rs24
-rw-r--r--ecs/src/system/stateful.rs14
4 files changed, 44 insertions, 51 deletions
diff --git a/ecs/src/component.rs b/ecs/src/component.rs
index 023be86..7a997c5 100644
--- a/ecs/src/component.rs
+++ b/ecs/src/component.rs
@@ -5,7 +5,7 @@ use std::ops::{Deref, DerefMut};
use seq_macro::seq;
use crate::system::{Input as SystemInput, Param as SystemParam, System};
-use crate::ComponentStorage;
+use crate::WorldData;
pub trait Component: SystemInput + Any
{
@@ -167,7 +167,7 @@ where
fn new<SystemImpl>(
system: &'world mut impl System<SystemImpl>,
- _component_storage: &'world mut ComponentStorage,
+ _world_data: &'world mut WorldData,
) -> Self
{
let local_component = system
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs
index e22913f..0c06472 100644
--- a/ecs/src/lib.rs
+++ b/ecs/src/lib.rs
@@ -23,18 +23,17 @@ pub mod tuple;
pub use ecs_macros::Component;
-#[derive(Debug)]
+#[derive(Debug, Default)]
struct Entity
{
components: Vec<Box<dyn Component>>,
}
-#[derive(Debug)]
+#[derive(Debug, Default)]
pub struct World
{
systems: Vec<TypeErasedSystem>,
- events: HashMap<EventId, Vec<usize>>,
- component_storage: ComponentStorage,
+ data: WorldData,
}
impl World
@@ -42,18 +41,15 @@ impl World
#[must_use]
pub fn new() -> Self
{
- Self {
- systems: Vec::new(),
- component_storage: ComponentStorage { entities: Vec::new() },
- events: HashMap::new(),
- }
+ Self::default()
}
pub fn create_entity<Comps>(&mut self, components: Comps)
where
Comps: ComponentSequence,
{
- self.component_storage
+ self.data
+ .component_storage
.entities
.push(Entity { components: components.into_vec() });
}
@@ -67,7 +63,8 @@ impl World
{
self.systems.push(system.into_type_erased());
- self.events
+ self.data
+ .events
.entry(event.id())
.or_default()
.push(self.systems.len() - 1);
@@ -80,14 +77,14 @@ impl World
/// Will panic if a system has dissapeared.
pub fn emit(&mut self, event: &impl Event)
{
- let Some(system_indices) = self.events.get(&event.id()).cloned() else {
+ let Some(system_indices) = self.data.events.get(&event.id()).cloned() else {
return;
};
for system_index in system_indices {
let system = self.systems.get_mut(system_index).unwrap();
- system.run(&mut self.component_storage);
+ system.run(&mut self.data);
}
}
@@ -95,16 +92,15 @@ impl World
where
Comps: ComponentSequence,
{
- Query::new(&mut self.component_storage)
+ Query::new(&mut self.data)
}
}
-impl Default for World
+#[derive(Debug, Default)]
+pub struct WorldData
{
- fn default() -> Self
- {
- Self::new()
- }
+ events: HashMap<EventId, Vec<usize>>,
+ component_storage: ComponentStorage,
}
#[derive(Debug)]
@@ -112,7 +108,7 @@ pub struct Query<'world, Comps>
where
Comps: ComponentSequence,
{
- component_storage: &'world mut ComponentStorage,
+ world_data: &'world mut WorldData,
comps_pd: PhantomData<Comps>,
}
@@ -120,12 +116,9 @@ impl<'world, Comps> Query<'world, Comps>
where
Comps: ComponentSequence,
{
- fn new(component_storage: &'world mut ComponentStorage) -> Self
+ fn new(world_data: &'world mut WorldData) -> Self
{
- Self {
- component_storage,
- comps_pd: PhantomData,
- }
+ Self { world_data, comps_pd: PhantomData }
}
}
@@ -136,7 +129,7 @@ where
pub fn iter(&self) -> QueryComponentIter<Comps>
{
QueryComponentIter {
- entity_iter: self.component_storage.entities.iter(),
+ entity_iter: self.world_data.component_storage.entities.iter(),
component_type_ids: Comps::type_ids(),
comps_pd: PhantomData,
}
@@ -145,7 +138,7 @@ where
pub fn iter_mut(&mut self) -> QueryComponentMutIter<Comps>
{
QueryComponentMutIter {
- entity_iter: self.component_storage.entities.iter_mut(),
+ entity_iter: self.world_data.component_storage.entities.iter_mut(),
component_type_ids: Comps::type_ids(),
comps_pd: PhantomData,
}
@@ -191,10 +184,10 @@ where
fn new<SystemImpl>(
_system: &'world mut impl System<SystemImpl>,
- component_storage: &'world mut ComponentStorage,
+ world_data: &'world mut WorldData,
) -> Self
{
- Self::new(component_storage)
+ Self::new(world_data)
}
fn is_compatible<Other: SystemParam<'world>>() -> bool
@@ -338,7 +331,7 @@ where
})
}
-#[derive(Debug)]
+#[derive(Debug, Default)]
pub struct ComponentStorage
{
entities: Vec<Entity>,
diff --git a/ecs/src/system.rs b/ecs/src/system.rs
index a49edda..96f0254 100644
--- a/ecs/src/system.rs
+++ b/ecs/src/system.rs
@@ -8,7 +8,7 @@ use seq_macro::seq;
use crate::component::Component;
use crate::system::util::check_params_are_compatible;
use crate::tuple::{FilterElement as TupleFilterElement, With as TupleWith};
-use crate::ComponentStorage;
+use crate::WorldData;
pub mod stateful;
@@ -21,7 +21,7 @@ pub trait System<Impl>: 'static
#[must_use]
fn initialize(self, input: Self::Input) -> Self;
- fn run(&mut self, component_storage: &mut ComponentStorage);
+ fn run(&mut self, world_data: &mut WorldData);
fn into_type_erased(self) -> TypeErased;
@@ -51,7 +51,7 @@ macro_rules! impl_system {
self
}
- fn run(&mut self, component_storage: &mut ComponentStorage)
+ fn run(&mut self, world_data: &mut WorldData)
{
#(
check_params_are_compatible!(I, TParam~I, $c);
@@ -66,11 +66,11 @@ macro_rules! impl_system {
};
// SAFETY: All parameters are compatible so this is fine
- let component_storage = unsafe {
- &mut *addr_of_mut!(*component_storage)
+ let world_data = unsafe {
+ &mut *addr_of_mut!(*world_data)
};
- TParam~I::new(this, component_storage)
+ TParam~I::new(this, world_data)
},)*);
}
@@ -78,10 +78,10 @@ macro_rules! impl_system {
{
TypeErased {
data: Box::new(self),
- func: Box::new(|data, component_storage| {
+ func: Box::new(|data, world_data| {
let me = data.downcast_mut::<Func>().unwrap();
- me.run(component_storage);
+ me.run(world_data);
}),
}
}
@@ -123,9 +123,9 @@ pub struct TypeErased
impl TypeErased
{
- pub fn run(&mut self, component_storage: &mut ComponentStorage)
+ pub fn run(&mut self, world_data: &mut WorldData)
{
- (self.func)(self.data.as_mut(), component_storage);
+ (self.func)(self.data.as_mut(), world_data);
}
}
@@ -138,7 +138,7 @@ impl Debug for TypeErased
}
/// Function in [`TypeErased`] used to run the system.
-type TypeErasedFunc = dyn Fn(&mut dyn Any, &mut ComponentStorage);
+type TypeErasedFunc = dyn Fn(&mut dyn Any, &mut WorldData);
/// A parameter to a [`System`].
///
@@ -153,7 +153,7 @@ pub unsafe trait Param<'world>
fn new<SystemImpl>(
system: &'world mut impl System<SystemImpl>,
- component_storage: &'world mut ComponentStorage,
+ world_data: &'world mut WorldData,
) -> Self;
fn is_compatible<Other: Param<'world>>() -> bool;
diff --git a/ecs/src/system/stateful.rs b/ecs/src/system/stateful.rs
index 7b136cf..3f71000 100644
--- a/ecs/src/system/stateful.rs
+++ b/ecs/src/system/stateful.rs
@@ -14,7 +14,7 @@ use crate::tuple::{
TakeOptionElementResult as TupleTakeOptionElementResult,
WithOptionElements as TupleWithOptionElements,
};
-use crate::ComponentStorage;
+use crate::WorldData;
/// A stateful system.
pub struct Stateful<Func>
@@ -76,7 +76,7 @@ macro_rules! impl_system {
self
}
- fn run(&mut self, component_storage: &mut ComponentStorage)
+ fn run(&mut self, world_data: &mut WorldData)
{
#(
check_params_are_compatible!(I, TParam~I, $c);
@@ -91,11 +91,11 @@ macro_rules! impl_system {
};
// SAFETY: All parameters are compatible so this is fine
- let component_storage = unsafe {
- &mut *addr_of_mut!(*component_storage)
+ let world_data = unsafe {
+ &mut *addr_of_mut!(*world_data)
};
- TParam~I::new(this, component_storage)
+ TParam~I::new(this, world_data)
},)*);
}
@@ -103,10 +103,10 @@ macro_rules! impl_system {
{
TypeErased {
data: Box::new(self),
- func: Box::new(|data, component_storage| {
+ func: Box::new(|data, world_data| {
let me = data.downcast_mut::<Self>().unwrap();
- me.run(component_storage);
+ me.run(world_data);
}),
}
}