Compare commits
13 commits
3e2b549717
...
d33dfca274
Author | SHA1 | Date | |
---|---|---|---|
d33dfca274 | |||
ca4a040d20 | |||
29141e5b9a | |||
bbee8a0041 | |||
d730eb586b | |||
7ee6ef0a08 | |||
4370853e23 | |||
fe1decdbbd | |||
c728605684 | |||
7a20347988 | |||
3aa5438fed | |||
540be64f6a | |||
a073f8e520 |
8 changed files with 665 additions and 647 deletions
|
@ -12,5 +12,6 @@ ron = "0.10.0"
|
|||
syn = { version = "2.0.67", features = ["extra-traits", "full"] }
|
||||
quote = "1.0.35"
|
||||
proc-macro2 = "1.0.86"
|
||||
itertools = "0.14.0"
|
||||
|
||||
utilities = { git = "https://gavania.de/hodasemi/utilities.git" }
|
||||
|
|
|
@ -11,8 +11,6 @@ serde.workspace = true
|
|||
paste.workspace = true
|
||||
ron.workspace = true
|
||||
utilities.workspace = true
|
||||
itertools.workspace = true
|
||||
|
||||
update_macros = { path = "../update_macros" }
|
||||
|
||||
[features]
|
||||
timings = []
|
||||
|
|
|
@ -4,18 +4,20 @@ use std::{
|
|||
time::Duration,
|
||||
};
|
||||
|
||||
use crate::{world::ComponentChange, *};
|
||||
use crate::{resources::Resource, world::ComponentChange, *};
|
||||
|
||||
enum CommandsTypes {
|
||||
enum CommandTypes {
|
||||
InsertEntity(EntityObject),
|
||||
RemoveEntity(Entity),
|
||||
UpdateEntity(Entity, ComponentChange),
|
||||
|
||||
Event(TypeId, Box<dyn Any + Send + Sync>),
|
||||
|
||||
InsertResource(TypeId, Box<dyn Resource>),
|
||||
}
|
||||
|
||||
pub struct Commands {
|
||||
commands: Vec<CommandsTypes>,
|
||||
commands: Vec<CommandTypes>,
|
||||
now: Duration,
|
||||
}
|
||||
|
||||
|
@ -35,24 +37,24 @@ impl Commands {
|
|||
let entity = entity_object.as_entity();
|
||||
|
||||
self.commands
|
||||
.push(CommandsTypes::InsertEntity(entity_object));
|
||||
.push(CommandTypes::InsertEntity(entity_object));
|
||||
|
||||
entity
|
||||
}
|
||||
|
||||
pub fn remove_entity(&mut self, entity: Entity) {
|
||||
self.commands.push(CommandsTypes::RemoveEntity(entity));
|
||||
self.commands.push(CommandTypes::RemoveEntity(entity));
|
||||
}
|
||||
|
||||
pub fn insert_component<T: EntityComponent>(&mut self, entity: Entity, component: T) {
|
||||
self.commands.push(CommandsTypes::UpdateEntity(
|
||||
self.commands.push(CommandTypes::UpdateEntity(
|
||||
entity,
|
||||
ComponentChange::Added(TypeId::of::<T>(), Box::new(component)),
|
||||
));
|
||||
}
|
||||
|
||||
pub fn remove_component<T: EntityComponent>(&mut self, entity: Entity) {
|
||||
self.commands.push(CommandsTypes::UpdateEntity(
|
||||
self.commands.push(CommandTypes::UpdateEntity(
|
||||
entity,
|
||||
ComponentChange::Removed(TypeId::of::<T>()),
|
||||
));
|
||||
|
@ -60,20 +62,32 @@ impl Commands {
|
|||
|
||||
pub fn write_event<T: Any + Send + Sync>(&mut self, payload: T) {
|
||||
self.commands
|
||||
.push(CommandsTypes::Event(TypeId::of::<T>(), Box::new(payload)));
|
||||
.push(CommandTypes::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<()> {
|
||||
for command in self.commands {
|
||||
match command {
|
||||
CommandsTypes::InsertEntity(entity_object) => {
|
||||
CommandTypes::InsertEntity(entity_object) => {
|
||||
world.add_entity(entity_object)?;
|
||||
}
|
||||
CommandsTypes::RemoveEntity(entity) => world.remove_entity(entity),
|
||||
CommandsTypes::UpdateEntity(entity, component_change) => {
|
||||
CommandTypes::RemoveEntity(entity) => world.remove_entity(entity),
|
||||
CommandTypes::UpdateEntity(entity, component_change) => {
|
||||
world.component_change(entity, component_change)
|
||||
}
|
||||
CommandsTypes::Event(type_id, any) => world.events.write_payload(type_id, any),
|
||||
CommandTypes::Event(type_id, payload) => {
|
||||
world.events.write_payload(type_id, payload)
|
||||
}
|
||||
CommandTypes::InsertResource(type_id, resource) => {
|
||||
world.resources.insert_resource(type_id, resource)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -38,6 +38,10 @@ impl Resources {
|
|||
.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) {
|
||||
if !self.contains::<T>() {
|
||||
self.insert(T::default());
|
||||
|
|
|
@ -1,370 +1,15 @@
|
|||
#![allow(clippy::type_complexity)]
|
||||
|
||||
use std::any::TypeId;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::marker::PhantomData;
|
||||
#[cfg(feature = "timings")]
|
||||
use std::time::Instant;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::{any::TypeId, marker::PhantomData};
|
||||
|
||||
use anyhow::Result;
|
||||
use indexmap::IndexMap;
|
||||
|
||||
#[cfg(feature = "timings")]
|
||||
use super::super::timings::Timings;
|
||||
use itertools::Itertools;
|
||||
use update_macros::implement_updates;
|
||||
|
||||
use crate::resources::Resource as ResourceTrait;
|
||||
use crate::*;
|
||||
use update_macros::{implement_pair_update, implement_single_update};
|
||||
|
||||
macro_rules! impl_singleton_update {
|
||||
// without resources
|
||||
( $($var:ident $(,)?)+ ) => {
|
||||
impl<Func, Filter, $( $var, )+> CreateArchetype<( $( $var, )+ ), (), Func, Filter> for Archetype
|
||||
where
|
||||
Func: Fn(&mut Commands, Entity, $(&mut $var,)+) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
Filter: CheckFilter + 'static,
|
||||
$(
|
||||
$var: EntityComponent + ComponentDebug,
|
||||
)+
|
||||
{
|
||||
paste::item! {
|
||||
fn create(f: Func, filter: Filter) -> 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 f = f.clone();
|
||||
|
||||
Ok(Box::new(move |e, commands, _world| {
|
||||
unsafe { f(commands, e, $([< $var:lower >].as_mut(),)+) }
|
||||
}))
|
||||
}),
|
||||
|
||||
entities: IndexMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Func, Filter, $( $var, )+> AddUpdates<( $( $var, )+ ), (), Func, Filter> for Updates
|
||||
where
|
||||
$(
|
||||
$var: EntityComponent + ComponentDebug,
|
||||
)+
|
||||
Func: Fn(& mut Commands, Entity, $(&mut $var,)+) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
Filter: CheckFilter + 'static
|
||||
{
|
||||
fn add_update(
|
||||
&mut self,
|
||||
name: &str,
|
||||
priority: u32,
|
||||
func: Func,
|
||||
filter: Filter
|
||||
) -> Result<()> {
|
||||
paste::item! {
|
||||
self.add(name, priority, Update::Single(Archetype::create(func, filter)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Func, Filter, $( $var, )+> AddUpdates<( $( $var, )+ ), (), Func, Filter> for WorldBuilder
|
||||
where
|
||||
$(
|
||||
$var: EntityComponent + ComponentDebug,
|
||||
)+
|
||||
Func: Fn(& mut Commands, Entity, $(&mut $var,)+) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
Filter: CheckFilter + 'static
|
||||
{
|
||||
fn add_update(
|
||||
&mut self,
|
||||
name: &str,
|
||||
priority: u32,
|
||||
func: Func,
|
||||
filter: Filter,
|
||||
) -> Result<()> {
|
||||
self.updates.add_update(name, priority, func, filter)
|
||||
}
|
||||
}
|
||||
};
|
||||
// with resources
|
||||
( $($var:ident $(,)?)+ [$($res:ident $(,)?)+] ) => {
|
||||
impl<Func, Filter, $( $var, )+ $( $res, )+> CreateArchetype<( $( $var, )+ ), ( $( $res, )+ ), Func, Filter> for Archetype
|
||||
where
|
||||
Func: Fn(&mut Commands, Entity, $(&mut $var,)+ $(&mut $res,)+) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
Filter: CheckFilter + 'static,
|
||||
$(
|
||||
$var: EntityComponent + ComponentDebug,
|
||||
)+
|
||||
$(
|
||||
$res: ResourceTrait,
|
||||
)+
|
||||
{
|
||||
paste::item! {
|
||||
fn create(f: Func, filter: Filter) -> 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 f = f.clone();
|
||||
|
||||
Ok(Box::new(move |e, commands, world| {
|
||||
|
||||
let (
|
||||
$(
|
||||
[< $res:lower _var >],
|
||||
)+
|
||||
): (
|
||||
$(
|
||||
&mut $res,
|
||||
)+
|
||||
) = world.resources.get_mut()?;
|
||||
|
||||
unsafe { f(commands, e, $([< $var:lower >].as_mut(),)+ $([< $res:lower _var >],)+) }
|
||||
}))
|
||||
}),
|
||||
|
||||
entities: IndexMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Func, Filter, $( $var, )+ $( $res, )+> AddUpdates<( $( $var, )+ ), ( $( $res, )+ ), Func, Filter> for Updates
|
||||
where
|
||||
$(
|
||||
$var: EntityComponent + ComponentDebug,
|
||||
)+
|
||||
$(
|
||||
$res: ResourceTrait,
|
||||
)+
|
||||
Func: Fn(& mut Commands, Entity, $(&mut $var,)+ $(&mut $res,)+) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
Filter: CheckFilter + 'static
|
||||
{
|
||||
fn add_update(
|
||||
&mut self,
|
||||
name: &str,
|
||||
priority: u32,
|
||||
func: Func,
|
||||
filter: Filter
|
||||
) -> Result<()> {
|
||||
paste::item! {
|
||||
self.add(name, priority, Update::Single(Archetype::create(func, filter)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Func, Filter, $( $var, )+ $( $res, )+> AddUpdates<( $( $var, )+ ), ( $( $res, )+ ), Func, Filter> for WorldBuilder
|
||||
where
|
||||
$(
|
||||
$var: EntityComponent + ComponentDebug,
|
||||
)+
|
||||
$(
|
||||
$res: ResourceTrait,
|
||||
)+
|
||||
Func: Fn(& mut Commands, Entity, $(&mut $var,)+ $(&mut $res,)+) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
Filter: CheckFilter + 'static
|
||||
{
|
||||
fn add_update(
|
||||
&mut self,
|
||||
name: &str,
|
||||
priority: u32,
|
||||
func: Func,
|
||||
filter: Filter,
|
||||
) -> Result<()> {
|
||||
self.updates.add_update(name, priority, func, filter)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_pair_update {
|
||||
(
|
||||
$lhs_id: expr,
|
||||
( $([$lhs_little: ident: $lhs_big: ident]$(,)?)+ ),
|
||||
$rhs_id: expr,
|
||||
( $([$rhs_little: ident: $rhs_big: ident]$(,)?)+ )
|
||||
) => {
|
||||
impl ArchetypePair {
|
||||
paste::item! {
|
||||
pub fn [<create_lhs_ $lhs_id _rhs_ $rhs_id>] <F, LeftFilter, RightFilter, $($lhs_big,)+ $($rhs_big,)+>
|
||||
(f: F, left_filter: LeftFilter, right_filter: RightFilter) -> Self
|
||||
where
|
||||
F: Fn(&mut Commands, (Entity, $(&mut $lhs_big,)+), (Entity, $(&mut $rhs_big,)+)) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
LeftFilter: CheckFilter + 'static,
|
||||
RightFilter: CheckFilter + 'static,
|
||||
$(
|
||||
$rhs_big: EntityComponent + ComponentDebug,
|
||||
)+
|
||||
$(
|
||||
$lhs_big: EntityComponent + ComponentDebug,
|
||||
)+
|
||||
{
|
||||
$(
|
||||
left_filter.verify_dedup::<$lhs_big>();
|
||||
)+
|
||||
|
||||
$(
|
||||
right_filter.verify_dedup::<$rhs_big>();
|
||||
)+
|
||||
|
||||
Self {
|
||||
check_left_entity: Box::new({
|
||||
move |entity| {
|
||||
$(
|
||||
if !entity.components.contains::<$lhs_big>() {
|
||||
return false;
|
||||
}
|
||||
)+
|
||||
|
||||
if !left_filter.check(entity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}),
|
||||
check_right_entity: Box::new({
|
||||
move |entity| {
|
||||
$(
|
||||
if !entity.components.contains::<$rhs_big>() {
|
||||
return false;
|
||||
}
|
||||
)+
|
||||
|
||||
if !right_filter.check(entity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}),
|
||||
|
||||
create_callback: Box::new(move |lhs_entity, rhs_entity| {
|
||||
$(
|
||||
let $lhs_little = UnsafeComponentStore::from(
|
||||
lhs_entity.get_component::<$lhs_big>()?
|
||||
);
|
||||
)+
|
||||
|
||||
$(
|
||||
let $rhs_little = UnsafeComponentStore::from(
|
||||
rhs_entity.get_component::<$rhs_big>()?
|
||||
);
|
||||
)+
|
||||
|
||||
let f = f.clone();
|
||||
|
||||
Ok(Box::new(move |lhs_e, rhs_e, commands| {
|
||||
unsafe { f(commands, (lhs_e, $($lhs_little.as_mut(),)+), (rhs_e, $($rhs_little.as_mut(),)+) ) }
|
||||
}))
|
||||
}),
|
||||
|
||||
entities: IndexMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Func, LhsFilter, RhsFilter, $( $lhs_big, )+ $($rhs_big,)+> AddUpdates<( ($( $lhs_big, )+), ($($rhs_big,)+) ), (), Func, (LhsFilter, RhsFilter)> for Updates
|
||||
where
|
||||
$(
|
||||
$rhs_big: EntityComponent + ComponentDebug,
|
||||
)+
|
||||
$(
|
||||
$lhs_big: EntityComponent + ComponentDebug,
|
||||
)+
|
||||
Func: Fn(&mut Commands, (Entity, $(&mut $lhs_big,)+), (Entity, $(&mut $rhs_big,)+)) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
LhsFilter: CheckFilter + 'static,
|
||||
RhsFilter: CheckFilter + 'static
|
||||
{
|
||||
fn add_update(
|
||||
&mut self,
|
||||
name: &str,
|
||||
priority: u32,
|
||||
func: Func,
|
||||
filter: (LhsFilter, RhsFilter)
|
||||
) -> Result<()> {
|
||||
paste::item! {
|
||||
self.add(name, priority, Update::Pair(ArchetypePair::[<create_lhs_ $lhs_id _rhs_ $rhs_id>](func, filter.0, filter.1)))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<Func, LhsFilter, RhsFilter, $( $lhs_big, )+ $($rhs_big,)+> AddUpdates<( ($( $lhs_big, )+), ($($rhs_big,)+) ), (), Func, (LhsFilter, RhsFilter)> for WorldBuilder
|
||||
where
|
||||
$(
|
||||
$rhs_big: EntityComponent + ComponentDebug,
|
||||
)+
|
||||
$(
|
||||
$lhs_big: EntityComponent + ComponentDebug,
|
||||
)+
|
||||
Func: Fn(& mut Commands, (Entity, $(&mut $lhs_big,)+), (Entity, $(&mut $rhs_big,)+)) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
LhsFilter: CheckFilter + 'static,
|
||||
RhsFilter: CheckFilter + 'static
|
||||
{
|
||||
fn add_update(
|
||||
&mut self,
|
||||
name: &str,
|
||||
priority: u32,
|
||||
func: Func,
|
||||
filter: (LhsFilter, RhsFilter),
|
||||
) -> Result<()> {
|
||||
self.updates.add_update(name, priority, func, filter)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_update_filter {
|
||||
( $name: ident, $([$big: ident, $little: ident]$(,)?)+ ) => {
|
||||
|
@ -386,7 +31,7 @@ macro_rules! impl_update_filter {
|
|||
}
|
||||
|
||||
impl<$($big: EntityComponent,)+> CheckFilter for [<$name Filter>]<$($big,)+> {
|
||||
fn check(&self, entity: &EntityObject) -> bool {
|
||||
fn check(entity: &EntityObject) -> bool {
|
||||
$(
|
||||
if entity.contains_component::<$big>() {
|
||||
return false;
|
||||
|
@ -396,7 +41,7 @@ macro_rules! impl_update_filter {
|
|||
true
|
||||
}
|
||||
|
||||
fn verify_dedup<O: EntityComponent>(&self) {
|
||||
fn verify_dedup<O: EntityComponent>() {
|
||||
$(
|
||||
if TypeId::of::<O>() == TypeId::of::<$big>() {
|
||||
panic!("Type is used as input and filter at the same time");
|
||||
|
@ -418,37 +63,58 @@ macro_rules! impl_update_filter {
|
|||
};
|
||||
}
|
||||
|
||||
pub struct Query<T, F = EmptyFilter>
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Query<C, F = EmptyFilter>
|
||||
where
|
||||
F: CheckFilter,
|
||||
{
|
||||
pub components: T,
|
||||
entity: Entity,
|
||||
c: C,
|
||||
filter: PhantomData<F>,
|
||||
}
|
||||
|
||||
d: PhantomData<F>,
|
||||
impl<C, F: CheckFilter> Query<C, F> {
|
||||
pub fn entity(&self) -> Entity {
|
||||
self.entity
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, F: CheckFilter> Deref for Query<C, F> {
|
||||
type Target = C;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.c
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, F: CheckFilter> DerefMut for Query<C, F> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.c
|
||||
}
|
||||
}
|
||||
|
||||
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, priority: u32, func: Func) -> Result<()>;
|
||||
}
|
||||
|
||||
trait CreateArchetype<T, R, Func, Filter> {
|
||||
fn create(f: Func, filter: Filter) -> Self;
|
||||
pub trait CreateArchetype<T, R, Func, Filter> {
|
||||
fn create(f: Func) -> Self;
|
||||
}
|
||||
|
||||
pub trait CheckFilter: Send + Sync + Default + Clone {
|
||||
fn check(&self, entity: &EntityObject) -> bool;
|
||||
fn verify_dedup<O: EntityComponent>(&self);
|
||||
fn check(entity: &EntityObject) -> bool;
|
||||
fn verify_dedup<O: EntityComponent>();
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct EmptyFilter;
|
||||
|
||||
impl CheckFilter for EmptyFilter {
|
||||
fn check(&self, _entity: &EntityObject) -> bool {
|
||||
fn check(_entity: &EntityObject) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn verify_dedup<O>(&self) {}
|
||||
fn verify_dedup<O>() {}
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
|
@ -471,42 +137,76 @@ impl ArchetypeInfo {
|
|||
}
|
||||
|
||||
pub struct Archetype {
|
||||
check_entity: Box<dyn Fn(&EntityObject) -> bool + Send + Sync>,
|
||||
queries: usize,
|
||||
check_entity: Box<dyn Fn(&EntityObject) -> Option<Vec<usize>> + Send + Sync>,
|
||||
create_callback: Box<
|
||||
dyn Fn(
|
||||
&EntityObject,
|
||||
&[&EntityObject],
|
||||
)
|
||||
-> Result<Box<dyn Fn(Entity, &mut Commands, &mut World) -> Result<()> + Send + Sync>>
|
||||
-> Result<Box<dyn Fn(&mut Commands, &mut World) -> Result<()> + Send + Sync>>
|
||||
+ Send
|
||||
+ Sync,
|
||||
>,
|
||||
|
||||
entities: IndexMap<
|
||||
Entity,
|
||||
Box<dyn Fn(Entity, &mut Commands, &mut World) -> Result<()> + Send + Sync>,
|
||||
>,
|
||||
entities:
|
||||
IndexMap<Vec<Entity>, Box<dyn Fn(&mut Commands, &mut World) -> Result<()> + Send + Sync>>,
|
||||
}
|
||||
|
||||
impl Archetype {
|
||||
pub fn add_entity(&mut self, entity_object: &EntityObject) -> Result<()> {
|
||||
if (self.check_entity)(entity_object) {
|
||||
let cb = (self.create_callback)(entity_object)?;
|
||||
pub fn add_entities(
|
||||
&mut self,
|
||||
new_entity: &EntityObject,
|
||||
entity_objects: &IndexMap<Entity, EntityObject>,
|
||||
) -> Result<()> {
|
||||
let mut complying_entities: Vec<Vec<&EntityObject>> = vec![Vec::new(); self.queries];
|
||||
|
||||
self.entities.insert(entity_object.as_entity(), cb);
|
||||
match (self.check_entity)(new_entity) {
|
||||
Some(complying_queries) => complying_queries
|
||||
.into_iter()
|
||||
.for_each(|q| complying_entities[q].push(new_entity)),
|
||||
None => return Ok(()),
|
||||
}
|
||||
|
||||
for old_entity in entity_objects.values() {
|
||||
if let Some(complying_queries) = (self.check_entity)(old_entity) {
|
||||
complying_queries
|
||||
.into_iter()
|
||||
.for_each(|q| complying_entities[q].push(old_entity));
|
||||
}
|
||||
}
|
||||
|
||||
// check if one of the queries could not be fullfilled
|
||||
for queryable_entities in complying_entities.iter() {
|
||||
if queryable_entities.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
for combinations in complying_entities.into_iter().multi_cartesian_product() {
|
||||
let cb = (self.create_callback)(&combinations)?;
|
||||
|
||||
self.entities
|
||||
.insert(combinations.iter().map(|e| e.as_entity()).collect(), cb);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_entity(&mut self, entity: Entity) {
|
||||
self.entities.swap_remove(&entity);
|
||||
while let Some(index) = self
|
||||
.entities
|
||||
.keys()
|
||||
.position(|entities| entities.contains(&entity))
|
||||
{
|
||||
self.entities.swap_remove_index(index);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute(&self, world: &mut World) -> Result<()> {
|
||||
let mut commands = Commands::new(world.now());
|
||||
|
||||
for (entity, callback) in self.entities.iter() {
|
||||
callback(*entity, &mut commands, world)?;
|
||||
for callback in self.entities.values() {
|
||||
callback(&mut commands, world)?;
|
||||
}
|
||||
|
||||
commands.apply_deferred(world)?;
|
||||
|
@ -516,120 +216,23 @@ impl Archetype {
|
|||
|
||||
pub fn entities(
|
||||
&self,
|
||||
) -> &IndexMap<Entity, Box<dyn Fn(Entity, &mut Commands, &mut World) -> Result<()> + Send + Sync>>
|
||||
{
|
||||
&self.entities
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ArchetypePair {
|
||||
check_left_entity: Box<dyn Fn(&EntityObject) -> bool + Send + Sync>,
|
||||
check_right_entity: Box<dyn Fn(&EntityObject) -> bool + Send + Sync>,
|
||||
|
||||
create_callback: Box<
|
||||
dyn Fn(
|
||||
&EntityObject,
|
||||
&EntityObject,
|
||||
)
|
||||
-> Result<Box<dyn Fn(Entity, Entity, &mut Commands) -> Result<()> + Send + Sync>>
|
||||
+ Send
|
||||
+ Sync,
|
||||
>,
|
||||
|
||||
entities: IndexMap<
|
||||
(Entity, Entity),
|
||||
Box<dyn Fn(Entity, Entity, &mut Commands) -> Result<()> + Send + Sync>,
|
||||
>,
|
||||
}
|
||||
|
||||
impl ArchetypePair {
|
||||
pub(crate) fn add_entity(
|
||||
&mut self,
|
||||
entity_object: &EntityObject,
|
||||
entities: &IndexMap<Entity, EntityObject>,
|
||||
) -> Result<()> {
|
||||
for (other_entity, other_entity_object) in entities.iter() {
|
||||
if entity_object.as_entity() == *other_entity {
|
||||
continue;
|
||||
}
|
||||
|
||||
// check if the entities can be on both sides
|
||||
if (self.check_left_entity)(entity_object)
|
||||
&& (self.check_right_entity)(other_entity_object)
|
||||
{
|
||||
let cb = (self.create_callback)(entity_object, other_entity_object)?;
|
||||
|
||||
self.entities
|
||||
.insert((entity_object.as_entity(), *other_entity), cb);
|
||||
}
|
||||
|
||||
if (self.check_left_entity)(other_entity_object)
|
||||
&& (self.check_right_entity)(entity_object)
|
||||
{
|
||||
let cb = (self.create_callback)(other_entity_object, entity_object)?;
|
||||
|
||||
self.entities
|
||||
.insert((*other_entity, entity_object.as_entity()), cb);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn remove_entity(&mut self, entity: Entity) {
|
||||
while let Some((left_entity, right_entity)) = self
|
||||
.entities
|
||||
.keys()
|
||||
.find(|(left_entity, right_entity)| *left_entity == entity || *right_entity == entity)
|
||||
.cloned()
|
||||
{
|
||||
self.entities.swap_remove(&(left_entity, right_entity));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn execute(&self, world: &mut World) -> Result<()> {
|
||||
let mut commands = Commands::new(world.now());
|
||||
|
||||
for ((left_entity, right_entity), callback) in self.entities.iter() {
|
||||
callback(*left_entity, *right_entity, &mut commands)?;
|
||||
}
|
||||
|
||||
commands.apply_deferred(world)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Update {
|
||||
Single(Archetype),
|
||||
Pair(ArchetypePair),
|
||||
}
|
||||
|
||||
impl From<Archetype> for Update {
|
||||
fn from(archetype: Archetype) -> Self {
|
||||
Self::Single(archetype)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ArchetypePair> for Update {
|
||||
fn from(pair: ArchetypePair) -> Self {
|
||||
Self::Pair(pair)
|
||||
) -> impl Iterator<
|
||||
Item = (
|
||||
&Vec<Entity>,
|
||||
&Box<dyn Fn(&mut Commands, &mut World) -> Result<()> + Send + Sync>,
|
||||
),
|
||||
> {
|
||||
self.entities.iter()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Updates {
|
||||
#[cfg(feature = "timings")]
|
||||
timings: Timings,
|
||||
|
||||
updates: Vec<(String, u32, Update)>,
|
||||
updates: Vec<(u32, Archetype)>,
|
||||
}
|
||||
|
||||
impl Default for Updates {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
#[cfg(feature = "timings")]
|
||||
timings: Timings::default,
|
||||
|
||||
updates: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
@ -637,38 +240,9 @@ impl Default for Updates {
|
|||
|
||||
impl Updates {
|
||||
pub(crate) fn update(&mut self, world: &mut World) -> Result<()> {
|
||||
#[cfg(feature = "timings")]
|
||||
if let Some(timings) = self.timings.check_timing(world.now(), None) {
|
||||
if !timings.is_empty() {
|
||||
println!("timings: {:#?}", timings);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "timings")]
|
||||
let timings = &mut self.timings;
|
||||
|
||||
self.updates
|
||||
.iter()
|
||||
.try_for_each(|(_name, _, update)| -> Result<()> {
|
||||
#[cfg(feature = "timings")]
|
||||
let before = Instant::now();
|
||||
|
||||
match update {
|
||||
Update::Single(archetype) => {
|
||||
archetype.execute(world)?;
|
||||
}
|
||||
Update::Pair(archetype_pair) => {
|
||||
archetype_pair.execute(world)?;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "timings")]
|
||||
timings.add(_name, Instant::now().duration_since(before));
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
.try_for_each(|(_, archetype)| archetype.execute(world))
|
||||
}
|
||||
|
||||
pub(crate) fn add_entity(
|
||||
|
@ -676,88 +250,27 @@ impl Updates {
|
|||
entity_object: &EntityObject,
|
||||
entities: &IndexMap<Entity, EntityObject>,
|
||||
) -> Result<()> {
|
||||
for (_, _, update) in self.updates.iter_mut() {
|
||||
match update {
|
||||
Update::Single(archetype) => {
|
||||
archetype.add_entity(entity_object)?;
|
||||
}
|
||||
Update::Pair(archetype_pair) => {
|
||||
archetype_pair.add_entity(entity_object, entities)?;
|
||||
}
|
||||
}
|
||||
for (_, archetype) in self.updates.iter_mut() {
|
||||
archetype.add_entities(entity_object, entities)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn remove_entity(&mut self, entity: Entity) {
|
||||
for (_, _, update) in self.updates.iter_mut() {
|
||||
match update {
|
||||
Update::Single(archetype) => {
|
||||
archetype.remove_entity(entity);
|
||||
}
|
||||
Update::Pair(archetype_pair) => {
|
||||
archetype_pair.remove_entity(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pub(crate) fn clear(&mut self) {
|
||||
// self.updates.clear();
|
||||
|
||||
// #[cfg(feature = "timings")]
|
||||
// self.timings.clear();
|
||||
// }
|
||||
|
||||
pub(crate) fn add(&mut self, name: &str, priority: u32, update: Update) -> Result<()> {
|
||||
#[cfg(feature = "timings")]
|
||||
self.timings.add_timing_afterwards(name);
|
||||
|
||||
self.updates.push((name.to_string(), priority, update));
|
||||
self.updates
|
||||
.sort_by(|(_, lhs_prio, _), (_, rhs_prio, _)| lhs_prio.cmp(rhs_prio));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Archetypes {
|
||||
archetypes: HashMap<String, Archetype>,
|
||||
}
|
||||
|
||||
impl Archetypes {
|
||||
pub(crate) fn add_entity(&mut self, entity_object: &EntityObject) -> Result<()> {
|
||||
for archetype in self.archetypes.values_mut() {
|
||||
archetype.add_entity(entity_object)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn remove_entity(&mut self, entity: Entity) {
|
||||
for archetype in self.archetypes.values_mut() {
|
||||
for (_, archetype) in self.updates.iter_mut() {
|
||||
archetype.remove_entity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn insert(&mut self, name: String, archetype: Archetype) {
|
||||
assert!(self.archetypes.insert(name, archetype).is_none());
|
||||
}
|
||||
pub(crate) fn add(&mut self, priority: u32, archetype: Archetype) -> Result<()> {
|
||||
self.updates.push((priority, archetype));
|
||||
self.updates.sort_by_key(|(prio, _)| *prio);
|
||||
|
||||
pub(crate) fn get(&self, name: &String) -> Option<&Archetype> {
|
||||
self.archetypes.get(name)
|
||||
}
|
||||
|
||||
pub(crate) fn execute(&self, name: &String, world: &mut World) -> Result<()> {
|
||||
self.archetypes[name].execute(world)
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
implement_pair_update!(impl_pair_update, 1, 10);
|
||||
implement_single_update!(impl_singleton_update, 1, 10);
|
||||
|
||||
#[rustfmt::skip]
|
||||
impl_update_filter!(Monuple, [R, r]);
|
||||
#[rustfmt::skip]
|
||||
|
@ -778,3 +291,5 @@ impl_update_filter!(Octuple, [R, r], [S, s], [T, t], [U, u], [V, v], [W, w],
|
|||
impl_update_filter!(Nonuple, [R, r], [S, s], [T, t], [U, u], [V, v], [W, w], [X, x], [Y, y], [Z, z]);
|
||||
#[rustfmt::skip]
|
||||
impl_update_filter!(Decuple, [R, r], [S, s], [T, t], [U, u], [V, v], [W, w], [X, x], [Y, y], [Z, z], [A, a]);
|
||||
|
||||
implement_updates!(5, 5);
|
||||
|
|
|
@ -4,7 +4,7 @@ use std::{
|
|||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use anyhow::{Error, Result, bail};
|
||||
use anyhow::{Result, bail};
|
||||
use indexmap::IndexMap;
|
||||
use utilities::prelude::{remove_life_time, remove_life_time_mut};
|
||||
|
||||
|
@ -14,7 +14,6 @@ pub struct WorldBuilder {
|
|||
pub(crate) updates: Updates,
|
||||
pub events: Events,
|
||||
pub resources: Resources,
|
||||
archetypes: Archetypes,
|
||||
systems: Vec<(
|
||||
u32,
|
||||
Box<dyn Fn(&mut World) -> Result<bool> + Send + Sync + 'static>,
|
||||
|
@ -29,10 +28,6 @@ impl WorldBuilder {
|
|||
self.systems.push((priority, Box::new(f)));
|
||||
self.systems.sort_by_key(|(priority, _)| *priority);
|
||||
}
|
||||
|
||||
pub fn add_archetype(&mut self, name: impl ToString, archetype: Archetype) {
|
||||
self.archetypes.insert(name.to_string(), archetype);
|
||||
}
|
||||
}
|
||||
|
||||
impl WorldBuilder {
|
||||
|
@ -42,7 +37,6 @@ impl WorldBuilder {
|
|||
events: self.events,
|
||||
resources: self.resources,
|
||||
entities: Default::default(),
|
||||
archetypes: self.archetypes,
|
||||
|
||||
entities_to_remove: Default::default(),
|
||||
entities_to_add: Default::default(),
|
||||
|
@ -68,7 +62,6 @@ pub struct World {
|
|||
pub events: Events,
|
||||
pub resources: Resources,
|
||||
pub(crate) entities: IndexMap<Entity, EntityObject>,
|
||||
archetypes: Archetypes,
|
||||
|
||||
entities_to_remove: Vec<Entity>,
|
||||
entities_to_add: Vec<EntityObject>,
|
||||
|
@ -89,7 +82,6 @@ impl World {
|
|||
events: Default::default(),
|
||||
resources: Default::default(),
|
||||
systems: Default::default(),
|
||||
archetypes: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -263,33 +255,6 @@ impl World {
|
|||
pub fn remove_entity(&mut self, entity: Entity) {
|
||||
self.entities_to_remove.push(entity);
|
||||
}
|
||||
|
||||
pub fn execute_archetype(&mut self, name: impl ToString) -> Result<()> {
|
||||
let archetypes = unsafe { remove_life_time_mut(&mut self.archetypes) };
|
||||
archetypes.execute(&name.to_string(), self)
|
||||
}
|
||||
|
||||
pub fn archetype_info(&self, name: impl ToString) -> Result<ArchetypeInfo> {
|
||||
let name = name.to_string();
|
||||
let archetype = self
|
||||
.archetypes
|
||||
.get(&name)
|
||||
.ok_or_else(|| Error::msg(format!("requested archetype ({}) not found", name)))?;
|
||||
|
||||
let mut entities = Vec::new();
|
||||
|
||||
for entity in archetype.entities().keys() {
|
||||
#[cfg(debug_assertions)]
|
||||
let debug_name = self.entity(*entity)?.debug_name.clone();
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
let debug_name = None;
|
||||
|
||||
entities.push((*entity, debug_name));
|
||||
}
|
||||
|
||||
Ok(ArchetypeInfo::new(entities))
|
||||
}
|
||||
}
|
||||
|
||||
// async application of changes
|
||||
|
@ -305,7 +270,6 @@ impl World {
|
|||
let e = entity.as_entity();
|
||||
|
||||
self.updates.add_entity(&mut entity, &self.entities)?;
|
||||
self.archetypes.add_entity(&entity)?;
|
||||
assert!(self.entities.insert(e, entity).is_none());
|
||||
|
||||
Ok(e)
|
||||
|
@ -319,7 +283,6 @@ impl World {
|
|||
|
||||
entity_object.activation_state.apply_change();
|
||||
self.updates.remove_entity(entity);
|
||||
self.archetypes.remove_entity(entity);
|
||||
|
||||
return Ok(Some(entity_object));
|
||||
}
|
||||
|
@ -339,7 +302,6 @@ impl World {
|
|||
.ok_or_else(|| EntityNotFoundError::new(entity))?;
|
||||
|
||||
self.updates.remove_entity(entity);
|
||||
self.archetypes.remove_entity(entity);
|
||||
|
||||
entity_object.activation_state.apply_change();
|
||||
|
||||
|
@ -351,7 +313,6 @@ impl World {
|
|||
entity_object.activation_state.apply_change();
|
||||
|
||||
self.updates.add_entity(entity_object, &self.entities)?;
|
||||
self.archetypes.add_entity(entity_object)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -367,7 +328,6 @@ impl World {
|
|||
.ok_or_else(|| EntityNotFoundError::new(entity))?;
|
||||
|
||||
self.updates.remove_entity(entity);
|
||||
self.archetypes.remove_entity(entity);
|
||||
|
||||
entity_object.activation_state.apply_change();
|
||||
|
||||
|
@ -378,7 +338,6 @@ impl World {
|
|||
entity_object.activation_state.apply_change();
|
||||
|
||||
self.updates.add_entity(entity_object, &self.entities)?;
|
||||
self.archetypes.add_entity(entity_object)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -416,7 +375,6 @@ impl World {
|
|||
|
||||
for (entity, changes) in core::mem::take(&mut self.entities_updates) {
|
||||
self.updates.remove_entity(entity);
|
||||
self.archetypes.remove_entity(entity);
|
||||
|
||||
if let Ok(entity_object) = unsafe { self.entity_mut_unchecked(entity) } {
|
||||
entity_object.activation_state.apply_change();
|
||||
|
@ -447,7 +405,6 @@ impl World {
|
|||
entity_object.activation_state.apply_change();
|
||||
|
||||
self.updates.add_entity(entity_object, &self.entities)?;
|
||||
self.archetypes.add_entity(entity_object)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
mod pair_update;
|
||||
mod single_update;
|
||||
mod update;
|
||||
|
||||
use pair_update::pair_update;
|
||||
use single_update::single_update;
|
||||
use update::update;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
|
@ -13,6 +15,31 @@ use syn::{
|
|||
token::Comma,
|
||||
};
|
||||
|
||||
struct UpdateInfo {
|
||||
max_components: usize,
|
||||
max_resources: usize,
|
||||
}
|
||||
|
||||
impl Parse for UpdateInfo {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
let max_components = input.parse::<LitInt>()?.base10_parse()?;
|
||||
input.parse::<Comma>()?;
|
||||
let max_resources = input.parse::<LitInt>()?.base10_parse()?;
|
||||
|
||||
Ok(Self {
|
||||
max_components,
|
||||
max_resources,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn implement_updates(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as UpdateInfo);
|
||||
|
||||
update(input.max_components, input.max_resources)
|
||||
}
|
||||
|
||||
struct InputInfo {
|
||||
macro_ident: Ident,
|
||||
start: usize,
|
||||
|
|
502
update_macros/src/update.rs
Normal file
502
update_macros/src/update.rs
Normal file
|
@ -0,0 +1,502 @@
|
|||
use proc_macro::TokenStream;
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::{ToTokens, format_ident, quote};
|
||||
use syn::Ident;
|
||||
|
||||
struct Query {
|
||||
components: Vec<Ident>,
|
||||
filter: Ident,
|
||||
}
|
||||
|
||||
impl Query {
|
||||
pub fn new(components: impl Iterator<Item = Ident>, filter_id: usize) -> Self {
|
||||
Self {
|
||||
components: components.collect(),
|
||||
filter: format_ident!("Filter{filter_id}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn types(&self) -> TokenStream2 {
|
||||
let component_list = self
|
||||
.components
|
||||
.iter()
|
||||
.map(|c| quote! { #c })
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let components = if component_list.len() == 1 {
|
||||
let component = &component_list[0];
|
||||
quote! { #component }
|
||||
} else {
|
||||
quote! { ( #( #component_list, )* ) }
|
||||
};
|
||||
|
||||
components
|
||||
}
|
||||
|
||||
pub fn dedup_checks(&self) -> TokenStream2 {
|
||||
let components = &self.components;
|
||||
let filter = &self.filter;
|
||||
|
||||
quote! {
|
||||
#( #filter::verify_dedup::<#components>(); )*
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_entity(&self, index: usize) -> TokenStream2 {
|
||||
let components = &self.components;
|
||||
let filter = &self.filter;
|
||||
|
||||
quote! {
|
||||
{
|
||||
let mut complies = true;
|
||||
|
||||
#(
|
||||
if !entity.components.contains::<#components>() {
|
||||
complies = false;
|
||||
}
|
||||
)*
|
||||
|
||||
if !#filter::check(entity) {
|
||||
complies = false;
|
||||
}
|
||||
|
||||
if complies {
|
||||
comply_queries.push(#index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn component_store_and_query(&self, index: usize) -> (TokenStream2, TokenStream2, Ident) {
|
||||
let components = &self.components;
|
||||
let entity_name = format_ident!("entity_{index}");
|
||||
let component_names = self
|
||||
.components
|
||||
.iter()
|
||||
.map(|component| format_ident!("q_{index}_{component}"))
|
||||
.collect::<Vec<_>>();
|
||||
let query_ident = format_ident!("query_{index}");
|
||||
let entity_ident = format_ident!("e_{index}");
|
||||
|
||||
(
|
||||
quote! {
|
||||
let #entity_name = &entities[#index];
|
||||
let #entity_ident = #entity_name.as_entity();
|
||||
|
||||
#(
|
||||
#[allow(non_snake_case)]
|
||||
let #component_names = UnsafeComponentStore::from(
|
||||
#entity_name.get_component::<#components>()?
|
||||
);
|
||||
)*
|
||||
},
|
||||
if component_names.len() == 1 {
|
||||
let component_name = &component_names[0];
|
||||
|
||||
quote! {
|
||||
let #query_ident = Query {
|
||||
entity: #entity_ident,
|
||||
c: unsafe { #component_name.as_mut() },
|
||||
filter: PhantomData,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
let #query_ident = Query {
|
||||
entity: #entity_ident,
|
||||
c: unsafe { ( #( #component_names.as_mut(), )* ) },
|
||||
filter: PhantomData,
|
||||
};
|
||||
}
|
||||
},
|
||||
query_ident,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn components(&self) -> &Vec<Ident> {
|
||||
&self.components
|
||||
}
|
||||
|
||||
pub fn filter(&self) -> &Ident {
|
||||
&self.filter
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Query {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream2) {
|
||||
let component_list = self
|
||||
.components
|
||||
.iter()
|
||||
.map(|c| quote! { &mut #c })
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let components = if component_list.len() == 1 {
|
||||
let component = &component_list[0];
|
||||
quote! { #component }
|
||||
} else {
|
||||
quote! { ( #( #component_list, )* ) }
|
||||
};
|
||||
|
||||
let filter = self.filter.clone();
|
||||
|
||||
TokenStream2::from(quote! {
|
||||
Query<#components, #filter>
|
||||
})
|
||||
.to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
|
||||
struct Update {
|
||||
queries: Vec<Query>,
|
||||
resources: Vec<Ident>,
|
||||
}
|
||||
|
||||
impl Update {
|
||||
const MIN_RESOURCE: usize = 0;
|
||||
const MIN_COMPONENTS: usize = 1;
|
||||
|
||||
pub fn new(queries: impl Iterator<Item = usize>, resources: usize) -> Self {
|
||||
Self {
|
||||
queries: queries
|
||||
.enumerate()
|
||||
.map(|(query, component_count)| {
|
||||
Query::new(
|
||||
(0..component_count).map(|component| format_ident!("C{component}Q{query}")),
|
||||
query,
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
resources: (0..resources)
|
||||
.map(|resource| format_ident!("Res{resource}"))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn filter_types(&self) -> TokenStream2 {
|
||||
let filter_types = self
|
||||
.queries
|
||||
.iter()
|
||||
.map(|query| query.filter())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if filter_types.len() == 1 {
|
||||
let filter = &filter_types[0];
|
||||
|
||||
quote! {
|
||||
#filter
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
( #( #filter_types, )* )
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn filter_type_impls(&self) -> TokenStream2 {
|
||||
let filter_types = self
|
||||
.queries
|
||||
.iter()
|
||||
.map(|query| query.filter())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
quote! {
|
||||
#( #filter_types, )*
|
||||
}
|
||||
}
|
||||
|
||||
pub fn filter_requirements(&self) -> TokenStream2 {
|
||||
let filter_requirements = self
|
||||
.queries
|
||||
.iter()
|
||||
.map(|query| query.filter())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
quote! {
|
||||
#( #filter_requirements: CheckFilter + 'static, )*
|
||||
}
|
||||
}
|
||||
|
||||
pub fn query_types(&self) -> TokenStream2 {
|
||||
let query_types = self
|
||||
.queries
|
||||
.iter()
|
||||
.map(|query| query.types())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
quote! {
|
||||
( #( #query_types, )* )
|
||||
}
|
||||
}
|
||||
|
||||
pub fn query_type_impls(&self) -> TokenStream2 {
|
||||
let query_types = self
|
||||
.queries
|
||||
.iter()
|
||||
.map(|query| query.components())
|
||||
.flatten()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
quote! {
|
||||
#( #query_types, )*
|
||||
}
|
||||
}
|
||||
|
||||
pub fn component_requirements(&self) -> TokenStream2 {
|
||||
let component_requirements = self
|
||||
.queries
|
||||
.iter()
|
||||
.map(|query| {
|
||||
query.components.iter().map(|component| {
|
||||
quote! {
|
||||
#component: EntityComponent + ComponentDebug,
|
||||
}
|
||||
})
|
||||
})
|
||||
.flatten()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
quote! {
|
||||
#( #component_requirements )*
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resource_store(&self) -> (TokenStream2, Vec<Ident>) {
|
||||
let resource_types = &self.resources;
|
||||
let resource_idents = self
|
||||
.resources
|
||||
.iter()
|
||||
.map(|resource| format_ident!("res_{resource}"))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if self.resources.is_empty() {
|
||||
(quote! {}, Vec::new())
|
||||
} else {
|
||||
(
|
||||
quote! {
|
||||
#[allow(non_snake_case)]
|
||||
let (
|
||||
#( #resource_idents, )*
|
||||
): (
|
||||
#( &mut #resource_types, )*
|
||||
) = world.resources.get_mut()?;
|
||||
},
|
||||
resource_idents,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resource_types(&self) -> TokenStream2 {
|
||||
let resource_types = &self.resources;
|
||||
|
||||
quote! {
|
||||
( #( #resource_types, )* )
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resource_type_impls(&self) -> TokenStream2 {
|
||||
let resource_types = &self.resources;
|
||||
|
||||
quote! {
|
||||
#( #resource_types, )*
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reource_requirement(&self) -> TokenStream2 {
|
||||
let resource_types = &self.resources;
|
||||
|
||||
quote! {
|
||||
#( #resource_types: ResourceTrait, )*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Update {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream2) {
|
||||
// generics to specify trait implementations
|
||||
let filter_types = self.filter_types();
|
||||
let filter_type_impls = self.filter_type_impls();
|
||||
let query_types = self.query_types();
|
||||
let query_type_impls = self.query_type_impls();
|
||||
let resource_types = self.resource_types();
|
||||
let resource_type_impls = self.resource_type_impls();
|
||||
|
||||
// panic!("{resource_type_impls}");
|
||||
|
||||
// function parameter
|
||||
let queries = &self.queries;
|
||||
let resources = if self.resources.is_empty() {
|
||||
quote! {}
|
||||
} else {
|
||||
let resources = &self.resources;
|
||||
|
||||
quote! {
|
||||
#( &mut #resources, )*
|
||||
}
|
||||
};
|
||||
|
||||
// trait requirements
|
||||
let filter_requirements = self.filter_requirements();
|
||||
let component_requirements = self.component_requirements();
|
||||
let reource_requirement = self.reource_requirement();
|
||||
|
||||
let query_count = self.queries.len();
|
||||
|
||||
let verify_dedup = self
|
||||
.queries
|
||||
.iter()
|
||||
.map(|query| query.dedup_checks())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let check_entities = self
|
||||
.queries
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, query)| query.check_entity(index))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let component_stores_and_queries = self
|
||||
.queries
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, query)| query.component_store_and_query(index))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut component_stores = Vec::new();
|
||||
let mut query_structs = Vec::new();
|
||||
let mut query_idents = Vec::new();
|
||||
|
||||
for (component_store, query_struct, query_ident) in component_stores_and_queries {
|
||||
component_stores.push(component_store);
|
||||
query_structs.push(query_struct);
|
||||
query_idents.push(query_ident);
|
||||
}
|
||||
|
||||
let (resource_store, resource_idents) = self.resource_store();
|
||||
|
||||
TokenStream2::from(quote! {
|
||||
impl<Func, #filter_type_impls #query_type_impls #resource_type_impls> CreateArchetype<#query_types, #resource_types, Func, #filter_types> for Archetype
|
||||
where
|
||||
Func: Fn(&mut Commands, #( #queries, )* #resources ) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
#filter_requirements
|
||||
#component_requirements
|
||||
#reource_requirement
|
||||
{
|
||||
fn create(f: Func) -> Self {
|
||||
#( #verify_dedup )*
|
||||
|
||||
Self {
|
||||
queries: #query_count,
|
||||
|
||||
check_entity: Box::new(move |entity| {
|
||||
let mut comply_queries = Vec::new();
|
||||
|
||||
#( #check_entities )*
|
||||
|
||||
if comply_queries.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(comply_queries)
|
||||
}
|
||||
}),
|
||||
|
||||
create_callback: Box::new(move |entities| {
|
||||
#( #component_stores )*
|
||||
|
||||
let f = f.clone();
|
||||
|
||||
Ok(Box::new(move |commands, world| {
|
||||
#( #query_structs )*
|
||||
#resource_store
|
||||
|
||||
f(commands, #( #query_idents, )* #( #resource_idents, )* )
|
||||
}))
|
||||
}),
|
||||
|
||||
entities: IndexMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Func, #filter_type_impls #query_type_impls #resource_type_impls> AddUpdates<#query_types, #resource_types, Func, #filter_types> for Updates
|
||||
where
|
||||
Func: Fn(&mut Commands, #( #queries, )* #resources ) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
#filter_requirements
|
||||
#component_requirements
|
||||
#reource_requirement
|
||||
{
|
||||
fn add_update(
|
||||
&mut self,
|
||||
priority: u32,
|
||||
func: Func,
|
||||
) -> Result<()> {
|
||||
self.add(priority, Archetype::create(func))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Func, #filter_type_impls #query_type_impls #resource_type_impls> AddUpdates<#query_types, #resource_types, Func, #filter_types> for WorldBuilder
|
||||
where
|
||||
Func: Fn(&mut Commands, #( #queries, )* #resources ) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
#filter_requirements
|
||||
#component_requirements
|
||||
#reource_requirement
|
||||
{
|
||||
fn add_update(
|
||||
&mut self,
|
||||
priority: u32,
|
||||
func: Func,
|
||||
) -> Result<()> {
|
||||
self.updates.add_update(priority, func)
|
||||
}
|
||||
}
|
||||
})
|
||||
.to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(max_components: usize, max_resources: usize) -> TokenStream {
|
||||
let mut queries: Vec<Vec<usize>> = Vec::new();
|
||||
|
||||
for c in Update::MIN_COMPONENTS..=max_components {
|
||||
queries.push(vec![c]);
|
||||
}
|
||||
|
||||
for x in Update::MIN_COMPONENTS..=max_components {
|
||||
for y in Update::MIN_COMPONENTS..=max_components {
|
||||
queries.push(vec![x, y]);
|
||||
}
|
||||
}
|
||||
|
||||
for x in Update::MIN_COMPONENTS..=max_components {
|
||||
for y in Update::MIN_COMPONENTS..=max_components {
|
||||
for z in Update::MIN_COMPONENTS..=max_components {
|
||||
queries.push(vec![x, y, z]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut resources = Vec::new();
|
||||
|
||||
for resource in Update::MIN_RESOURCE..=max_resources {
|
||||
resources.push(resource);
|
||||
}
|
||||
|
||||
let mut updates = Vec::new();
|
||||
|
||||
for query in queries.iter() {
|
||||
for resource_count in resources.iter() {
|
||||
updates.push(Update::new(query.iter().cloned(), *resource_count));
|
||||
}
|
||||
}
|
||||
|
||||
// updates.push(Update::new(vec![1].into_iter(), 0));
|
||||
|
||||
// let q = quote! {
|
||||
// #( #updates )*
|
||||
// };
|
||||
|
||||
// panic!("{q}");
|
||||
|
||||
TokenStream::from(quote! {
|
||||
#( #updates )*
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue