kubi/src/main.rs

157 lines
3.7 KiB
Rust
Raw Normal View History

2023-01-19 17:45:46 -06:00
use shipyard::{
World, Workload, IntoWorkload,
UniqueView, UniqueViewMut,
NonSendSync, Unique
};
2023-01-19 16:28:07 -06:00
use glium::{
glutin::{
event_loop::{EventLoop, ControlFlow},
event::{Event, WindowEvent}
}
};
2023-01-19 17:45:46 -06:00
use glam::vec3;
use std::time::{Instant, Duration};
2023-01-19 14:27:34 -06:00
2023-01-19 14:58:49 -06:00
mod logging;
2023-01-27 21:20:51 -06:00
2023-01-19 17:45:46 -06:00
pub(crate) mod rendering;
pub(crate) mod world;
2023-01-21 20:24:28 -06:00
pub(crate) mod player;
2023-01-19 19:54:41 -06:00
pub(crate) mod prefabs;
2023-01-20 13:40:08 -06:00
pub(crate) mod transform;
2023-01-20 19:39:55 -06:00
pub(crate) mod settings;
2023-01-21 20:24:28 -06:00
pub(crate) mod camera;
2023-01-24 20:01:44 -06:00
pub(crate) mod events;
2023-01-24 11:58:56 -06:00
pub(crate) mod input;
pub(crate) mod fly_controller;
2023-01-19 17:45:46 -06:00
2023-01-27 21:20:51 -06:00
use rendering::{
Renderer,
RenderTarget,
BackgroundColor,
clear_background
};
use world::{
init_game_world,
loading::update_loaded_world_around_player,
raycast::update_player_raycast
};
2023-01-20 14:55:02 -06:00
use player::spawn_player;
2023-01-19 19:54:41 -06:00
use prefabs::load_prefabs;
2023-01-20 20:40:23 -06:00
use settings::GameSettings;
2023-01-21 20:24:28 -06:00
use camera::compute_cameras;
2023-01-24 20:01:44 -06:00
use events::{clear_events, process_glutin_events};
use input::{init_input, process_inputs};
2023-01-24 20:36:24 -06:00
use fly_controller::update_controllers;
2023-01-27 21:20:51 -06:00
use rendering::{
selection_box::render_selection_box,
world::draw_world,
};
2023-01-19 16:28:07 -06:00
2023-01-19 17:45:46 -06:00
#[derive(Unique)]
pub(crate) struct DeltaTime(Duration);
2023-01-19 16:28:07 -06:00
2023-01-20 14:55:02 -06:00
fn startup() -> Workload {
2023-01-19 17:45:46 -06:00
(
2023-01-24 20:01:44 -06:00
load_prefabs,
init_input,
init_game_world,
2023-01-20 14:55:02 -06:00
spawn_player,
2023-01-19 17:45:46 -06:00
).into_workload()
2023-01-19 16:28:07 -06:00
}
fn update() -> Workload {
2023-01-20 14:55:02 -06:00
(
2023-01-24 20:01:44 -06:00
process_inputs,
2023-01-24 20:36:24 -06:00
update_controllers,
2023-01-21 20:24:28 -06:00
update_loaded_world_around_player,
2023-01-27 21:20:51 -06:00
update_player_raycast,
2023-01-24 20:36:24 -06:00
compute_cameras
2023-01-20 14:55:02 -06:00
).into_workload()
}
fn render() -> Workload {
(
clear_background,
2023-01-22 10:01:54 -06:00
draw_world,
2023-01-28 13:56:17 -06:00
render_selection_box,
2023-01-21 20:24:28 -06:00
).into_sequential_workload()
2023-01-19 16:28:07 -06:00
}
2023-01-24 20:36:24 -06:00
fn after_frame_end() -> Workload {
(
clear_events,
).into_workload()
}
2023-01-19 14:58:49 -06:00
2023-01-14 13:08:14 -06:00
fn main() {
2023-01-19 14:58:49 -06:00
logging::init();
2023-01-19 16:28:07 -06:00
//Create event loop
2023-01-19 14:58:49 -06:00
let event_loop = EventLoop::new();
2023-01-19 16:28:07 -06:00
//Create a shipyard world
2023-01-24 20:01:44 -06:00
let mut world = World::new();
2023-01-19 16:28:07 -06:00
2023-01-20 10:42:04 -06:00
//Add systems and uniques, Init and load things
2023-01-21 18:51:23 -06:00
world.add_unique_non_send_sync(Renderer::init(&event_loop));
2023-01-19 19:54:41 -06:00
world.add_unique(BackgroundColor(vec3(0.5, 0.5, 1.)));
world.add_unique(DeltaTime(Duration::default()));
2023-01-20 20:40:23 -06:00
world.add_unique(GameSettings::default());
2023-01-20 14:55:02 -06:00
//Register workloads
world.add_workload(startup);
2023-01-19 16:28:07 -06:00
world.add_workload(update);
world.add_workload(render);
2023-01-24 20:36:24 -06:00
world.add_workload(after_frame_end);
2023-01-19 16:28:07 -06:00
2023-01-20 14:55:02 -06:00
//Run startup systems
world.run_workload(startup).unwrap();
2023-01-19 16:28:07 -06:00
//Run the event loop
2023-01-19 17:45:46 -06:00
let mut last_update = Instant::now();
2023-01-19 14:58:49 -06:00
event_loop.run(move |event, _, control_flow| {
2023-01-19 16:28:07 -06:00
*control_flow = ControlFlow::Poll;
2023-01-24 20:01:44 -06:00
process_glutin_events(&mut world, &event);
2023-01-19 16:28:07 -06:00
match event {
Event::WindowEvent { event, .. } => match event {
2023-01-22 20:41:04 -06:00
WindowEvent::Resized(_size) => {
2023-01-20 09:28:34 -06:00
// todo ...
}
2023-01-19 16:28:07 -06:00
WindowEvent::CloseRequested => {
2023-01-19 17:45:46 -06:00
log::info!("exit requested");
2023-01-19 16:28:07 -06:00
*control_flow = ControlFlow::Exit;
},
_ => (),
},
Event::MainEventsCleared => {
2023-01-19 17:45:46 -06:00
//Update delta time (maybe move this into a system?)
{
let mut dt_view = world.borrow::<UniqueViewMut<DeltaTime>>().unwrap();
let now = Instant::now();
dt_view.0 = now - last_update;
last_update = now;
}
//Run update workflow
2023-01-19 16:28:07 -06:00
world.run_workload(update).unwrap();
2023-01-19 17:45:46 -06:00
2023-01-19 20:08:09 -06:00
//Start rendering (maybe use custom views for this?)
2023-01-22 20:19:26 -06:00
let target = {
2023-01-21 18:51:23 -06:00
let renderer = world.borrow::<NonSendSync<UniqueView<Renderer>>>().unwrap();
2023-01-19 16:28:07 -06:00
renderer.display.draw()
};
world.add_unique_non_send_sync(RenderTarget(target));
2023-01-19 17:45:46 -06:00
//Run render workflow
2023-01-19 16:28:07 -06:00
world.run_workload(render).unwrap();
2023-01-19 17:45:46 -06:00
//Finish rendering
2023-01-19 16:28:07 -06:00
let target = world.remove_unique::<RenderTarget>().unwrap();
target.0.finish().unwrap();
2023-01-24 20:36:24 -06:00
//FrameEnd
world.run_workload(after_frame_end).unwrap();
2023-01-19 16:28:07 -06:00
},
_ => (),
};
2023-01-19 14:58:49 -06:00
});
2023-01-14 13:08:14 -06:00
}