generated from tipragot/rust
Add the main menu #31
12
crates/border-wars/src/lib.rs
Normal file
12
crates/border-wars/src/lib.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
//! The file that contains utility functions, enums, structs of the game.
|
||||
|
||||
use bevy::prelude::*;
|
||||
use std::default::Default;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
|
||||
pub enum GameState {
|
||||
#[default]
|
||||
Menu,
|
||||
Lobby,
|
||||
Game,
|
||||
}
|
|
@ -1,13 +1,16 @@
|
|||
//! The main entry point of the game.
|
||||
|
||||
use bevy::prelude::*;
|
||||
use lib::GameState;
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
|
||||
use menu::MenuPlugin;
|
||||
|
||||
mod lib;
|
||||
mod menu;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_state::<GameState>()
|
||||
.add_plugins(MenuPlugin)
|
||||
.run();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use bevy::prelude::*;
|
||||
use bevy_egui::{egui, EguiContexts, EguiPlugin};
|
||||
use border_wars::GameState;
|
||||
|
||||
/// The plugin for the menu.
|
||||
pub struct MenuPlugin;
|
||||
|
@ -10,14 +11,19 @@ pub struct MenuPlugin;
|
|||
impl Plugin for MenuPlugin {
|
||||
/// A function that is called when the plugin is added to the application.
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins(EguiPlugin)
|
||||
.add_systems(Update, ui_connect_window)
|
||||
.add_systems(Update, ui_host_window);
|
||||
app.add_plugins(EguiPlugin).add_systems(Update,(
|
||||
ui_connect_window.run_if(state_exists_and_equals(GameState::Menu)),
|
||||
ui_host_window.run_if(state_exists_and_equals(GameState::Menu)),
|
||||
));
|
||||
}
|
||||
}
|
||||
/// Display a connect window, with a button to join a game and an input to enter
|
||||
/// the address.
|
||||
fn ui_connect_window(mut ctx: EguiContexts, mut connection_string: Local<String>) {
|
||||
fn ui_connect_window(
|
||||
mut ctx: EguiContexts,
|
||||
mut connection_string: Local<String>,
|
||||
mut state: ResMut<State<GameState>>,
|
||||
) {
|
||||
egui::Window::new("Connect")
|
||||
.default_width(400.0)
|
||||
.show(ctx.ctx_mut(), |ui| {
|
||||
|
|
Loading…
Reference in a new issue
Module creation should be after imports, not in between.