From da206b48248d0904ba78864146884ebdc87976a4 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Sat, 9 Dec 2023 18:55:57 -0500 Subject: [PATCH] Rename World::view to World::view_iter, some code cleanup --- lyra-ecs/src/query/dynamic/mod.rs | 28 +++++----------------------- lyra-ecs/src/query/dynamic/view.rs | 12 ++++++------ lyra-ecs/src/query/resource.rs | 14 +++++++------- lyra-ecs/src/query/tuple.rs | 2 +- lyra-ecs/src/world.rs | 10 +++++++--- 5 files changed, 26 insertions(+), 40 deletions(-) diff --git a/lyra-ecs/src/query/dynamic/mod.rs b/lyra-ecs/src/query/dynamic/mod.rs index 151d533..3ba11a3 100644 --- a/lyra-ecs/src/query/dynamic/mod.rs +++ b/lyra-ecs/src/query/dynamic/mod.rs @@ -5,18 +5,16 @@ use crate::{Fetch, world::World, Query, ComponentColumn, ComponentInfo}; pub mod view; pub use view::*; -#[derive(Clone, Copy, Hash)] -enum TypeId { - Rust(std::any::TypeId), - Unknown(u128), -} - /// Data that rust does not know the type of pub struct DynamicType { pub info: ComponentInfo, pub ptr: NonNull, } +/// A struct that fetches some dynamic type. +/// +/// Currently it can only fetch from archetypes, later it will be able to fetch dynamic +/// resources as well. Its meant to be a single Fetcher for all dynamic types. pub struct FetchDynamicType<'a> { col: &'a ComponentColumn, info: ComponentInfo, @@ -41,10 +39,6 @@ impl<'a> Fetch<'a> for FetchDynamicType<'a> { } } -pub trait DynamicQuery: Query { - -} - /// A query for receiving a dynamic type. /// /// There are no Ref or RefMut variants since this fetches the pointer to the component. @@ -59,24 +53,12 @@ impl QueryDynamicType { info, } } -} - -impl Query for QueryDynamicType { - type Item<'a> = DynamicType; - - type Fetch<'a> = FetchDynamicType<'a>; - - const ALWAYS_FETCHES: bool = false; - - fn new() -> Self { - panic!("QueryDynamicType does not implement QueryDefault, this should not have been called") - } fn can_visit_archetype(&self, archetype: &crate::archetype::Archetype) -> bool { archetype.has_column(self.info.type_id) } - unsafe fn fetch<'a>(&self, _world: &'a World, _arch_id: crate::archetype::ArchetypeId, archetype: &'a crate::archetype::Archetype) -> Self::Fetch<'a> { + unsafe fn fetch<'a>(&self, _world: &'a World, _arch_id: crate::archetype::ArchetypeId, archetype: &'a crate::archetype::Archetype) -> FetchDynamicType<'a> { let col = archetype.columns.iter().find(|c| c.info.type_id == self.info.type_id) .expect("You ignored 'can_visit_archetype'!"); diff --git a/lyra-ecs/src/query/dynamic/view.rs b/lyra-ecs/src/query/dynamic/view.rs index 8209c21..2ef7b96 100644 --- a/lyra-ecs/src/query/dynamic/view.rs +++ b/lyra-ecs/src/query/dynamic/view.rs @@ -1,8 +1,8 @@ use std::ops::Range; -use crate::{world::World, Archetype, Query, ArchetypeEntityId, Fetch, ArchetypeId}; +use crate::{world::World, Archetype, ArchetypeEntityId, Fetch, ArchetypeId}; -use super::{DynamicQuery, QueryDynamicType, FetchDynamicType}; +use super::{QueryDynamicType, FetchDynamicType, DynamicType}; pub struct DynamicView<'a> { world: &'a World, @@ -23,7 +23,7 @@ impl<'a> DynamicView<'a> { } impl<'a> IntoIterator for DynamicView<'a> { - type Item = Vec<::Item<'a>>; + type Item = Vec; type IntoIter = DynamicViewIter<'a>; @@ -34,7 +34,7 @@ impl<'a> IntoIterator for DynamicView<'a> { world: self.world, queries: self.queries, fetchers: Vec::new(), - archetypes: archetypes, + archetypes, next_archetype: 0, component_indices: 0..0, } @@ -51,7 +51,7 @@ pub struct DynamicViewIter<'a> { } impl<'a> Iterator for DynamicViewIter<'a> { - type Item = Vec<::Item<'a>>; + type Item = Vec; fn next(&mut self) -> Option { loop { @@ -104,7 +104,7 @@ impl<'a> Iterator for DynamicViewIter<'a> { mod tests { use std::{alloc::Layout, ptr::NonNull}; - use crate::{world::World, MemoryLayout, ComponentInfo, DynTypeId, DynamicBundle, dynamic::{DynamicQuery, QueryDynamicType}}; + use crate::{world::World, MemoryLayout, ComponentInfo, DynTypeId, DynamicBundle, dynamic::QueryDynamicType}; use super::DynamicView; diff --git a/lyra-ecs/src/query/resource.rs b/lyra-ecs/src/query/resource.rs index 7d29b57..514763b 100644 --- a/lyra-ecs/src/query/resource.rs +++ b/lyra-ecs/src/query/resource.rs @@ -167,7 +167,7 @@ mod tests { println!("Added resource"); } - let mut res_iter = world.view::>(); + let mut res_iter = world.view_iter::>(); let res = res_iter.next().unwrap(); assert_eq!(res.0, 0); } @@ -187,16 +187,16 @@ mod tests { println!("Added resource"); } - let i = world.view::<(QueryResource, &Vec2)>(); + let i = world.view_iter::<(QueryResource, &Vec2)>(); assert_eq!(i.count(), 3); - let i = world.view::<(&Vec2, QueryResource)>(); + let i = world.view_iter::<(&Vec2, QueryResource)>(); assert_eq!(i.count(), 3); - for (res, e) in world.view::<(QueryResource, &Vec2)>() { + for (res, e) in world.view_iter::<(QueryResource, &Vec2)>() { println!("Got res {}! and entity at {:?}", res.0, e); } - let i = world.view::>(); + let i = world.view_iter::>(); assert_eq!(i.count(), 1); } @@ -210,13 +210,13 @@ mod tests { } { - let mut resmut_iter = world.view::>(); + let mut resmut_iter = world.view_iter::>(); let mut resmut = resmut_iter.next().unwrap(); assert_eq!(resmut.0, 0); resmut.0 += 20; } - let mut res_iter = world.view::>(); + let mut res_iter = world.view_iter::>(); let res = res_iter.next().unwrap(); assert_eq!(res.0, 20); } diff --git a/lyra-ecs/src/query/tuple.rs b/lyra-ecs/src/query/tuple.rs index 99a3682..95966a0 100644 --- a/lyra-ecs/src/query/tuple.rs +++ b/lyra-ecs/src/query/tuple.rs @@ -165,7 +165,7 @@ mod tests { world.spawn(comps[0]); world.spawn(comps[1]); - for (pos2, pos3) in world.view::<(&Vec2, &Vec3)>() { + for (pos2, pos3) in world.view_iter::<(&Vec2, &Vec3)>() { println!("Entity at {:?} and {:?}", pos2, pos3); assert!(comps.contains(&( pos2.clone(), pos3.clone() ))); } diff --git a/lyra-ecs/src/world.rs b/lyra-ecs/src/world.rs index ad8547e..8d32ecd 100644 --- a/lyra-ecs/src/world.rs +++ b/lyra-ecs/src/world.rs @@ -1,6 +1,6 @@ use std::{collections::{HashMap, VecDeque}, any::TypeId, cell::{Ref, RefMut}}; -use crate::{archetype::{ArchetypeId, Archetype}, bundle::Bundle, component::Component, query::{ViewIter, View}, resource::ResourceData, Query, AsQuery}; +use crate::{archetype::{ArchetypeId, Archetype}, bundle::Bundle, component::Component, query::{ViewIter, View}, resource::ResourceData, Query, AsQuery, dynamic::{DynamicViewIter, QueryDynamicType, DynamicView}}; #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub struct EntityId(pub u64); @@ -115,12 +115,16 @@ impl World { } } - pub fn view<'a, T: 'static + Component + AsQuery>(&'a self) -> ViewIter<'a, T::Query> { + pub fn view_iter<'a, T: 'static + Component + AsQuery>(&'a self) -> ViewIter<'a, T::Query> { let archetypes = self.archetypes.values().collect(); let v = View::new(self, T::Query::new(), archetypes); v.into_iter() } + pub fn dynamic_view(&self) -> DynamicView { + DynamicView::new(self) + } + pub fn add_resource(&mut self, data: T) { self.resources.insert(TypeId::of::(), ResourceData::new(data)); } @@ -196,7 +200,7 @@ mod tests { }, )); let mut count = 0; - for pos in world.view::<&Vec2>() { + for pos in world.view_iter::<&Vec2>() { println!("Found entity at {:?}", pos); count += 1; }