lyra-engine/src/game.rs

414 lines
13 KiB
Rust
Executable File

use std::{sync::Arc, cell::RefCell, borrow::Borrow, collections::VecDeque};
use async_std::{task::block_on, sync::Mutex};
//use hecs::World;
use instant::Instant;
use resources::{Resources, Resource};
use tracing::{metadata::LevelFilter, info, debug, warn, error};
use tracing_subscriber::{
layer::{Layer, SubscriberExt},
filter::FilterFn,
util::SubscriberInitExt,
};
use winit::{window::{WindowBuilder, Window}, event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode, DeviceEvent}, event_loop::{EventLoop, ControlFlow}};
use crate::{render::{renderer::{Renderer, BasicRenderer}, render_job::RenderJob}, input_event::InputEvent, ecs::{components::{mesh::MeshComponent, transform::TransformComponent}, SimpleSystem, SystemDispatcher, world::World, resources::{WindowState, Events}}, input::Input};
pub struct Controls<'a> {
pub world: &'a mut World,
pub resources: &'a mut Resources,
}
struct TickCounter {
counter: u32,
last_second: Instant,
changed: bool,
tps: f32,
/// the time (in seconds) that passes between each tick
tick_time: f32
}
impl TickCounter {
fn new() -> Self {
Self {
counter: 0,
last_second: Instant::now(),
tps: 0.0,
changed: false,
tick_time: 0.0,
}
}
/// Returns true if the tps changed
fn tick(&mut self) -> bool {
self.counter += 1;
if self.last_second.elapsed().as_secs() > 0 {
self.tick_time = 1000.0 / self.counter as f32;
self.tps = self.counter as f32;
self.changed = true;
self.counter = 0;
self.last_second = Instant::now();
}
self.changed
}
/// Gets the change in ticks per second
fn get_change(&mut self) -> Option<f32> {
match self.changed {
true => {
self.changed = false;
Some(self.tps)
},
false => None,
}
}
/// Get the time (in seconds) between ticks
fn get_tick_time(&self) -> f32 {
self.tick_time
}
}
struct GameLoop {
window: Arc<Window>,
renderer: Box<dyn Renderer>,
world: Arc<Mutex<World>>,
resources: Arc<Mutex<Resources>>,
/// higher priority systems
engine_sys_dispatcher: SystemDispatcher,
user_sys_dispatcher: SystemDispatcher,
fps_counter: TickCounter,
}
impl GameLoop {
pub async fn new(window: Arc<Window>, world: Arc<Mutex<World>>, resources: Arc<Mutex<Resources>>, user_systems: SystemDispatcher) -> GameLoop {
Self {
window: Arc::clone(&window),
renderer: Box::new(BasicRenderer::create_with_window(window).await),
world,
resources,
engine_sys_dispatcher: SystemDispatcher::new(),
user_sys_dispatcher: user_systems,
fps_counter: TickCounter::new(),
}
}
pub async fn on_resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
self.renderer.on_resize(new_size).await;
}
pub fn run_sync(&mut self, event: Event<()>, control_flow: &mut ControlFlow) {
block_on(self.run_event_loop(event, control_flow))
}
async fn update(&mut self) {
let mut world = self.world.lock().await;
let mut resources = self.resources.lock().await;
world.clear_events();
let mut controls = Controls {
world: &mut world,
resources: &mut resources
};
if let Err(e) = self.engine_sys_dispatcher.execute_mut(&mut controls) {
error!("Error when executing engine ecs systems: '{}'", e);
}
if let Err(e) = self.user_sys_dispatcher.execute_mut(&mut controls) {
error!("Error when executing user ecs systems: '{}'", e);
}
}
async fn input_update(&mut self, event: &InputEvent) -> Option<ControlFlow> {
match event {
InputEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
},
..
} => {
self.on_exit();
Some(ControlFlow::Exit)
},
// TODO: Create system for this? or maybe merge into input system, idk
InputEvent::CursorEntered { .. } => {
let mut world = self.world.lock().await;
let state = match world.query_res_mut::<WindowState>() {
Some(i) => i,
None => {
world.insert_resource(WindowState::new());
// must succeed since it was just added
world.query_res_mut::<WindowState>().unwrap()
}
};
state.is_cursor_inside_window = true;
None
},
InputEvent::CursorLeft { .. } => {
let mut world = self.world.lock().await;
let state = match world.query_res_mut::<WindowState>() {
Some(i) => i,
None => {
world.insert_resource(WindowState::new());
// must succeed since it was just added
world.query_res_mut::<WindowState>().unwrap()
}
};
state.is_cursor_inside_window = false;
None
}
_ => {
//debug!("Got unhandled input event: \"{:?}\"", event);
None
}
}
}
fn render_window() {
todo!()
}
fn render_item() {
todo!()
}
fn on_exit(&mut self) {
info!("On exit!");
}
pub async fn run_event_loop(&mut self, event: Event<'_, ()>, control_flow: &mut ControlFlow) {
*control_flow = ControlFlow::Poll;
match event {
Event::DeviceEvent { device_id, event } => match event {
// convert a MouseMotion event to an InputEvent
DeviceEvent::MouseMotion { delta } => {
let mut world = self.world.lock().await;
// make sure that the mouse is inside the window and the mouse has focus before reporting mouse motion
if let Some(window_state) = world.query_res::<WindowState>() {
if window_state.is_focused && window_state.is_cursor_inside_window {
let event_queue = world.query_or_insert_res::<Events<InputEvent>>
(|| Box::new(Events::<InputEvent>::new()));
let input_event = InputEvent::MouseMotion { device_id, delta, };
event_queue.push_back(input_event);
}
}
},
_ => {}
},
Event::WindowEvent {
ref event,
window_id,
} if window_id == self.window.id() => {
// If try_from failed, that means that the WindowEvent is not an
// input related event.
if let Ok(input_event) = InputEvent::try_from(event) {
// Its possible to receive multiple input events before the update event for
// the InputSystem is called, so we must use a queue for the events.
{
let mut world = self.world.lock().await;
world.trigger_event(input_event.clone());
/* let event_queue = match world.query_res_mut::<Events<InputEvent>>() {
Some(i) => i,
None => {
world.insert_resource(VecDeque::<InputEvent>::new());
// must succeed since it was just added
world.query_res_mut::<Events<InputEvent>>().unwrap()
}
};
event_queue.push_back(input_event.clone()); */
}
if let Some(new_control) = self.input_update(&input_event).await {
*control_flow = new_control;
}
} else {
match event {
WindowEvent::CloseRequested => {
self.on_exit();
*control_flow = ControlFlow::Exit
},
WindowEvent::Resized(physical_size) => {
self.on_resize(*physical_size).await;
},
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
self.on_resize(**new_inner_size).await;
},
WindowEvent::Focused(is_focused) => {
let mut world = self.world.lock().await;
let state = match world.query_res_mut::<WindowState>() {
Some(s) => s,
None => {
world.insert_resource(WindowState::new());
// must succeed since it was just added
world.query_res_mut::<WindowState>().unwrap()
}
};
state.is_focused = *is_focused;
},
_ => {}
}
}
},
Event::RedrawRequested(window_id) if window_id == self.window.id() => {
// Update the world
self.update().await;
/* self.fps_counter.tick();
if let Some(fps) = self.fps_counter.get_change() {
debug!("FPS: {}fps, {:.2}ms/frame", fps, self.fps_counter.get_tick_time());
} */
let mut world = self.world.lock().await;
self.renderer.as_mut().prepare(&mut world).await;
world.clear_updated_resources();
drop(world);
match self.renderer.as_mut().render().await {
Ok(_) => {}
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => self.on_resize(self.renderer.as_ref().surface_size()).await,
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// All other errors (Outdated, Timeout) should be resolved by the next frame
Err(e) => eprintln!("{:?}", e),
}
},
Event::MainEventsCleared => {
self.window.request_redraw();
},
_ => {}
}
}
}
pub struct Game {
world: Option<Arc<Mutex<World>>>,
resources: Option<Arc<Mutex<Resources>>>,
system_dispatcher: Option<SystemDispatcher>
}
impl Default for Game {
fn default() -> Self {
Self {
world: None,
resources: Some(Arc::new(Mutex::new(Resources::new()))),
system_dispatcher: Some(SystemDispatcher::new()),
}
}
}
impl Game {
pub async fn initialize() -> Game {
let filter = FilterFn::new(|metadata| {
metadata.module_path()
.unwrap_or_else(|| metadata.target())
.starts_with("lyra_engine") && (LevelFilter::DEBUG >= metadata.level().to_owned())
});
let layer = tracing_subscriber::fmt::layer();
tracing_subscriber::registry()
.with(layer.with_filter(filter))
.init();
Self::default()
}
pub fn with_world(&mut self, world: World) -> &mut Self {
self.world = Some(Arc::new(Mutex::new(world)));
self
}
pub fn with_world_arc(&mut self, world: Arc<Mutex<World>>) -> &mut Self {
self.world = Some(world);
self
}
pub fn with_system<S>(&mut self, name: &str, system: S, depends: &[&str]) -> &mut Self
where
S: SimpleSystem + 'static
{
let depends: Vec<String> = depends
.iter()
.map(|s| s.to_string())
.collect();
let dispatcher = self.system_dispatcher.as_mut().unwrap();
dispatcher.add_system(name.to_string(), Box::new(system), depends);
self
}
pub async fn with_res<T>(&mut self, r: T) -> &mut Self
where
T: Resource
{
let resources = self.resources.as_mut().unwrap();
let mut resources = resources.lock().await;
resources.insert(r);
drop(resources);
self
}
pub async fn run(&mut self) {
let world = self.world.take().expect("ECS World was never given to Game!");
let resources = self.resources.take().unwrap();
let event_loop = EventLoop::new();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let systems = self.system_dispatcher.take().unwrap();
let mut g_loop = GameLoop::new(Arc::clone(&window), world, resources, systems).await;
event_loop.run(move |event, _, control_flow| {
g_loop.run_sync(event, control_flow);
});
}
}