beautify code

This commit is contained in:
griffi-gh 2024-02-12 23:50:08 +01:00
parent 01f642fcbb
commit 2ba011a89b

View file

@ -1,310 +1,314 @@
#![allow(clippy::too_many_arguments)] // allowed because systems often need a lot of arguments #![allow(clippy::too_many_arguments)] // allowed because systems often need a lot of arguments
use shipyard::{ use shipyard::{
World, Workload, IntoWorkload, World, Workload, IntoWorkload,
UniqueView, UniqueViewMut, UniqueView, UniqueViewMut,
NonSendSync, WorkloadModificator, NonSendSync, WorkloadModificator,
SystemModificator SystemModificator
}; };
use winit::{ use winit::{
event_loop::{EventLoop, ControlFlow}, event_loop::{EventLoop, ControlFlow},
event::{Event, WindowEvent} event::{Event, WindowEvent}
}; };
use glam::vec3; use glam::vec3;
use std::time::Instant; use std::time::Instant;
pub use kubi_shared::transform; pub use kubi_shared::transform;
pub(crate) mod rendering; pub(crate) mod rendering;
pub(crate) mod world; pub(crate) mod world;
pub(crate) mod player; pub(crate) mod player;
pub(crate) mod prefabs; pub(crate) mod prefabs;
pub(crate) mod settings; pub(crate) mod settings;
pub(crate) mod camera; pub(crate) mod camera;
pub(crate) mod events; pub(crate) mod events;
pub(crate) mod input; pub(crate) mod input;
pub(crate) mod fly_controller; pub(crate) mod fly_controller;
pub(crate) mod block_placement; pub(crate) mod block_placement;
pub(crate) mod delta_time; pub(crate) mod delta_time;
pub(crate) mod cursor_lock; pub(crate) mod cursor_lock;
pub(crate) mod control_flow; pub(crate) mod control_flow;
pub(crate) mod state; pub(crate) mod state;
pub(crate) mod kubi_ui_integration; pub(crate) mod kubi_ui_integration;
pub(crate) mod networking; pub(crate) mod networking;
pub(crate) mod init; pub(crate) mod init;
pub(crate) mod color; pub(crate) mod color;
pub(crate) mod loading_screen; pub(crate) mod loading_screen;
pub(crate) mod connecting_screen; pub(crate) mod connecting_screen;
pub(crate) mod fixed_timestamp; pub(crate) mod fixed_timestamp;
pub(crate) mod filesystem; pub(crate) mod filesystem;
use world::{ use world::{
init_game_world, init_game_world,
loading::update_loaded_world_around_player, loading::update_loaded_world_around_player,
raycast::update_raycasts, raycast::update_raycasts,
queue::apply_queued_blocks, queue::apply_queued_blocks,
tasks::ChunkTaskManager, tasks::ChunkTaskManager,
}; };
use player::{spawn_player, MainPlayer}; use player::{spawn_player, MainPlayer};
use prefabs::load_prefabs; use prefabs::load_prefabs;
use settings::{load_settings, GameSettings}; use settings::{load_settings, GameSettings};
use camera::compute_cameras; use camera::compute_cameras;
use events::{ use events::{
clear_events, clear_events,
process_glutin_events, process_glutin_events,
initial_resize_event, initial_resize_event,
player_actions::generate_move_events, player_actions::generate_move_events,
}; };
use input::{init_input, process_inputs}; use input::{init_input, process_inputs};
use fly_controller::update_controllers; use fly_controller::update_controllers;
use rendering::{ use rendering::{
Renderer, Renderer,
RenderTarget, RenderTarget,
BackgroundColor, BackgroundColor,
clear_background, clear_background,
init_window_size, init_window_size,
update_window_size, update_window_size,
primitives::init_primitives, primitives::init_primitives,
world::{draw_world, draw_current_chunk_border}, world::{draw_world, draw_current_chunk_border},
selection_box::render_selection_box, selection_box::render_selection_box,
entities::render_entities, entities::render_entities,
}; };
use block_placement::update_block_placement; use block_placement::update_block_placement;
use delta_time::{DeltaTime, init_delta_time}; use delta_time::{DeltaTime, init_delta_time};
use cursor_lock::{insert_lock_state, update_cursor_lock_state, lock_cursor_now}; use cursor_lock::{insert_lock_state, update_cursor_lock_state, lock_cursor_now};
use control_flow::{exit_on_esc, insert_control_flow_unique, RequestExit}; use control_flow::{exit_on_esc, insert_control_flow_unique, RequestExit};
use state::{is_ingame, is_ingame_or_loading, is_loading, init_state, update_state, is_connecting}; use state::{is_ingame, is_ingame_or_loading, is_loading, init_state, update_state, is_connecting};
use networking::{update_networking, update_networking_late, is_multiplayer, disconnect_on_exit, is_singleplayer}; use networking::{update_networking, update_networking_late, is_multiplayer, disconnect_on_exit, is_singleplayer};
use init::initialize_from_args; use init::initialize_from_args;
use kubi_ui_integration::{kubi_ui_init, kubi_ui_begin, kubi_ui_end, kubi_ui_draw}; use kubi_ui_integration::{kubi_ui_init, kubi_ui_begin, kubi_ui_end, kubi_ui_draw};
use loading_screen::update_loading_screen; use loading_screen::update_loading_screen;
use connecting_screen::switch_to_loading_if_connected; use connecting_screen::switch_to_loading_if_connected;
use fixed_timestamp::init_fixed_timestamp_storage; use fixed_timestamp::init_fixed_timestamp_storage;
use filesystem::AssetManager; use filesystem::AssetManager;
/// stuff required to init the renderer and other basic systems /// stuff required to init the renderer and other basic systems
fn pre_startup() -> Workload { fn pre_startup() -> Workload {
( (
load_settings, load_settings,
).into_sequential_workload() ).into_sequential_workload()
} }
fn startup() -> Workload { fn startup() -> Workload {
( (
init_fixed_timestamp_storage, init_fixed_timestamp_storage,
initial_resize_event, initial_resize_event,
init_window_size, init_window_size,
kubi_ui_init, kubi_ui_init,
load_prefabs, load_prefabs,
init_primitives, init_primitives,
insert_lock_state, insert_lock_state,
init_state, init_state,
initialize_from_args, initialize_from_args,
lock_cursor_now, lock_cursor_now,
init_input, init_input,
insert_control_flow_unique, insert_control_flow_unique,
init_delta_time, init_delta_time,
).into_sequential_workload() ).into_sequential_workload()
} }
fn update() -> Workload { fn update() -> Workload {
( (
update_window_size, update_window_size,
update_cursor_lock_state, update_cursor_lock_state,
process_inputs, process_inputs,
kubi_ui_begin, kubi_ui_begin,
( (
init_game_world.run_if_missing_unique::<ChunkTaskManager>(), init_game_world.run_if_missing_unique::<ChunkTaskManager>(),
( (
spawn_player.run_if_storage_empty::<MainPlayer>(), spawn_player.run_if_storage_empty::<MainPlayer>(),
).into_sequential_workload().run_if(is_singleplayer), ).into_sequential_workload().run_if(is_singleplayer),
).into_sequential_workload().run_if(is_ingame_or_loading), ).into_sequential_workload().run_if(is_ingame_or_loading),
update_networking().run_if(is_multiplayer), update_networking().run_if(is_multiplayer),
( (
switch_to_loading_if_connected switch_to_loading_if_connected
).into_sequential_workload().run_if(is_connecting), ).into_sequential_workload().run_if(is_connecting),
( (
update_loading_screen, update_loading_screen,
).into_sequential_workload().run_if(is_loading), ).into_sequential_workload().run_if(is_loading),
( (
update_loaded_world_around_player, update_loaded_world_around_player,
).into_sequential_workload().run_if(is_ingame_or_loading), ).into_sequential_workload().run_if(is_ingame_or_loading),
( (
update_controllers, update_controllers,
generate_move_events, generate_move_events,
update_raycasts, update_raycasts,
update_block_placement, update_block_placement,
apply_queued_blocks, apply_queued_blocks,
).into_sequential_workload().run_if(is_ingame), ).into_sequential_workload().run_if(is_ingame),
update_networking_late.run_if(is_multiplayer), update_networking_late.run_if(is_multiplayer),
compute_cameras, compute_cameras,
kubi_ui_end, kubi_ui_end,
update_state, update_state,
exit_on_esc, exit_on_esc,
disconnect_on_exit.run_if(is_multiplayer), disconnect_on_exit.run_if(is_multiplayer),
).into_sequential_workload() ).into_sequential_workload()
} }
fn render() -> Workload { fn render() -> Workload {
( (
clear_background, clear_background,
( (
draw_world, draw_world,
draw_current_chunk_border, draw_current_chunk_border,
render_selection_box, render_selection_box,
render_entities, render_entities,
).into_sequential_workload().run_if(is_ingame), ).into_sequential_workload().run_if(is_ingame),
kubi_ui_draw, kubi_ui_draw,
).into_sequential_workload() ).into_sequential_workload()
} }
fn after_frame_end() -> Workload { fn after_frame_end() -> Workload {
( (
clear_events, clear_events,
).into_sequential_workload() ).into_sequential_workload()
} }
#[cfg(all(windows, not(debug_assertions)))] #[cfg(all(windows, not(debug_assertions)))]
fn attach_console() { fn attach_console() {
use winapi::um::wincon::{AttachConsole, ATTACH_PARENT_PROCESS}; use winapi::um::wincon::{AttachConsole, ATTACH_PARENT_PROCESS};
unsafe { AttachConsole(ATTACH_PARENT_PROCESS); } unsafe { AttachConsole(ATTACH_PARENT_PROCESS); }
} }
#[no_mangle] #[no_mangle]
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
pub fn android_main(app: android_activity::AndroidApp) { pub fn android_main(app: android_activity::AndroidApp) {
use android_activity::WindowManagerFlags; use android_activity::WindowManagerFlags;
app.set_window_flags(WindowManagerFlags::FULLSCREEN, WindowManagerFlags::empty()); app.set_window_flags(WindowManagerFlags::FULLSCREEN, WindowManagerFlags::empty());
kubi_main(app) kubi_main(app)
} }
#[no_mangle] #[no_mangle]
pub fn kubi_main(#[cfg(target_os = "android")] app: android_activity::AndroidApp) { pub fn kubi_main(
//Attach console on release builds on windows #[cfg(target_os = "android")]
#[cfg(all(windows, not(debug_assertions)))] attach_console(); app: android_activity::AndroidApp
) {
//Print version //Attach console on release builds on windows
println!("{:─^54}", format!("[ ▄▀ Kubi client v. {} ]", env!("CARGO_PKG_VERSION"))); #[cfg(all(windows, not(debug_assertions)))]
attach_console();
//Init env_logger
kubi_logging::init(); //Print version
println!("{:─^54}", format!("[ ▄▀ Kubi client v. {} ]", env!("CARGO_PKG_VERSION")));
//Create a shipyard world
let mut world = World::new(); //Init env_logger
kubi_logging::init();
//Init assman
world.add_unique(AssetManager { //Create a shipyard world
#[cfg(target_os = "android")] let mut world = World::new();
app: app.clone()
}); //Init assman
world.add_unique(AssetManager {
//Register workloads #[cfg(target_os = "android")]
world.add_workload(pre_startup); app: app.clone()
world.add_workload(startup); });
world.add_workload(update);
world.add_workload(render); //Register workloads
world.add_workload(after_frame_end); world.add_workload(pre_startup);
world.add_workload(startup);
//Save _visualizer.json world.add_workload(update);
#[cfg(feature = "generate_visualizer_data")] world.add_workload(render);
std::fs::write( world.add_workload(after_frame_end);
"_visualizer.json",
serde_json::to_string(&world.workloads_info()).unwrap(), //Save _visualizer.json
).unwrap(); #[cfg(feature = "generate_visualizer_data")]
std::fs::write(
//Run pre-startup procedure "_visualizer.json",
world.run_workload(pre_startup).unwrap(); serde_json::to_string(&world.workloads_info()).unwrap(),
).unwrap();
//Create event loop
let event_loop ={ //Run pre-startup procedure
#[cfg(not(target_os = "android"))] { EventLoop::new().unwrap() } world.run_workload(pre_startup).unwrap();
#[cfg(target_os = "android")] {
use winit::{ //Create event loop
platform::android::EventLoopBuilderExtAndroid, let event_loop ={
event_loop::EventLoopBuilder #[cfg(not(target_os = "android"))] { EventLoop::new().unwrap() }
}; #[cfg(target_os = "android")] {
EventLoopBuilder::new().with_android_app(app).build().unwrap() use winit::{
} platform::android::EventLoopBuilderExtAndroid,
}; event_loop::EventLoopBuilder
};
//Run the event loop EventLoopBuilder::new().with_android_app(app).build().unwrap()
let mut last_update = Instant::now(); }
let mut ready = false; };
event_loop.run(move |event, window_target| {
//Wait for the window to become active (required for android) //Run the event loop
if !ready { let mut last_update = Instant::now();
if Event::Resumed != event { let mut ready = false;
window_target.set_control_flow(ControlFlow::Wait); event_loop.run(move |event, window_target| {
return //Wait for the window to become active (required for android)
} if !ready {
if Event::Resumed != event {
//Initialize renderer window_target.set_control_flow(ControlFlow::Wait);
{ return
let settings = world.borrow::<UniqueView<GameSettings>>().unwrap(); }
world.add_unique_non_send_sync(Renderer::init(window_target, &settings));
} //Initialize renderer
world.add_unique(BackgroundColor(vec3(0.5, 0.5, 1.))); {
let settings = world.borrow::<UniqueView<GameSettings>>().unwrap();
//Run startup systems world.add_unique_non_send_sync(Renderer::init(window_target, &settings));
world.run_workload(startup).unwrap(); }
world.add_unique(BackgroundColor(vec3(0.5, 0.5, 1.)));
ready = true;
} //Run startup systems
world.run_workload(startup).unwrap();
window_target.set_control_flow(ControlFlow::Poll);
ready = true;
process_glutin_events(&mut world, &event); }
#[allow(clippy::collapsible_match, clippy::single_match)] window_target.set_control_flow(ControlFlow::Poll);
match event {
#[cfg(target_os = "android")] process_glutin_events(&mut world, &event);
Event::Suspended => {
window_target.exit(); #[allow(clippy::collapsible_match, clippy::single_match)]
} match event {
#[cfg(target_os = "android")]
Event::WindowEvent { event, .. } => match event { Event::Suspended => {
WindowEvent::CloseRequested => { window_target.exit();
log::info!("exit requested"); }
window_target.exit();
}, Event::WindowEvent { event, .. } => match event {
_ => (), WindowEvent::CloseRequested => {
}, log::info!("exit requested");
window_target.exit();
Event::AboutToWait => { },
//Update delta time (maybe move this into a system?) _ => (),
{ },
let mut dt_view = world.borrow::<UniqueViewMut<DeltaTime>>().unwrap();
let now = Instant::now(); Event::AboutToWait => {
dt_view.0 = now - last_update; //Update delta time (maybe move this into a system?)
last_update = now; {
} let mut dt_view = world.borrow::<UniqueViewMut<DeltaTime>>().unwrap();
let now = Instant::now();
//Run update workflows dt_view.0 = now - last_update;
world.run_workload(update).unwrap(); last_update = now;
}
//Start rendering (maybe use custom views for this?)
let target = { //Run update workflows
let renderer = world.borrow::<NonSendSync<UniqueView<Renderer>>>().unwrap(); world.run_workload(update).unwrap();
renderer.display.draw()
}; //Start rendering (maybe use custom views for this?)
world.add_unique_non_send_sync(RenderTarget(target)); let target = {
let renderer = world.borrow::<NonSendSync<UniqueView<Renderer>>>().unwrap();
//Run render workflow renderer.display.draw()
world.run_workload(render).unwrap(); };
world.add_unique_non_send_sync(RenderTarget(target));
//Finish rendering
let target = world.remove_unique::<RenderTarget>().unwrap(); //Run render workflow
target.0.finish().unwrap(); world.run_workload(render).unwrap();
//After frame end //Finish rendering
world.run_workload(after_frame_end).unwrap(); let target = world.remove_unique::<RenderTarget>().unwrap();
target.0.finish().unwrap();
//Process control flow changes
if world.borrow::<UniqueView<RequestExit>>().unwrap().0 { //After frame end
window_target.exit(); world.run_workload(after_frame_end).unwrap();
}
}, //Process control flow changes
_ => (), if world.borrow::<UniqueView<RequestExit>>().unwrap().0 {
}; window_target.exit();
}).unwrap(); }
} },
_ => (),
};
}).unwrap();
}