/*
* 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.
*/
use bevy::{
prelude::*,
ui::{JustifyContent, Size, Style, Val},
};
use super::{despawn_screen, GameState};
/// This plugin will display a splsh logo on startup
pub struct SplashPlugin;
impl Plugin for SplashPlugin {
fn build(&self, app: &mut App) {
app
// Load the splash when we enter the Splash state
.add_system_set(SystemSet::on_enter(GameState::Splash).with_system(splash_setup))
// Run a timer for a few seconds
.add_system_set(SystemSet::on_update(GameState::Splash).with_system(splash_timer))
// Despawn when we exit the Splash state
.add_system_set(
SystemSet::on_exit(GameState::Splash).with_system(despawn_screen::<OnSplashScreen>),
);
}
}
/// Tag component for tagging entities on the splash screen
#[derive(Component)]
struct OnSplashScreen;
/// Timer resource
#[derive(Resource, Deref, DerefMut)]
struct SplashTimer(Timer);
fn splash_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let logo = asset_server.load("branding/logo.png");
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()
},
OnSplashScreen,
))
.with_children(|parent| {
parent.spawn(ImageBundle {
style: Style {
size: Size::new(Val::Px(1000.), Val::Auto),
..Default::default()
},
image: UiImage(logo),
..Default::default()
});
});
// Insert the timer resource
commands.insert_resource(SplashTimer(Timer::from_seconds(2., TimerMode::Once)));
}
fn splash_timer(
mut game_state: ResMut<State<GameState>>,
time: Res<Time>,
mut timer: ResMut<SplashTimer>,
) {
if timer.tick(time.delta()).finished() {
game_state.set(GameState::MainMenu).unwrap()
}
}