2020-07-14 23:23:25 +00:00
|
|
|
use crate::{Resources, World};
|
2020-07-10 04:18:35 +00:00
|
|
|
use std::borrow::Cow;
|
2020-07-14 23:23:25 +00:00
|
|
|
use fixedbitset::FixedBitSet;
|
|
|
|
use hecs::{Query, Access};
|
2020-07-10 04:18:35 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum ThreadLocalExecution {
|
|
|
|
Immediate,
|
|
|
|
NextFlush,
|
2020-07-10 08:37:06 +00:00
|
|
|
}
|
2020-07-10 04:18:35 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
|
|
|
pub struct SystemId(pub u32);
|
|
|
|
|
|
|
|
impl SystemId {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
SystemId(rand::random::<u32>())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait System: Send + Sync {
|
|
|
|
fn name(&self) -> Cow<'static, str>;
|
|
|
|
fn id(&self) -> SystemId;
|
2020-07-14 02:29:34 +00:00
|
|
|
fn update_archetype_access(&mut self, world: &World);
|
2020-07-14 21:19:17 +00:00
|
|
|
fn get_archetype_access(&self) -> &ArchetypeAccess;
|
2020-07-10 04:18:35 +00:00
|
|
|
fn thread_local_execution(&self) -> ThreadLocalExecution;
|
|
|
|
fn run(&mut self, world: &World, resources: &Resources);
|
|
|
|
fn run_thread_local(&mut self, world: &mut World, resources: &mut Resources);
|
|
|
|
fn initialize(&mut self, _resources: &mut Resources) {}
|
|
|
|
}
|
2020-07-14 23:23:25 +00:00
|
|
|
|
|
|
|
// credit to Ratysz from the Yaks codebase
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ArchetypeAccess {
|
|
|
|
pub immutable: FixedBitSet,
|
|
|
|
pub mutable: FixedBitSet,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ArchetypeAccess {
|
|
|
|
pub fn is_compatible(&self, other: &ArchetypeAccess) -> bool {
|
|
|
|
self.mutable.is_disjoint(&other.mutable)
|
|
|
|
&& self.mutable.is_disjoint(&other.immutable)
|
|
|
|
&& self.immutable.is_disjoint(&other.mutable)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn union(&mut self, other: &ArchetypeAccess) {
|
|
|
|
self.mutable.union_with(&other.mutable);
|
|
|
|
self.immutable.union_with(&other.immutable);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_access_for_query<Q>(&mut self, world: &World)
|
|
|
|
where
|
|
|
|
Q: Query,
|
|
|
|
{
|
|
|
|
self.immutable.clear();
|
|
|
|
self.mutable.clear();
|
|
|
|
let iterator = world.archetypes();
|
|
|
|
let bits = iterator.len();
|
|
|
|
self.immutable.grow(bits);
|
|
|
|
self.mutable.grow(bits);
|
|
|
|
iterator
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(index, archetype)| archetype.access::<Q>().map(|access| (index, access)))
|
|
|
|
.for_each(|(archetype, access)| match access {
|
|
|
|
Access::Read => self.immutable.set(archetype, true),
|
|
|
|
Access::Write => self.mutable.set(archetype, true),
|
|
|
|
Access::Iterate => (),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|