diff --git a/lib/ecs/BUILD b/lib/ecs/BUILD index a5be4b1..33d5b39 100644 --- a/lib/ecs/BUILD +++ b/lib/ecs/BUILD @@ -8,8 +8,8 @@ rust_library( "src/borrow.rs", "src/component.rs", "src/componentmap.rs", + "src/globalmap.rs", "src/entity.rs", - "src/resourcemap.rs", "src/system.rs", "src/world.rs", ], diff --git a/lib/ecs/src/component.rs b/lib/ecs/src/component.rs index cd8ef28..5350980 100644 --- a/lib/ecs/src/component.rs +++ b/lib/ecs/src/component.rs @@ -8,9 +8,9 @@ pub fn component_id() -> ID { std::any::TypeId::of::() } -pub trait Resource: 'static { +pub trait Global: 'static { } -pub fn resource_id() -> ID { +pub fn global_id() -> ID { std::any::TypeId::of::() } diff --git a/lib/ecs/src/resourcemap.rs b/lib/ecs/src/globalmap.rs similarity index 65% rename from lib/ecs/src/resourcemap.rs rename to lib/ecs/src/globalmap.rs index 8c8beb5..a211f4e 100644 --- a/lib/ecs/src/resourcemap.rs +++ b/lib/ecs/src/globalmap.rs @@ -5,13 +5,13 @@ use std::ops::{Deref, DerefMut}; use crate::component; use crate::borrow; -struct ResourceMapEntry { - resource: Box, +struct GlobalMapEntry { + global: Box, borrow: Cell, } -pub struct ResourceMap { - value: UnsafeCell>, +pub struct GlobalMap { + value: UnsafeCell>, borrow: Cell, } @@ -24,7 +24,7 @@ impl AccessError { } } -impl ResourceMap { +impl GlobalMap { pub fn new() -> Self { Self { value: UnsafeCell::new(BTreeMap::new()), @@ -32,78 +32,78 @@ impl ResourceMap { } } - pub fn get<'a, T: component::Resource>(&'a self) -> Result, AccessError> { + pub fn get<'a, T: component::Global>(&'a self) -> Result, AccessError> { match borrow::RefMut::new(&self.borrow) { None => Err(AccessError::concurrent()), Some(b) => { let map = self.value.get(); unsafe { - match (*map).get(&component::resource_id::()) { + match (*map).get(&component::global_id::()) { Some(entry) => { - let val = &entry.resource; + let val = &entry.global; match borrow::Ref::new(&entry.borrow) { None => Err(AccessError::concurrent()), Some(b2) => { let val = val.as_ref(); - let val = & *(val as *const (dyn component::Resource) as *const T); + let val = & *(val as *const (dyn component::Global) as *const T); drop(b); - Ok(ResourceRef { val, borrow: Some(b2) }) + Ok(GlobalRef { val, borrow: Some(b2) }) }, } }, - None => Err(AccessError("resource absent from world".to_string())), + None => Err(AccessError("global absent from world".to_string())), } } } } } - pub fn get_mut<'a, T: component::Resource>(&'a self) -> Result, AccessError> { + pub fn get_mut<'a, T: component::Global>(&'a self) -> Result, AccessError> { match borrow::RefMut::new(&self.borrow) { None => Err(AccessError::concurrent()), Some(b) => { let map = self.value.get(); unsafe { - match (*map).get_mut(&component::resource_id::()) { + match (*map).get_mut(&component::global_id::()) { Some(entry) => { - let val = &mut entry.resource; + let val = &mut entry.global; match borrow::RefMut::new(&entry.borrow) { None => Err(AccessError::concurrent()), Some(b2) => { let val = val.as_mut(); - let val = &mut *(val as *mut (dyn component::Resource) as *mut T); + let val = &mut *(val as *mut (dyn component::Global) as *mut T); drop(b); - Ok(ResourceRefMut { val, borrow: Some(b2) }) + Ok(GlobalRefMut { val, borrow: Some(b2) }) }, } }, - None => Err(AccessError("resource absent from world".to_string())), + None => Err(AccessError("global absent from world".to_string())), } } } } } - pub fn set<'a, T: component::Resource>(&'a self, r: T) -> Result<(), AccessError> { + pub fn set<'a, T: component::Global>(&'a self, r: T) -> Result<(), AccessError> { match borrow::RefMut::new(&self.borrow) { None => Err(AccessError::concurrent()), Some(b) => { let map = self.value.get(); - let rid = component::resource_id::(); + let rid = component::global_id::(); unsafe { match (*map).get_mut(&rid) { Some(entry) => { match borrow::RefMut::new(&entry.borrow) { None => { return Err(AccessError::concurrent()); }, Some(b2) => { - entry.resource = Box::new(r); + entry.global = Box::new(r); drop(b2); }, }; }, None => { - (*map).insert(rid, ResourceMapEntry { - resource: Box::new(r), + (*map).insert(rid, GlobalMapEntry { + global: Box::new(r), borrow: Cell::new(borrow::UNUSED), }); }, @@ -116,12 +116,12 @@ impl ResourceMap { } } -pub struct ResourceRef<'a, T: component::Resource> { +pub struct GlobalRef<'a, T: component::Global> { val: *const T, borrow: Option>, } -impl<'a, T: component::Resource> Deref for ResourceRef<'a, T> { +impl<'a, T: component::Global> Deref for GlobalRef<'a, T> { type Target = T; fn deref(&self) -> &Self::Target { @@ -131,24 +131,24 @@ impl<'a, T: component::Resource> Deref for ResourceRef<'a, T> { } } -impl <'a, T: component::Resource> Drop for ResourceRef<'a, T> { +impl <'a, T: component::Global> Drop for GlobalRef<'a, T> { fn drop(&mut self) { self.borrow = None; } } -pub struct ResourceRefMut<'a, T: component::Resource> { +pub struct GlobalRefMut<'a, T: component::Global> { val: *mut T, borrow: Option>, } -impl <'a, T: component::Resource> Drop for ResourceRefMut<'a, T> { +impl <'a, T: component::Global> Drop for GlobalRefMut<'a, T> { fn drop(&mut self) { self.borrow = None; } } -impl <'a, T: component::Resource> Deref for ResourceRefMut<'a, T> { +impl <'a, T: component::Global> Deref for GlobalRefMut<'a, T> { type Target = T; fn deref(&self) -> &Self::Target { unsafe { @@ -157,7 +157,7 @@ impl <'a, T: component::Resource> Deref for ResourceRefMut<'a, T> { } } -impl <'a, T: component::Resource> DerefMut for ResourceRefMut<'a, T> { +impl <'a, T: component::Global> DerefMut for GlobalRefMut<'a, T> { fn deref_mut(&mut self) -> &mut Self::Target { unsafe { &mut(*self.val) diff --git a/lib/ecs/src/lib.rs b/lib/ecs/src/lib.rs index 381a9bb..9063045 100644 --- a/lib/ecs/src/lib.rs +++ b/lib/ecs/src/lib.rs @@ -2,17 +2,17 @@ pub mod borrow; pub mod component; pub mod componentmap; pub mod entity; -pub mod resourcemap; +pub mod globalmap; pub mod system; pub mod world; pub use component::Component as Component; -pub use component::Resource as Resource; +pub use component::Global as Global; pub use world::World as World; pub use world::ReadComponent as ReadComponent; pub use world::ReadWriteComponent as ReadWriteComponent; -pub use world::ReadResource as ReadResource; -pub use world::ReadWriteResource as ReadWriteResource; +pub use world::ReadGlobal as ReadGlobal; +pub use world::ReadWriteGlobal as ReadWriteGlobal; pub use system::System as System; pub use system::Join as Join; pub use system::Processor as Processor; diff --git a/lib/ecs/src/system.rs b/lib/ecs/src/system.rs index b646682..368ed44 100644 --- a/lib/ecs/src/system.rs +++ b/lib/ecs/src/system.rs @@ -6,7 +6,7 @@ use crate::{ world::{ ReadComponent, ReadComponentIter, ReadWriteComponent, ReadWriteComponentIter, - ReadResource, ReadWriteResource, + ReadGlobal, ReadWriteGlobal, World, } }; @@ -69,15 +69,15 @@ impl<'a, T: component::Component> AccessComponent<'a> for ReadWriteComponent<'a, } } -impl<'a, T: component::Resource> Access<'a> for ReadResource<'a, T> { +impl<'a, T: component::Global> Access<'a> for ReadGlobal<'a, T> { fn fetch(world: &'a World) -> Self { - world.resource() + world.global() } } -impl<'a, T: component::Resource> Access<'a> for ReadWriteResource<'a, T> { +impl<'a, T: component::Global> Access<'a> for ReadWriteGlobal<'a, T> { fn fetch(world: &'a World) -> Self { - world.resource_mut() + world.global_mut() } } @@ -197,21 +197,21 @@ impl <'a, J: Join<'a>> Iterator for JoinIter<'a, J> { mod test { use crate::{ component::Component, - component::Resource, + component::Global, system, system::Join, - world::{ReadComponent, ReadWriteComponent, ReadResource, ReadWriteResource, World}, + world::{ReadComponent, ReadWriteComponent, ReadGlobal, ReadWriteGlobal, World}, }; #[derive(Clone,Debug,Default)] struct Delta(f32); - impl Resource for Delta {} + impl Global for Delta {} #[derive(Clone,Debug)] struct PhysicsStatus { object_count: u64, } - impl Resource for PhysicsStatus {} + impl Global for PhysicsStatus {} #[derive(Clone,Debug)] struct Position { @@ -233,8 +233,8 @@ mod test { impl<'a> system::System<'a> for Physics { type SystemData = ( ReadWriteComponent<'a, Position> , ReadComponent<'a, Velocity> - , ReadResource<'a, Delta> - , ReadWriteResource<'a, PhysicsStatus>); + , ReadGlobal<'a, Delta> + , ReadWriteGlobal<'a, PhysicsStatus>); fn run(&mut self, (pos, vel, delta, status): Self::SystemData) { let d = delta.get(); @@ -254,8 +254,8 @@ mod test { let mut world = World::new(); world.new_entity().with(Velocity { x: 0.0, y: 0.0, z: 1.0 }).with(Position { x: 1.0, y: 2.0, z: 3.0 }).build(); world.new_entity().with(Velocity { x: 0.0, y: 0.0, z: 2.0 }).with(Position { x: 4.0, y: 5.0, z: 6.0 }).build(); - world.set_resource(Delta(1.0)); - world.set_resource(PhysicsStatus { object_count: 0u64 }); + world.set_global(Delta(1.0)); + world.set_global(PhysicsStatus { object_count: 0u64 }); let mut p = system::Processor { world: &world, @@ -267,9 +267,9 @@ mod test { assert_eq!(vec![3.0, 6.0], positions.iter().map(|(_, el)| el.z).collect::>()); p.run(); assert_eq!(vec![4.0, 8.0], positions.iter().map(|(_, el)| el.z).collect::>()); - world.set_resource(Delta(2.0)); + world.set_global(Delta(2.0)); p.run(); assert_eq!(vec![6.0, 12.0], positions.iter().map(|(_, el)| el.z).collect::>()); - assert_eq!(2, world.resource::().get().object_count); + assert_eq!(2, world.global::().get().object_count); } } diff --git a/lib/ecs/src/world.rs b/lib/ecs/src/world.rs index 3bfe525..cf668a0 100644 --- a/lib/ecs/src/world.rs +++ b/lib/ecs/src/world.rs @@ -7,10 +7,10 @@ use crate::componentmap::{ ComponentMapIter, ComponentMapIterMut, }; -use crate::resourcemap::{ - ResourceMap, - ResourceRef, - ResourceRefMut, +use crate::globalmap::{ + GlobalMap, + GlobalRef, + GlobalRefMut, }; use crate::entity; use crate::component; @@ -91,31 +91,31 @@ impl <'a, T: component::Component> Iterator for ReadWriteComponentIter<'a, T> { } } -pub struct ReadResource<'a, T: component::Resource> { +pub struct ReadGlobal<'a, T: component::Global> { world: &'a World, phantom: PhantomData<&'a T>, } -impl<'a, T: component::Resource> ReadResource<'a, T> { - pub fn get(&self) -> ResourceRef<'a, T> { - self.world.resources.get::().unwrap() +impl<'a, T: component::Global> ReadGlobal<'a, T> { + pub fn get(&self) -> GlobalRef<'a, T> { + self.world.globals.get::().unwrap() } } -pub struct ReadWriteResource<'a, T: component::Resource> { +pub struct ReadWriteGlobal<'a, T: component::Global> { world: &'a World, phantom: PhantomData<&'a T>, } -impl<'a, T: component::Resource> ReadWriteResource<'a, T> { - pub fn get(&self) -> ResourceRefMut<'a, T> { - self.world.resources.get_mut::().unwrap() +impl<'a, T: component::Global> ReadWriteGlobal<'a, T> { + pub fn get(&self) -> GlobalRefMut<'a, T> { + self.world.globals.get_mut::().unwrap() } } pub struct World { components: BTreeMap, - resources: ResourceMap, + globals: GlobalMap, next_id: entity::ID, } @@ -123,7 +123,7 @@ impl World { pub fn new() -> Self { Self { components: BTreeMap::new(), - resources: ResourceMap::new(), + globals: GlobalMap::new(), next_id: 1u64, } } @@ -158,22 +158,22 @@ impl World { } } - pub fn resource<'a, T: component::Resource>(&'a self) -> ReadResource<'a, T> { - ReadResource { + pub fn global<'a, T: component::Global>(&'a self) -> ReadGlobal<'a, T> { + ReadGlobal { world: self, phantom: PhantomData, } } - pub fn resource_mut<'a, T: component::Resource>(&'a self) -> ReadWriteResource<'a, T> { - ReadWriteResource { + pub fn global_mut<'a, T: component::Global>(&'a self) -> ReadWriteGlobal<'a, T> { + ReadWriteGlobal { world: self, phantom: PhantomData, } } - pub fn set_resource(&self, r: T) { - self.resources.set(r).unwrap(); + pub fn set_global(&self, r: T) { + self.globals.set(r).unwrap(); } }