use alloc::collections::VecDeque;
use crate::world::World;
use super::{encoder::LocalActionEncoder, ActionEncoder, ActionFn, LocalActionFn};
#[derive(Default)]
#[repr(transparent)]
pub struct ActionBuffer {
actions: VecDeque<ActionFn<'static>>,
}
impl ActionBuffer {
#[inline(always)]
pub fn new() -> Self {
Self {
actions: VecDeque::new(),
}
}
#[inline(always)]
pub(super) fn actions(&mut self) -> &mut VecDeque<ActionFn<'static>> {
&mut self.actions
}
#[inline(always)]
pub fn encoder<'a>(&'a mut self, world: &'a World) -> ActionEncoder<'a> {
ActionEncoder::new(self, world.entities())
}
#[inline(always)]
pub fn execute(&mut self, world: &mut World) -> bool {
if self.actions.is_empty() {
return false;
}
while let Some(fun) = self.actions.pop_front() {
fun.call(world);
}
true
}
}
#[derive(Default)]
#[repr(transparent)]
pub struct LocalActionBuffer {
actions: VecDeque<LocalActionFn<'static>>,
}
impl LocalActionBuffer {
#[inline(always)]
pub fn new() -> Self {
Self {
actions: VecDeque::new(),
}
}
#[inline(always)]
pub(super) fn actions(&mut self) -> &mut VecDeque<LocalActionFn<'static>> {
&mut self.actions
}
#[inline(always)]
pub fn encoder<'a>(&'a mut self, world: &'a World) -> LocalActionEncoder<'a> {
LocalActionEncoder::new(self, world.entities())
}
#[inline(always)]
pub fn execute(&mut self, world: &mut World) -> bool {
if self.actions.is_empty() {
return false;
}
while let Some(fun) = self.actions.pop_front() {
fun.call(world);
}
true
}
#[inline(always)]
pub(crate) fn pop(&mut self) -> Option<LocalActionFn<'static>> {
self.actions.pop_front()
}
}
pub trait ActionBufferSliceExt {
fn execute_all(&mut self, world: &mut World) -> bool;
}
impl ActionBufferSliceExt for [ActionBuffer] {
#[inline(always)]
fn execute_all(&mut self, world: &mut World) -> bool {
self.iter_mut()
.fold(false, |acc, encoder| acc | encoder.execute(world))
}
}
impl ActionBufferSliceExt for [LocalActionBuffer] {
#[inline(always)]
fn execute_all(&mut self, world: &mut World) -> bool {
self.iter_mut()
.fold(false, |acc, encoder| acc | encoder.execute(world))
}
}