DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: dcdb2639c124e8c51e7f0a812e6dc6117dd7a7a1 deck-builder/client/src/plugins/phases/loading/mod.rs -rw-r--r-- 2.6 KiB
dcdb2639Jonni Liljamo feat(client): handle AfterAuth message 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
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * This file is part of laurelin/client
 * Copyright (C) 2023 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 iyes_loopless::prelude::*;

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_enter_system(GameState::Loading, loading_setup)
            // Despawn when we exit the Loading state
            .add_exit_system(GameState::Loading, 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 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,
    });

    commands.insert_resource(NextState(GameState::MainMenu));
}