72 lines
1.4 KiB
Rust
72 lines
1.4 KiB
Rust
use std::time::Duration;
|
|
|
|
use anyhow::Result;
|
|
use flax::World;
|
|
|
|
use super::events::Events;
|
|
|
|
pub trait Module {
|
|
fn on_update(
|
|
&mut self,
|
|
world: &mut World,
|
|
events: &mut Events,
|
|
frame_time: Duration,
|
|
) -> Result<()>;
|
|
}
|
|
|
|
pub struct ModulesStack {
|
|
modules: Vec<Box<dyn Module>>,
|
|
}
|
|
|
|
impl ModulesStack {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
modules: Vec::new(),
|
|
}
|
|
}
|
|
|
|
pub fn iter(&self) -> std::slice::Iter<Box<dyn Module>> {
|
|
self.modules.iter()
|
|
}
|
|
|
|
pub fn iter_mut(&mut self) -> std::slice::IterMut<Box<dyn Module>> {
|
|
self.modules.iter_mut()
|
|
}
|
|
|
|
pub fn push<T: 'static + Module>(&mut self, layer: T) {
|
|
let layer = Box::new(layer);
|
|
self.modules.push(layer);
|
|
}
|
|
|
|
pub fn insert<T: 'static + Module>(&mut self, index: usize, layer: T) {
|
|
let layer = Box::new(layer);
|
|
self.modules.insert(index, layer);
|
|
}
|
|
}
|
|
|
|
impl Default for ModulesStack {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl<'a> IntoIterator for &'a ModulesStack {
|
|
type Item = &'a Box<dyn Module>;
|
|
|
|
type IntoIter = std::slice::Iter<'a, Box<dyn Module>>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
self.iter()
|
|
}
|
|
}
|
|
|
|
impl<'a> IntoIterator for &'a mut ModulesStack {
|
|
type Item = &'a mut Box<dyn Module>;
|
|
|
|
type IntoIter = std::slice::IterMut<'a, Box<dyn Module>>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
self.iter_mut()
|
|
}
|
|
}
|