DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: 24224ca565341517f1182485a1c543072f8d505d deck-builder/sdbclient/src/plugins/splash/mod.rs -rw-r--r-- 2.3 KiB
24224ca5Jonni Liljamo Create plugins mod, move splash to plugins 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * 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 crate::{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()
    }
}