lyra-engine/src/plugin.rs

117 lines
3.7 KiB
Rust
Raw Normal View History

use lyra_resource::ResourceManager;
use crate::ecs::EventsPlugin;
use crate::ecs::components::DeltaTimePlugin;
2023-09-09 14:51:58 +00:00
use crate::game::Game;
use crate::input::InputPlugin;
use crate::render::window::WindowPlugin;
2023-09-09 14:51:58 +00:00
/// A Plugin is something you can add to a `Game` that can be used to define systems, or spawn initial entities.
2023-09-09 14:51:58 +00:00
pub trait Plugin {
/// Setup this plugin. This runs before the game has started
fn setup(&self, game: &mut Game);
2023-10-23 01:49:31 +00:00
fn is_ready(&self, _game: &mut Game) -> bool {
true
}
2023-10-23 01:49:31 +00:00
fn complete(&self, _game: &mut Game) {
}
2023-10-23 01:49:31 +00:00
fn cleanup(&self, _game: &mut Game) {
}
2023-09-09 14:51:58 +00:00
}
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) }
#[derive(Default)]
pub struct ResourceManagerPlugin;
impl Plugin for ResourceManagerPlugin {
fn setup(&self, game: &mut Game) {
game.world().insert_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) {
2023-10-23 01:49:31 +00:00
EventsPlugin.setup(game);
InputPlugin.setup(game);
ResourceManagerPlugin.setup(game);
WindowPlugin::default().setup(game);
DeltaTimePlugin.setup(game);
}
}