From dbb92232bcede3436959dcc066dc777680345bbc Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Fri, 10 Feb 2023 02:36:11 +0100 Subject: [PATCH] loading screen --- kubi/src/gui/progressbar.rs | 13 +++---- kubi/src/loading_screen.rs | 77 +++++++++++++++++++++++++++++++++---- kubi/src/main.rs | 4 +- kubi/src/rendering.rs | 30 ++++++++++++++- 4 files changed, 107 insertions(+), 17 deletions(-) diff --git a/kubi/src/gui/progressbar.rs b/kubi/src/gui/progressbar.rs index 3e848c9..9a945c7 100644 --- a/kubi/src/gui/progressbar.rs +++ b/kubi/src/gui/progressbar.rs @@ -1,4 +1,4 @@ -use shipyard::{UniqueView, UniqueViewMut, NonSendSync, View, Component, IntoIter}; +use shipyard::{UniqueView, UniqueViewMut, NonSendSync, View, Component, IntoIter, IntoWithId, Get}; use glium::{Surface, uniform, DrawParameters}; use crate::{ prefabs::ProgressbarShaderPrefab, @@ -25,10 +25,9 @@ pub fn render_progressbars( primary: View, secondary: View, ) { - for (_, transform, progress, pri, sec) in (&components, &transforms, &progressbars, &primary, &secondary).iter() { - //TODO do this properly - let pri = Some(pri).copied(); - let sec = Some(sec).copied(); + for (eid, (_, transform, progress)) in (&components, &transforms, &progressbars).iter().with_id() { + let primary_color = primary.get(eid).copied().unwrap_or_default(); + let secondary_color = secondary.get(eid).copied().unwrap_or_default(); target.0.draw( &rect.0, &rect.1, @@ -37,8 +36,8 @@ pub fn render_progressbars( transform: transform.0.to_cols_array_2d(), ui_view: view.0.to_cols_array_2d(), progress: progress.progress, - color: pri.unwrap_or_default().0.to_array(), - bg_color: sec.unwrap_or_default().0.to_array(), + color: primary_color.0.to_array(), + bg_color: secondary_color.0.to_array(), }, &DrawParameters::default() ).unwrap(); diff --git a/kubi/src/loading_screen.rs b/kubi/src/loading_screen.rs index 6ddb4e0..f003174 100644 --- a/kubi/src/loading_screen.rs +++ b/kubi/src/loading_screen.rs @@ -1,17 +1,62 @@ -use shipyard::{UniqueView, UniqueViewMut, Workload, IntoWorkload}; +use shipyard::{UniqueView, UniqueViewMut, Workload, IntoWorkload, WorkloadModificator, EntityId, Unique, AllStoragesViewMut, ViewMut, View, IntoIter, Get}; +use glam::{Mat3, vec2}; use crate::{ world::ChunkStorage, - state::GameState + state::{GameState, NextState}, + transform::Transform2d, + gui::{ + GuiComponent, + progressbar::ProgressbarComponent + }, rendering::{WindowSize, if_resized}, }; -pub fn insert_progressbar() { +#[derive(Unique, Clone, Copy)] +struct ProgressbarId(EntityId); +fn spawn_loading_screen( + mut storages: AllStoragesViewMut, +) { + let size = *storages.borrow::>().unwrap(); + let entity = storages.add_entity(( + GuiComponent, + Transform2d(Mat3::from_scale_angle_translation( + vec2(size.0.x as f32, 16.), + 0., + vec2(0., 0.) + )), + ProgressbarComponent { + progress: 0.33 + }, + )); + storages.add_unique(ProgressbarId(entity)); } +fn resize_progress_bar( + size: UniqueView, + bar: UniqueView, + mut transforms: ViewMut +) { + let mut trans = (&mut transforms).get(bar.0).unwrap(); + trans.0.x_axis.x = size.0.x as f32; +} -pub fn switch_to_ingame_if_loaded( +fn update_progress_bar_progress ( world: UniqueView, - mut state: UniqueViewMut + mut bar: ViewMut, + eid: UniqueView, +) { + let bar = (&mut bar).get(eid.0).unwrap(); + let loaded = world.chunks.iter().fold(0, |acc, (&_, chunk)| { + acc + chunk.desired_state.matches(chunk.current_state) as usize + }); + let total = world.chunks.len(); + let progress = loaded as f32 / total as f32; + bar.progress = progress; +} + +fn switch_to_ingame_if_loaded( + world: UniqueView, + mut state: UniqueViewMut ) { if world.chunks.is_empty() { return @@ -19,13 +64,29 @@ pub fn switch_to_ingame_if_loaded( if world.chunks.iter().all(|(_, chunk)| { chunk.desired_state.matches(chunk.current_state) }) { - *state = GameState::InGame; + state.0 = Some(GameState::InGame); + } +} + +fn despawn_loading_screen_if_switching_state( + mut storages: AllStoragesViewMut, +) { + let next = *storages.borrow::>().unwrap(); + if let Some(state) = next.0 { + if state != GameState::LoadingWorld { + let progress_bar = storages.borrow::>().unwrap().0; + storages.delete_entity(progress_bar); + storages.remove_unique::().unwrap(); + } } } pub fn update_loading_screen() -> Workload { ( - insert_progressbar, - switch_to_ingame_if_loaded + spawn_loading_screen.into_workload().run_if_missing_unique::(), + resize_progress_bar.into_workload().run_if(if_resized), + update_progress_bar_progress, + switch_to_ingame_if_loaded, + despawn_loading_screen_if_switching_state, ).into_workload() } diff --git a/kubi/src/main.rs b/kubi/src/main.rs index d29b4c7..c743c24 100644 --- a/kubi/src/main.rs +++ b/kubi/src/main.rs @@ -66,7 +66,7 @@ use rendering::{ primitives::init_primitives, selection_box::render_selection_box, world::draw_world, - world::draw_current_chunk_border, + world::draw_current_chunk_border, init_window_size, update_window_size, }; use block_placement::block_placement_system; use delta_time::{DeltaTime, init_delta_time}; @@ -80,6 +80,7 @@ use loading_screen::update_loading_screen; fn startup() -> Workload { ( initial_resize_event, + init_window_size, load_settings, load_prefabs, init_primitives, @@ -97,6 +98,7 @@ fn startup() -> Workload { } fn update() -> Workload { ( + update_window_size, update_cursor_lock_state, process_inputs, exit_on_esc, diff --git a/kubi/src/rendering.rs b/kubi/src/rendering.rs index a288b3a..f0b621a 100644 --- a/kubi/src/rendering.rs +++ b/kubi/src/rendering.rs @@ -1,4 +1,4 @@ -use shipyard::{Unique, NonSendSync, UniqueView, UniqueViewMut, AllStoragesView}; +use shipyard::{Unique, NonSendSync, UniqueView, UniqueViewMut, View, IntoIter, AllStoragesView}; use glium::{ Display, Surface, Version, Api, @@ -9,6 +9,7 @@ use glium::{ }, }; use glam::{Vec3, UVec2}; +use crate::events::WindowResizedEvent; pub mod primitives; pub mod world; @@ -20,6 +21,9 @@ pub struct RenderTarget(pub glium::Frame); #[derive(Unique)] pub struct BackgroundColor(pub Vec3); +#[derive(Unique, Clone, Copy)] +pub struct WindowSize(pub UVec2); + #[derive(Unique)] pub struct Renderer { pub display: Display @@ -49,3 +53,27 @@ pub fn clear_background( ) { target.0.clear_color_srgb_and_depth((color.0.x, color.0.y, color.0.z, 1.), 1.); } + +//not sure if this belongs here + +pub fn init_window_size( + storages: AllStoragesView, +) { + let size = storages.borrow::>().unwrap().iter().next().unwrap().0; + storages.add_unique(WindowSize(size)) +} + +pub fn update_window_size( + mut win_size: UniqueViewMut, + resize: View, +) { + if let Some(resize) = resize.iter().next() { + win_size.0 = resize.0; + } +} + +pub fn if_resized ( + resize: View, +) -> bool { + resize.len() > 0 +}