simple queries (which will be completely rewritten

This commit is contained in:
SeanOMik 2023-05-25 22:59:27 -04:00
parent cde7d140ea
commit f62f21e69f
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
5 changed files with 41 additions and 16 deletions

View File

@ -1,6 +1,6 @@
use std::any::{Any, TypeId};
use crate::{world::{Entity, ArchetypeEntityId}, bundle::Bundle};
use crate::{world::{Entity, ArchetypeEntityId}, bundle::Bundle, component::Component};
pub trait ComponentColumn: Any {
fn as_any(&self) -> &dyn Any;
@ -15,7 +15,7 @@ pub trait ComponentColumn: Any {
fn component_type_name(&self) -> String;
}
impl<T: Send + Sync + 'static> ComponentColumn for Vec<T> {
impl<T: Component> ComponentColumn for Vec<T> {
fn as_any(&self) -> &dyn Any {
self
}
@ -74,7 +74,7 @@ pub struct Archetype {
impl Archetype {
/// Create a new archetype from another archetype and add a column
pub fn new_archetype_add<T: Send + Sync + 'static>(new_id: ArchetypeId, archetype: &Archetype) -> Archetype {
pub fn new_archetype_add<T: Component>(new_id: ArchetypeId, archetype: &Archetype) -> Archetype {
let mut columns: Vec<_> = archetype
.columns
.iter()
@ -95,7 +95,7 @@ impl Archetype {
}
/// Create a new archetype from another archetype and remove a column
pub fn new_archetype_remove<T: Send + Sync + 'static>(new_id: ArchetypeId, archetype: &Archetype) -> Archetype {
pub fn new_archetype_remove<T: Component>(new_id: ArchetypeId, archetype: &Archetype) -> Archetype {
let mut columns: Vec<_> = archetype
.columns
.iter()
@ -123,7 +123,7 @@ impl Archetype {
}
}
pub fn get_component_mut<T: Send + Sync + 'static>(&mut self, entity: ArchetypeEntityId) -> Option<&mut T> {
pub fn get_component_mut<T: Component>(&mut self, entity: ArchetypeEntityId) -> Option<&mut T> {
for col in self.columns.iter_mut() {
if col.as_any().is::<Vec<T>>() {
let components: &mut Vec<T> = col.as_any_mut().downcast_mut().unwrap();
@ -135,7 +135,7 @@ impl Archetype {
None
}
pub fn get_component<T: Send + Sync + 'static>(&self, entity: ArchetypeEntityId) -> Option<&T> {
pub fn get_component<T: Component>(&self, entity: ArchetypeEntityId) -> Option<&T> {
for col in self.columns.iter() {
if col.as_ref().as_any().is::<Vec<T>>() {
let components: &Vec<T> = col.as_any().downcast_ref().unwrap();
@ -147,6 +147,11 @@ impl Archetype {
None
}
pub fn get_component_column<T: Component>(&self) -> Option<&Vec<T>> {
let col = self.columns.iter().find(|c| c.as_any().is::<Vec<T>>())?;
col.as_any().downcast_ref()
}
pub(crate) fn add_entity(&mut self, components: Vec<Box<dyn ComponentColumn>>) -> ArchetypeEntityId {
let mut created_entity: Option<ArchetypeEntityId> = None;

View File

@ -1,6 +1,6 @@
use std::any::{TypeId, Any};
use crate::archetype::ComponentColumn;
use crate::{archetype::ComponentColumn, component::Component};
pub trait Bundle {
// Get a list of type ids that this bundle is storing
@ -13,7 +13,7 @@ pub trait Bundle {
macro_rules! impl_bundle_tuple {
( $(($name: ident, $index: tt))+ ) => (
impl<$($name: Send + Sync + 'static),+> Bundle for ($($name,)+) {
impl<$($name: Component),+> Bundle for ($($name,)+) {
fn types(&self) -> Vec<TypeId> {
vec![$(self.$index.type_id()),+]
}

View File

@ -1,5 +1,3 @@
use std::any::Any;
pub trait Component : Any {
}
/// Shorthand for `Send + Sync + 'static`, so it never needs to be implemented manually.
pub trait Component: Send + Sync + 'static {}
impl<T: Send + Sync + 'static> Component for T {}

View File

@ -1,5 +1,3 @@
use std::any::Any;
use crate::world::World;
mod archetype;
@ -21,4 +19,13 @@ fn main() {
} else {
println!("no component found :(");
}
let pos = Position2d(836, 348);
let _e = world.spawn((pos,));
println!("\nstart of querying!\n");
for pos in world.query::<Position2d>() {
println!("Queried Position2d: {:?}", pos);
}
}

View File

@ -1,4 +1,5 @@
use std::{collections::{HashMap, VecDeque}, any::{Any, TypeId}};
use std::slice::Iter;
use crate::{archetype::{ArchetypeId, Archetype}, bundle::Bundle, component::Component};
@ -103,10 +104,24 @@ impl World {
new_entity
}
pub fn get_component<T: Send + Sync + 'static>(&self, entity: Entity) -> Option<&T> {
pub fn get_component<T: Component>(&self, entity: Entity) -> Option<&T> {
let record = self.entity_index.get(&entity.id)?;
let archetype = self.archetypes.get(&record.id)?;
archetype.get_component(record.index)
}
pub fn query<T: Component>(&self) -> impl Iterator<Item = &T> {
self.archetypes
.iter()
.filter_map(|(_, a)| a.get_component_column::<T>())
.flatten()
}
/* pub fn query_m<B: Bundle>(&self) -> impl Iterator<Item = &T> {
self.archetypes
.iter()
.filter_map(|(_, a)| a.get_component_column::<T>())
.flatten()
} */
}