DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: e32bf353e291ee93e5ce3438cbaf7d6140906bf6 deck-builder/client/src/main.rs -rw-r--r-- 5.0 KiB
e32bf353Jonni Liljamo feat(client): swap cur_game to be in global, and set it 1 year, 8 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * 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.
 */

#![allow(clippy::too_many_arguments)]

use bevy::{
    app::AppExit,
    prelude::*,
    utils::HashMap,
    window::{
        CompositeAlphaMode, Cursor, PresentMode, WindowLevel, WindowMode, WindowResizeConstraints,
        WindowResolution,
    },
};
use bevy_egui::EguiPlugin;
use naia_bevy_client::{
    Client, ClientConfig as NaiaClientConfig, Plugin as NaiaClientPlugin, ReceiveEvents,
};

use laurelin_shared::{
    server::protocol::protocol,
    types::{game::GamePub, user::UserPub},
};

mod cfg;
mod constants;
mod plugins;
mod runtime;
mod util;

#[cfg(feature = "dev")]
mod dev;

// NOTE: lua things, currently not in use
//use bevy_mod_scripting::prelude::*;
//mod lua;

/// Used to control the state of the game
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default, States)]
pub enum GameState {
    #[default]
    Splash,
    Loading,
    MainMenu,
    Game,
}

#[derive(Resource)]
pub struct Global {
    pub users_cache: Vec<UserPub>,
    /// stores ids of users currently in queue to be gotten
    pub users_cache_queue: Vec<String>,
    pub games_cache: HashMap<String, GamePub>,
    pub games_cache_queue: Vec<String>,

    pub cur_game: Option<GamePub>,
}

#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
struct MainLoop;

#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
struct AfterMainLoop;

fn main() {
    let mut app = App::new();

    app.add_plugins(DefaultPlugins.set(WindowPlugin {
        primary_window: Some(Window {
            cursor: Cursor::default(),
            present_mode: PresentMode::Fifo,
            mode: WindowMode::Windowed,
            position: WindowPosition::Centered(MonitorSelection::Primary),
            resolution: WindowResolution::new(1280., 720.),
            title: "Laurelin".to_string(),
            composite_alpha_mode: CompositeAlphaMode::Auto,
            resize_constraints: WindowResizeConstraints {
                min_width: 1280.,
                min_height: 720.,
                max_width: 3840.,
                max_height: 2160.,
            },
            resizable: false,
            decorations: true,
            transparent: false,
            focused: true,
            window_level: WindowLevel::Normal,
            canvas: None,
            fit_canvas_to_parent: false,
            prevent_default_event_handling: false,
            ime_enabled: false,
            ..Default::default()
        }),
        ..Default::default()
    }));

    app.configure_set(MainLoop.after(ReceiveEvents))
        .configure_set(AfterMainLoop.after(MainLoop));

    #[cfg(feature = "dev")]
    app.add_plugin(dev::DevPlugin);
    if !app.is_plugin_added::<EguiPlugin>() {
        app.add_plugin(EguiPlugin);
    }

    //app.add_plugin(ScriptingPlugin).add_plugin(lua::LuaPlugin);

    app.insert_resource(Global {
        users_cache: vec![],
        users_cache_queue: vec![],
        games_cache: HashMap::new(),
        games_cache_queue: vec![],

        cur_game: None,
    })
    .insert_resource(cfg::CfgDirs(
        directories::ProjectDirs::from("com", "liljamo", "deckbuilder")
            .expect("failed to get project directories"),
    ));

    app.insert_resource(cfg::CfgSettings::default())
        .register_type::<cfg::CfgSettings>()
        .insert_resource(cfg::CfgUser::default())
        .register_type::<cfg::CfgUser>()
        .insert_resource(cfg::CfgDev::default())
        .register_type::<cfg::CfgDev>();

    app.add_startup_system(setup).add_state::<GameState>();

    app.add_plugin(plugins::config::ConfigPlugin)
        .add_plugin(plugins::phases::splash::SplashPlugin)
        .add_plugin(plugins::phases::loading::LoadingPlugin)
        .add_plugin(plugins::menu::MenuPlugin);

    // Lastly, add in runtime data structs
    //app.insert_resource(runtime::menu::RTDMenu::default())
    //    .insert_resource(runtime::game::RTDGame::default());

    // Networking
    app.add_plugin(NaiaClientPlugin::new(
        NaiaClientConfig::default(),
        protocol(),
    ))
    .add_plugin(plugins::networking::NetworkingPlugin);

    // Graceful exit
    app.add_event::<GracefulExit>()
        .add_system(handle_graceful_exit.in_set(AfterMainLoop));

    app.run();
}

fn setup(mut commands: Commands) {
    // Spawn a camera
    commands.spawn(Camera3dBundle::default());
}

/// Utility function do despawn an entity and all its children
pub fn despawn_screen<T: Component>(to_despawn: Query<Entity, With<T>>, mut commands: Commands) {
    for entity in &to_despawn {
        commands.entity(entity).despawn_recursive();
    }
}

pub struct GracefulExit;

/// Graceful exit, disconnects client cleanly before closing
pub fn handle_graceful_exit(
    mut ev: EventReader<GracefulExit>,
    mut app_exit_events: EventWriter<AppExit>,
    mut client: Client,
) {
    for _ in ev.iter() {
        if client.is_connected() {
            client.disconnect();
        }

        app_exit_events.send(AppExit);
    }
}