2023-09-14 16:58:59 +00:00
|
|
|
use lyra_resource::ResourceManager;
|
|
|
|
|
2023-09-17 16:08:08 +00:00
|
|
|
use crate::ecs::EventsPlugin;
|
2023-10-27 02:26:13 +00:00
|
|
|
use crate::ecs::components::DeltaTimePlugin;
|
2023-09-09 14:51:58 +00:00
|
|
|
use crate::game::Game;
|
2023-09-14 16:58:59 +00:00
|
|
|
use crate::input::InputPlugin;
|
2023-09-17 16:08:08 +00:00
|
|
|
use crate::render::window::WindowPlugin;
|
2023-09-09 14:51:58 +00:00
|
|
|
|
2023-09-10 04:38:54 +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-09-17 16:08:08 +00:00
|
|
|
|
2023-10-23 01:49:31 +00:00
|
|
|
fn is_ready(&self, _game: &mut Game) -> bool {
|
2023-09-17 16:08:08 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2023-10-23 01:49:31 +00:00
|
|
|
fn complete(&self, _game: &mut Game) {
|
2023-09-17 16:08:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-23 01:49:31 +00:00
|
|
|
fn cleanup(&self, _game: &mut Game) {
|
2023-09-17 16:08:08 +00:00
|
|
|
|
|
|
|
}
|
2023-09-09 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 04:38:54 +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) }
|
2023-09-14 16:58:59 +00:00
|
|
|
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) {
|
2023-12-26 19:12:53 +00:00
|
|
|
game.world().add_resource(ResourceManager::new());
|
2023-09-14 16:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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);
|
2023-09-17 16:08:08 +00:00
|
|
|
WindowPlugin::default().setup(game);
|
2023-10-27 02:26:13 +00:00
|
|
|
DeltaTimePlugin.setup(game);
|
2023-09-14 16:58:59 +00:00
|
|
|
}
|
|
|
|
}
|