loading screen

This commit is contained in:
griffi-gh 2023-02-10 02:36:11 +01:00
parent 1ce3b94cd5
commit 41d7803a64
4 changed files with 107 additions and 17 deletions

View file

@ -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 glium::{Surface, uniform, DrawParameters};
use crate::{ use crate::{
prefabs::ProgressbarShaderPrefab, prefabs::ProgressbarShaderPrefab,
@ -25,10 +25,9 @@ pub fn render_progressbars(
primary: View<PrimaryColor>, primary: View<PrimaryColor>,
secondary: View<SecondaryColor>, secondary: View<SecondaryColor>,
) { ) {
for (_, transform, progress, pri, sec) in (&components, &transforms, &progressbars, &primary, &secondary).iter() { for (eid, (_, transform, progress)) in (&components, &transforms, &progressbars).iter().with_id() {
//TODO do this properly let primary_color = primary.get(eid).copied().unwrap_or_default();
let pri = Some(pri).copied(); let secondary_color = secondary.get(eid).copied().unwrap_or_default();
let sec = Some(sec).copied();
target.0.draw( target.0.draw(
&rect.0, &rect.0,
&rect.1, &rect.1,
@ -37,8 +36,8 @@ pub fn render_progressbars(
transform: transform.0.to_cols_array_2d(), transform: transform.0.to_cols_array_2d(),
ui_view: view.0.to_cols_array_2d(), ui_view: view.0.to_cols_array_2d(),
progress: progress.progress, progress: progress.progress,
color: pri.unwrap_or_default().0.to_array(), color: primary_color.0.to_array(),
bg_color: sec.unwrap_or_default().0.to_array(), bg_color: secondary_color.0.to_array(),
}, },
&DrawParameters::default() &DrawParameters::default()
).unwrap(); ).unwrap();

View file

@ -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::{ use crate::{
world::ChunkStorage, 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::<UniqueView<WindowSize>>().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<WindowSize>,
bar: UniqueView<ProgressbarId>,
mut transforms: ViewMut<Transform2d>
) {
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<ChunkStorage>, world: UniqueView<ChunkStorage>,
mut state: UniqueViewMut<GameState> mut bar: ViewMut<ProgressbarComponent>,
eid: UniqueView<ProgressbarId>,
) {
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<ChunkStorage>,
mut state: UniqueViewMut<NextState>
) { ) {
if world.chunks.is_empty() { if world.chunks.is_empty() {
return return
@ -19,13 +64,29 @@ pub fn switch_to_ingame_if_loaded(
if world.chunks.iter().all(|(_, chunk)| { if world.chunks.iter().all(|(_, chunk)| {
chunk.desired_state.matches(chunk.current_state) 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::<UniqueView<NextState>>().unwrap();
if let Some(state) = next.0 {
if state != GameState::LoadingWorld {
let progress_bar = storages.borrow::<UniqueView<ProgressbarId>>().unwrap().0;
storages.delete_entity(progress_bar);
storages.remove_unique::<ProgressbarId>().unwrap();
}
} }
} }
pub fn update_loading_screen() -> Workload { pub fn update_loading_screen() -> Workload {
( (
insert_progressbar, spawn_loading_screen.into_workload().run_if_missing_unique::<ProgressbarId>(),
switch_to_ingame_if_loaded 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() ).into_workload()
} }

View file

@ -66,7 +66,7 @@ use rendering::{
primitives::init_primitives, primitives::init_primitives,
selection_box::render_selection_box, selection_box::render_selection_box,
world::draw_world, 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 block_placement::block_placement_system;
use delta_time::{DeltaTime, init_delta_time}; use delta_time::{DeltaTime, init_delta_time};
@ -80,6 +80,7 @@ use loading_screen::update_loading_screen;
fn startup() -> Workload { fn startup() -> Workload {
( (
initial_resize_event, initial_resize_event,
init_window_size,
load_settings, load_settings,
load_prefabs, load_prefabs,
init_primitives, init_primitives,
@ -97,6 +98,7 @@ fn startup() -> Workload {
} }
fn update() -> Workload { fn update() -> Workload {
( (
update_window_size,
update_cursor_lock_state, update_cursor_lock_state,
process_inputs, process_inputs,
exit_on_esc, exit_on_esc,

View file

@ -1,4 +1,4 @@
use shipyard::{Unique, NonSendSync, UniqueView, UniqueViewMut, AllStoragesView}; use shipyard::{Unique, NonSendSync, UniqueView, UniqueViewMut, View, IntoIter, AllStoragesView};
use glium::{ use glium::{
Display, Surface, Display, Surface,
Version, Api, Version, Api,
@ -9,6 +9,7 @@ use glium::{
}, },
}; };
use glam::{Vec3, UVec2}; use glam::{Vec3, UVec2};
use crate::events::WindowResizedEvent;
pub mod primitives; pub mod primitives;
pub mod world; pub mod world;
@ -20,6 +21,9 @@ pub struct RenderTarget(pub glium::Frame);
#[derive(Unique)] #[derive(Unique)]
pub struct BackgroundColor(pub Vec3); pub struct BackgroundColor(pub Vec3);
#[derive(Unique, Clone, Copy)]
pub struct WindowSize(pub UVec2);
#[derive(Unique)] #[derive(Unique)]
pub struct Renderer { pub struct Renderer {
pub display: Display 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.); 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::<View<WindowResizedEvent>>().unwrap().iter().next().unwrap().0;
storages.add_unique(WindowSize(size))
}
pub fn update_window_size(
mut win_size: UniqueViewMut<WindowSize>,
resize: View<WindowResizedEvent>,
) {
if let Some(resize) = resize.iter().next() {
win_size.0 = resize.0;
}
}
pub fn if_resized (
resize: View<WindowResizedEvent>,
) -> bool {
resize.len() > 0
}