Finish implementing plugins, create default plugins, create input plugin

This commit is contained in:
SeanOMik 2023-09-10 00:38:54 -04:00
parent e96cb3585b
commit 71c1188f3a
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
7 changed files with 96 additions and 48 deletions

View File

@ -30,3 +30,4 @@ edict = "0.5.0"
atomicell = "0.1.9"
aligned-vec = "0.5.0"
tracing-appender = "0.2.2"
stopwatch = "0.0.7"

View File

@ -1,3 +0,0 @@
pub struct App {
}

View File

@ -1,4 +1,4 @@
use std::{collections::{HashMap, VecDeque}, any::{TypeId, Any}, cell::{RefCell, Ref, RefMut}};
use std::{collections::{HashMap, VecDeque}, any::{TypeId, Any}, cell::RefCell};
use crate::castable_any::CastableAny;

View File

@ -241,9 +241,7 @@ impl Default for Game {
impl Game {
pub async fn initialize() -> Game {
let mut def = Self::default();
//def.system_dispatcher.add_system("input", InputSystem::new(), &[]);
def
Self::default()
}
pub fn with_system<S>(&mut self, name: &str, system: S, depends: &[&str]) -> &mut Self
@ -279,7 +277,8 @@ impl Game {
.with(fmt::layer().with_writer(stdout_layer))
.with(filter::Targets::new()
.with_target("lyra_engine", Level::TRACE)
.with_default(Level::INFO))
.with_target("wgpu_core", Level::INFO)
.with_default(Level::DEBUG))
.init();
// setup all the plugins

View File

@ -221,26 +221,10 @@ impl<T: Clone + Hash + Eq + PartialEq + 'static> InputStorage for InputButtons<T
}
}
pub struct InputSystem {
gilrs: Option<Arc<Mutex<Gilrs>>>, // TODO
}
#[derive(Default)]
pub struct InputSystem;
impl InputSystem {
pub(crate) fn new() -> Self {
let gilrs = match Gilrs::new() {
Ok(g) => Some(Arc::new(Mutex::new(g))),
Err(e) => {
warn!("Failure to initialize gilrs, gamepads will not work!\n{}", e);
None
}
};
Self {
gilrs,
}
}
pub fn update(&mut self, event: &InputEvent, world: &mut edict::World) -> bool {
let event_queue = world.get_resource_mut::<EventQueue>();
if event_queue.is_none() {
@ -349,28 +333,12 @@ impl SimpleSystem for InputSystem {
}
}
fn input_system_fn(world: &mut edict::World) -> anyhow::Result<()> {
//let world = &mut controls.world;
/// Plugin that runs InputSystem
#[derive(Default)]
pub struct InputPlugin;
let queue = world.get_resource_mut::<EventQueue>()
.map(|q| q.read_events::<InputEvent>()).flatten();
if queue.is_none() {
return Ok(());
}
let mut events = queue.unwrap();
let mut input = InputSystem::new();
while let Some(event) = events.pop_front() {
input.update(&event, world);
}
Ok(())
}
impl Plugin for InputSystem {
impl Plugin for InputPlugin {
fn setup(&self, game: &mut crate::game::Game) {
game.with_system("input", input_system_fn, &[]);
game.with_system("input", InputSystem::default(), &[]);
}
}

View File

@ -7,4 +7,18 @@ pub mod math;
pub mod input;
pub mod castable_any;
pub mod plugin;
pub mod app;
use plugin::Plugin;
use crate::input::InputPlugin;
/// 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::Game) {
// setup input
InputPlugin::default().setup(game);
}
}

View File

@ -1,7 +1,76 @@
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) }