Compare commits

..

No commits in common. "540be64f6a59e89641d4afccf90af473ea055d91" and "da5e137432613ee916618c0485d93bb0314edee4" have entirely different histories.

5 changed files with 22 additions and 185 deletions

View file

@ -4,20 +4,18 @@ use std::{
time::Duration, time::Duration,
}; };
use crate::{resources::Resource, world::ComponentChange, *}; use crate::{world::ComponentChange, *};
enum CommandTypes { enum CommandsTypes {
InsertEntity(EntityObject), InsertEntity(EntityObject),
RemoveEntity(Entity), RemoveEntity(Entity),
UpdateEntity(Entity, ComponentChange), UpdateEntity(Entity, ComponentChange),
Event(TypeId, Box<dyn Any + Send + Sync>), Event(TypeId, Box<dyn Any + Send + Sync>),
InsertResource(TypeId, Box<dyn Resource>),
} }
pub struct Commands { pub struct Commands {
commands: Vec<CommandTypes>, commands: Vec<CommandsTypes>,
now: Duration, now: Duration,
} }
@ -37,24 +35,24 @@ impl Commands {
let entity = entity_object.as_entity(); let entity = entity_object.as_entity();
self.commands self.commands
.push(CommandTypes::InsertEntity(entity_object)); .push(CommandsTypes::InsertEntity(entity_object));
entity entity
} }
pub fn remove_entity(&mut self, entity: Entity) { pub fn remove_entity(&mut self, entity: Entity) {
self.commands.push(CommandTypes::RemoveEntity(entity)); self.commands.push(CommandsTypes::RemoveEntity(entity));
} }
pub fn insert_component<T: EntityComponent>(&mut self, entity: Entity, component: T) { pub fn insert_component<T: EntityComponent>(&mut self, entity: Entity, component: T) {
self.commands.push(CommandTypes::UpdateEntity( self.commands.push(CommandsTypes::UpdateEntity(
entity, entity,
ComponentChange::Added(TypeId::of::<T>(), Box::new(component)), ComponentChange::Added(TypeId::of::<T>(), Box::new(component)),
)); ));
} }
pub fn remove_component<T: EntityComponent>(&mut self, entity: Entity) { pub fn remove_component<T: EntityComponent>(&mut self, entity: Entity) {
self.commands.push(CommandTypes::UpdateEntity( self.commands.push(CommandsTypes::UpdateEntity(
entity, entity,
ComponentChange::Removed(TypeId::of::<T>()), ComponentChange::Removed(TypeId::of::<T>()),
)); ));
@ -62,32 +60,20 @@ impl Commands {
pub fn write_event<T: Any + Send + Sync>(&mut self, payload: T) { pub fn write_event<T: Any + Send + Sync>(&mut self, payload: T) {
self.commands self.commands
.push(CommandTypes::Event(TypeId::of::<T>(), Box::new(payload))); .push(CommandsTypes::Event(TypeId::of::<T>(), Box::new(payload)));
}
pub fn insert_resource<T: Resource>(&mut self, resource: T) {
self.commands.push(CommandTypes::InsertResource(
TypeId::of::<T>(),
Box::new(resource),
));
} }
pub(crate) fn apply_deferred(self, world: &mut World) -> Result<()> { pub(crate) fn apply_deferred(self, world: &mut World) -> Result<()> {
for command in self.commands { for command in self.commands {
match command { match command {
CommandTypes::InsertEntity(entity_object) => { CommandsTypes::InsertEntity(entity_object) => {
world.add_entity(entity_object)?; world.add_entity(entity_object)?;
} }
CommandTypes::RemoveEntity(entity) => world.remove_entity(entity), CommandsTypes::RemoveEntity(entity) => world.remove_entity(entity),
CommandTypes::UpdateEntity(entity, component_change) => { CommandsTypes::UpdateEntity(entity, component_change) => {
world.component_change(entity, component_change) world.component_change(entity, component_change)
} }
CommandTypes::Event(type_id, payload) => { CommandsTypes::Event(type_id, any) => world.events.write_payload(type_id, any),
world.events.write_payload(type_id, payload)
}
CommandTypes::InsertResource(type_id, resource) => {
world.resources.insert_resource(type_id, resource)
}
} }
} }

View file

@ -6,7 +6,6 @@ pub mod get_disjoint_mut;
pub mod resources; pub mod resources;
mod type_map; mod type_map;
mod unsafe_component_store; mod unsafe_component_store;
mod update_testing;
mod updates; mod updates;
mod world; mod world;

View file

@ -38,10 +38,6 @@ impl Resources {
.flatten() .flatten()
} }
pub(crate) fn insert_resource(&mut self, type_id: TypeId, resource: Box<dyn Resource>) {
self.map.insert(type_id, resource);
}
pub fn insert_if_not_exists<T: Resource + Default>(&mut self) { pub fn insert_if_not_exists<T: Resource + Default>(&mut self) {
if !self.contains::<T>() { if !self.contains::<T>() {
self.insert(T::default()); self.insert(T::default());

View file

@ -1,154 +0,0 @@
#![allow(unused)]
use anyhow::Result;
use std::marker::PhantomData;
use std::{ops::Deref, ops::DerefMut};
use crate::*;
#[derive(Copy, Clone)]
pub struct Query<C, F = EmptyFilter>
where
F: StaticCheckFilter,
{
entity: Entity,
c: C,
filter: PhantomData<F>,
}
impl<C, F: StaticCheckFilter> Query<C, F> {
pub fn entity(&self) -> Entity {
self.entity
}
}
impl<C, F: StaticCheckFilter> Deref for Query<C, F> {
type Target = C;
fn deref(&self) -> &Self::Target {
&self.c
}
}
impl<C, F: StaticCheckFilter> DerefMut for Query<C, F> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.c
}
}
trait CreateQueryArchetype<T, R, Func, Filter> {
fn create(f: Func) -> Self;
}
pub trait StaticCheckFilter: Send + Sync + Default + Clone {
fn check(entity: &EntityObject) -> bool;
fn verify_dedup<O: EntityComponent>();
}
impl StaticCheckFilter for EmptyFilter {
fn check(_entity: &EntityObject) -> bool {
true
}
fn verify_dedup<O>() {}
}
pub struct QueryArchetype {
check_entity: Box<dyn Fn(&EntityObject) -> bool + Send + Sync>,
create_callback: Box<
dyn Fn(
&EntityObject,
)
-> Result<Box<dyn Fn(&mut Commands, &mut World) -> Result<()> + Send + Sync>>
+ Send
+ Sync,
>,
entities: Vec<Box<dyn Fn(&mut Commands, &mut World) -> Result<()> + Send + Sync>>,
}
macro_rules! impl_query_singleton_update {
( $($var:ident $(,)?)+ ) => {
impl<Func, Filter, $( $var, )+> CreateQueryArchetype<( $( $var, )+ ), (), Func, Filter> for QueryArchetype
where
Func: Fn(&mut Commands, Query<( $( &mut $var, )+ ), Filter>) -> Result<()> + Send + Sync + Clone + 'static,
Filter: StaticCheckFilter,
$(
$var: EntityComponent + ComponentDebug,
)+
{
paste::item! {
fn create(f: Func) -> Self {
$(
Filter::verify_dedup::<$var>();
)+
Self {
check_entity: Box::new({
move |entity| {
$(
if !entity.components.contains::<$var>() {
return false;
}
)+
if !Filter::check(entity) {
return false;
}
true
}
}),
create_callback: Box::new(move |entity| {
$(
let [< $var:lower >] = UnsafeComponentStore::from(
entity.get_component::<$var>()?
);
)+
let e = entity.as_entity();
let f = f.clone();
Ok(Box::new(move |commands, _world| {
let q = Query {
entity: e,
c: unsafe { ($([< $var:lower >].as_mut(),)+) },
filter: PhantomData,
};
f(commands, q)
}))
}),
entities: Vec::new(),
}
}
}
}
};
}
impl_query_singleton_update!(R);
struct T {}
impl EntityComponent for T {
fn name(&self) -> &str {
Self::debug_name()
}
}
impl ComponentDebug for T {
fn debug_name() -> &'static str {
"T"
}
}
fn system(_commands: &mut Commands, mut _query: Query<&mut T>) -> Result<()> {
Ok(())
}
fn test() {
// let q = QueryArchetype::create(system);
}

View file

@ -3,6 +3,7 @@
use std::any::TypeId; use std::any::TypeId;
use std::collections::HashMap; use std::collections::HashMap;
use std::marker::PhantomData;
#[cfg(feature = "timings")] #[cfg(feature = "timings")]
use std::time::Instant; use std::time::Instant;
@ -417,6 +418,15 @@ macro_rules! impl_update_filter {
}; };
} }
pub struct Query<T, F = EmptyFilter>
where
F: CheckFilter,
{
pub components: T,
d: PhantomData<F>,
}
pub trait AddUpdates<T, R, Func, Filter> { pub trait AddUpdates<T, R, Func, Filter> {
fn add_update(&mut self, name: &str, priority: u32, func: Func, filter: Filter) -> Result<()>; fn add_update(&mut self, name: &str, priority: u32, func: Func, filter: Filter) -> Result<()>;
} }