Clean up code a bit
This commit is contained in:
parent
73b44857eb
commit
3068710ba4
|
@ -89,10 +89,17 @@ impl SystemDispatcher {
|
||||||
/// and will cause a panic later on in the engine.
|
/// and will cause a panic later on in the engine.
|
||||||
///
|
///
|
||||||
/// TODO: Return false if a cycle was created, making it impossible to know when to dispatch them. This will mean that the System would not of had been added.
|
/// TODO: Return false if a cycle was created, making it impossible to know when to dispatch them. This will mean that the System would not of had been added.
|
||||||
pub fn add_system(&mut self, name: String, system: Box<dyn SimpleSystem>, dependencies: Vec<String>) -> bool {
|
pub fn add_system<S>(&mut self, name: &str, system: S, dependencies: &[&str]) -> bool
|
||||||
|
where
|
||||||
|
S: SimpleSystem + 'static
|
||||||
|
{
|
||||||
|
let name = name.to_string();
|
||||||
|
let dependencies: Vec<String> = dependencies.iter().map(|s| s.to_string()).collect();
|
||||||
|
let system = Box::new(system) as Box<dyn SimpleSystem>;
|
||||||
|
|
||||||
let added_index =
|
let added_index =
|
||||||
self.graph.add_node(SystemGraphNode {
|
self.graph.add_node(SystemGraphNode {
|
||||||
name: name.clone(),
|
name: name.to_string(),
|
||||||
dependent_on: dependencies.clone(),
|
dependent_on: dependencies.clone(),
|
||||||
system,
|
system,
|
||||||
});
|
});
|
||||||
|
|
30
src/game.rs
30
src/game.rs
|
@ -13,7 +13,7 @@ use tracing_subscriber::{
|
||||||
|
|
||||||
use winit::{window::{WindowBuilder, Window}, event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode, DeviceEvent}, event_loop::{EventLoop, ControlFlow}};
|
use winit::{window::{WindowBuilder, Window}, event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode, DeviceEvent}, event_loop::{EventLoop, ControlFlow}};
|
||||||
|
|
||||||
use crate::{render::renderer::{Renderer, BasicRenderer}, input_event::InputEvent, ecs::{SimpleSystem, SystemDispatcher, resources::{WindowState, Events, EventQueue}}};
|
use crate::{render::renderer::{Renderer, BasicRenderer}, input_event::InputEvent, ecs::{SimpleSystem, SystemDispatcher, resources::{WindowState, Events, EventQueue}}, input::InputSystem};
|
||||||
|
|
||||||
pub struct Controls<'a> {
|
pub struct Controls<'a> {
|
||||||
pub world: &'a mut edict::World,
|
pub world: &'a mut edict::World,
|
||||||
|
@ -87,12 +87,6 @@ struct GameLoop {
|
||||||
|
|
||||||
impl GameLoop {
|
impl GameLoop {
|
||||||
pub async fn new(window: Arc<Window>, world: Arc<Mutex<edict::World>>, user_systems: SystemDispatcher) -> GameLoop {
|
pub async fn new(window: Arc<Window>, world: Arc<Mutex<edict::World>>, user_systems: SystemDispatcher) -> GameLoop {
|
||||||
// Create the EventQueue resource in the world
|
|
||||||
{
|
|
||||||
let mut world = world.lock().await;
|
|
||||||
world.insert_resource(EventQueue::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
window: Arc::clone(&window),
|
window: Arc::clone(&window),
|
||||||
renderer: Box::new(BasicRenderer::create_with_window(window).await),
|
renderer: Box::new(BasicRenderer::create_with_window(window).await),
|
||||||
|
@ -108,6 +102,12 @@ impl GameLoop {
|
||||||
self.renderer.on_resize(new_size);
|
self.renderer.on_resize(new_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn on_init(&mut self) {
|
||||||
|
// Create the EventQueue resource in the world
|
||||||
|
let mut world = self.world.lock().await;
|
||||||
|
world.insert_resource(EventQueue::new());
|
||||||
|
}
|
||||||
|
|
||||||
pub fn run_sync(&mut self, event: Event<()>, control_flow: &mut ControlFlow) {
|
pub fn run_sync(&mut self, event: Event<()>, control_flow: &mut ControlFlow) {
|
||||||
block_on(self.run_event_loop(event, control_flow))
|
block_on(self.run_event_loop(event, control_flow))
|
||||||
}
|
}
|
||||||
|
@ -291,7 +291,7 @@ impl GameLoop {
|
||||||
|
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
world: Option<Arc<Mutex<edict::World>>>,
|
world: Option<Arc<Mutex<edict::World>>>,
|
||||||
system_dispatcher: Option<SystemDispatcher>
|
system_dispatcher: Option<SystemDispatcher>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Game {
|
impl Default for Game {
|
||||||
|
@ -317,7 +317,9 @@ impl Game {
|
||||||
.with(layer.with_filter(filter))
|
.with(layer.with_filter(filter))
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
Self::default()
|
let mut def = Self::default();
|
||||||
|
def.system_dispatcher.as_mut().unwrap().add_system("input", InputSystem::new(), &[]);
|
||||||
|
def
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_world(&mut self, world: edict::World) -> &mut Self {
|
pub fn with_world(&mut self, world: edict::World) -> &mut Self {
|
||||||
|
@ -336,14 +338,8 @@ impl Game {
|
||||||
where
|
where
|
||||||
S: SimpleSystem + 'static
|
S: SimpleSystem + 'static
|
||||||
{
|
{
|
||||||
|
|
||||||
let depends: Vec<String> = depends
|
|
||||||
.iter()
|
|
||||||
.map(|s| s.to_string())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let dispatcher = self.system_dispatcher.as_mut().unwrap();
|
let dispatcher = self.system_dispatcher.as_mut().unwrap();
|
||||||
dispatcher.add_system(name.to_string(), Box::new(system), depends);
|
dispatcher.add_system(name, system, depends);
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -358,6 +354,8 @@ impl Game {
|
||||||
|
|
||||||
let mut g_loop = GameLoop::new(Arc::clone(&window), world, systems).await;
|
let mut g_loop = GameLoop::new(Arc::clone(&window), world, systems).await;
|
||||||
|
|
||||||
|
g_loop.on_init().await;
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
g_loop.run_sync(event, control_flow);
|
g_loop.run_sync(event, control_flow);
|
||||||
});
|
});
|
||||||
|
|
18
src/input.rs
18
src/input.rs
|
@ -1,11 +1,11 @@
|
||||||
use std::{any::{Any, TypeId}, collections::{HashMap, hash_map::DefaultHasher, VecDeque, HashSet}, hash::{Hash, Hasher}, sync::{Arc, Mutex}, cell::RefCell};
|
use std::{collections::{HashMap, hash_map::DefaultHasher}, hash::{Hash, Hasher}, sync::{Arc, Mutex}};
|
||||||
|
|
||||||
use gilrs_core::Gilrs;
|
use gilrs_core::Gilrs;
|
||||||
use glam::Vec2;
|
use glam::Vec2;
|
||||||
use tracing::{warn, debug};
|
use tracing::{warn, debug};
|
||||||
use winit::event::{VirtualKeyCode, ElementState, MouseScrollDelta};
|
use winit::event::{ElementState, MouseScrollDelta};
|
||||||
|
|
||||||
use crate::{ecs::{SimpleSystem, resources::{CastableAny, Events, EventQueue}}, input_event::InputEvent};
|
use crate::{ecs::{SimpleSystem, resources::EventQueue}, input_event::InputEvent};
|
||||||
|
|
||||||
pub type KeyCode = winit::event::VirtualKeyCode;
|
pub type KeyCode = winit::event::VirtualKeyCode;
|
||||||
|
|
||||||
|
@ -239,11 +239,11 @@ impl<T: Clone + Hash + Eq + PartialEq + 'static> InputStorage for InputButtons<T
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct InputEventUpdater {
|
pub struct InputSystem {
|
||||||
gilrs: Option<Arc<Mutex<Gilrs>>>, // TODO
|
gilrs: Option<Arc<Mutex<Gilrs>>>, // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InputEventUpdater {
|
impl InputSystem {
|
||||||
pub(crate) fn new() -> Self {
|
pub(crate) fn new() -> Self {
|
||||||
let gilrs = match Gilrs::new() {
|
let gilrs = match Gilrs::new() {
|
||||||
Ok(g) => Some(Arc::new(Mutex::new(g))),
|
Ok(g) => Some(Arc::new(Mutex::new(g))),
|
||||||
|
@ -270,7 +270,7 @@ impl InputEventUpdater {
|
||||||
InputEvent::KeyboardInput { input, .. } => {
|
InputEvent::KeyboardInput { input, .. } => {
|
||||||
if let Some(code) = input.virtual_keycode {
|
if let Some(code) = input.virtual_keycode {
|
||||||
drop(event_queue);
|
drop(event_queue);
|
||||||
let mut e = world.with_resource(|| InputButtons::<KeyCode>::new());
|
let e = world.with_resource(|| InputButtons::<KeyCode>::new());
|
||||||
//let mut e = with_resource_mut(world, || InputButtons::<KeyCode>::new());
|
//let mut e = with_resource_mut(world, || InputButtons::<KeyCode>::new());
|
||||||
e.add_input_from_winit(code, input.state);
|
e.add_input_from_winit(code, input.state);
|
||||||
}
|
}
|
||||||
|
@ -320,7 +320,7 @@ impl InputEventUpdater {
|
||||||
event_queue.trigger_event(button_event.clone());
|
event_queue.trigger_event(button_event.clone());
|
||||||
drop(event_queue);
|
drop(event_queue);
|
||||||
|
|
||||||
let mut e = world.with_resource(|| InputButtons::<MouseButton>::new());
|
let e = world.with_resource(|| InputButtons::<MouseButton>::new());
|
||||||
e.add_input_from_winit(button_event, state.clone());
|
e.add_input_from_winit(button_event, state.clone());
|
||||||
},
|
},
|
||||||
InputEvent::Touch(t) => {
|
InputEvent::Touch(t) => {
|
||||||
|
@ -336,7 +336,7 @@ impl InputEventUpdater {
|
||||||
finger_id: t.id,
|
finger_id: t.id,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut touches = world.with_resource(|| Touches::new());
|
let touches = world.with_resource(|| Touches::new());
|
||||||
touches.touches.push(touch);
|
touches.touches.push(touch);
|
||||||
},
|
},
|
||||||
_ => {},
|
_ => {},
|
||||||
|
@ -346,7 +346,7 @@ impl InputEventUpdater {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SimpleSystem for InputEventUpdater {
|
impl SimpleSystem for InputSystem {
|
||||||
fn execute_mut(&mut self, controls: &mut crate::game::Controls) -> anyhow::Result<()> {
|
fn execute_mut(&mut self, controls: &mut crate::game::Controls) -> anyhow::Result<()> {
|
||||||
let world = &mut controls.world;
|
let world = &mut controls.world;
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ use game::Game;
|
||||||
use input_event::InputEvent;
|
use input_event::InputEvent;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
use crate::input::{InputEventUpdater, KeyCode, MouseMotion, MouseExact, CursorEnteredWindow, MouseScroll, CursorLeftWindow, MouseButton, InputButtons};
|
use crate::input::{InputSystem, KeyCode, MouseMotion, MouseExact, CursorEnteredWindow, MouseScroll, CursorLeftWindow, MouseButton, InputButtons};
|
||||||
use crate::render::material::Material;
|
use crate::render::material::Material;
|
||||||
use crate::render::texture::Texture;
|
use crate::render::texture::Texture;
|
||||||
use crate::ecs::components::camera::CameraComponent;
|
use crate::ecs::components::camera::CameraComponent;
|
||||||
|
@ -146,7 +146,6 @@ async fn main() {
|
||||||
|
|
||||||
Game::initialize().await
|
Game::initialize().await
|
||||||
.with_world(world)
|
.with_world(world)
|
||||||
.with_system("input", InputEventUpdater::new(), &[])
|
.with_system("jiggle", jiggle_system, &["input"])
|
||||||
.with_system("jiggle", jiggle_system, &[])
|
|
||||||
.run().await;
|
.run().await;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue