use lyra_resource::ResourceManager; use crate::ecs::EventsPlugin; use crate::ecs::components::DeltaTimePlugin; use crate::game::Game; use crate::input::InputPlugin; use crate::render::window::WindowPlugin; /// 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); fn is_ready(&self, _game: &mut Game) -> bool { true } fn complete(&self, _game: &mut Game) { } fn cleanup(&self, _game: &mut Game) { } } impl
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 (&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) }
#[derive(Default)]
pub struct ResourceManagerPlugin;
impl Plugin for ResourceManagerPlugin {
fn setup(&self, game: &mut Game) {
game.world().add_resource(ResourceManager::new());
}
}
/// Default plugins of Lyra. Make sure to have these added to the Game first
#[derive(Default)]
pub struct DefaultPlugins;
impl Plugin for DefaultPlugins {
fn setup(&self, game: &mut Game) {
EventsPlugin.setup(game);
InputPlugin.setup(game);
ResourceManagerPlugin.setup(game);
WindowPlugin::default().setup(game);
DeltaTimePlugin.setup(game);
}
}