/*
* This file is part of sdbclient
* Copyright (C) 2022 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
// FIXME: I hate this. Need to rework whole loading system at some point with a real loading state and screen.
use bevy::{
prelude::*,
ui::{JustifyContent, Size, Style, Val},
};
use crate::{
constants::TEXT_COLOR,
despawn_screen,
plugins::config::{LoadEvent, LoadEventValue},
GameState,
};
/// This plugin is used to load all configs during startup
pub struct LoadingPlugin;
impl Plugin for LoadingPlugin {
fn build(&self, app: &mut App) {
app
// Load the loading screen when we enter the Loading state
.add_system_set(SystemSet::on_enter(GameState::Loading).with_system(loading_setup))
// Despawn when we exit the Loading state
.add_system_set(
SystemSet::on_exit(GameState::Loading)
.with_system(despawn_screen::<OnLoadingScreen>),
);
}
}
/// Tag component for tagging entities on the loading screen
#[derive(Component)]
struct OnLoadingScreen;
fn loading_setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut game_state: ResMut<State<GameState>>,
mut load_event_writer: EventWriter<LoadEvent>,
) {
let font = asset_server.load("fonts/FiraMono-Regular.ttf");
commands
.spawn((
NodeBundle {
style: Style {
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
size: Size::new(Val::Percent(100.), Val::Percent(100.)),
..Default::default()
},
..Default::default()
},
OnLoadingScreen,
))
.with_children(|parent| {
parent.spawn(TextBundle {
text: Text::from_section(
"...",
TextStyle {
font: font.clone(),
font_size: 80.0,
color: TEXT_COLOR,
},
),
style: Style {
size: Size::new(Val::Px(1000.), Val::Auto),
align_self: AlignSelf::Center,
..Default::default()
},
..Default::default()
});
});
// Queue up load events for all configs
load_event_writer.send(LoadEvent {
value: LoadEventValue::Settings,
});
load_event_writer.send(LoadEvent {
value: LoadEventValue::User,
});
load_event_writer.send(LoadEvent {
value: LoadEventValue::Dev,
});
game_state.overwrite_set(GameState::MainMenu).unwrap();
}