lyra-engine/src/plugin.rs

76 lines
2.8 KiB
Rust

use crate::game::Game;
/// A Plugin is something you can add to a `Game` that can be used to define systems, or spawn initial entities.
pub trait Plugin {
/// Setup this plugin. This runs before the game has started
fn setup(&self, game: &mut Game);
}
impl<P> Plugin for P
where P: Fn(&mut Game)
{
fn setup(&self, game: &mut Game) {
self(game);
}
}
/// Represents a set of plugins that will be executed in order they are supplied.
#[derive(Default)]
pub struct PluginSet {
/// A set of plugins that will be executed in order
pub plugins: Vec<Box<dyn Plugin>>,
}
impl PluginSet {
pub fn new() -> Self {
Self::default()
}
pub fn add_plugin<P>(&mut self, plugin: P) -> &mut Self
where
P: Plugin + 'static
{
self.plugins.push(Box::new(plugin));
self
}
}
impl Plugin for PluginSet {
fn setup(&self, game: &mut Game) {
for plugin in self.plugins.iter() {
plugin.setup(game);
}
}
}
// Macro used for implementing PluginSet for tuples
macro_rules! impl_tuple_plugin_set {
( $(($name: ident, $index: tt))+ ) => (
impl<$($name: Plugin + 'static),+> From<($($name,)+)> for PluginSet {
fn from(value: ($($name,)+)) -> Self {
let plugins = vec![$(Box::new(value.$index) as Box<(dyn Plugin + 'static)>),+];
Self {
plugins,
}
}
}
);
}
impl_tuple_plugin_set! { (C0, 0) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) (C3, 3) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) (C3, 3) (C4, 4) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) (C3, 3) (C4, 4) (C5, 5) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) (C3, 3) (C4, 4) (C5, 5) (C6, 6) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) (C3, 3) (C4, 4) (C5, 5) (C6, 6) (C7, 7) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) (C3, 3) (C4, 4) (C5, 5) (C6, 6) (C7, 7) (C8, 8) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) (C3, 3) (C4, 4) (C5, 5) (C6, 6) (C7, 7) (C8, 8) (C9, 9) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) (C3, 3) (C4, 4) (C5, 5) (C6, 6) (C7, 7) (C8, 8) (C9, 9) (C10, 10) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) (C3, 3) (C4, 4) (C5, 5) (C6, 6) (C7, 7) (C8, 8) (C9, 9) (C10, 10) (C11, 11) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) (C3, 3) (C4, 4) (C5, 5) (C6, 6) (C7, 7) (C8, 8) (C9, 9) (C10, 10) (C11, 11) (C12, 12) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) (C3, 3) (C4, 4) (C5, 5) (C6, 6) (C7, 7) (C8, 8) (C9, 9) (C10, 10) (C11, 11) (C12, 12) (C13, 13) }
impl_tuple_plugin_set! { (C0, 0) (C1, 1) (C2, 2) (C3, 3) (C4, 4) (C5, 5) (C6, 6) (C7, 7) (C8, 8) (C9, 9) (C10, 10) (C11, 11) (C12, 12) (C13, 13) (C14, 14) }