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.
|
//! The main entry point of the game.
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use lib::GameState;
|
||||||
use menu::MenuPlugin;
|
use menu::MenuPlugin;
|
||||||
|
|
||||||
|
mod lib;
|
||||||
mod menu;
|
mod menu;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
|
.add_state::<GameState>()
|
||||||
.add_plugins(MenuPlugin)
|
.add_plugins(MenuPlugin)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_egui::{egui, EguiContexts, EguiPlugin};
|
use bevy_egui::{egui, EguiContexts, EguiPlugin};
|
||||||
|
use border_wars::GameState;
|
||||||
|
|
||||||
/// The plugin for the menu.
|
/// The plugin for the menu.
|
||||||
pub struct MenuPlugin;
|
pub struct MenuPlugin;
|
||||||
|
@ -10,14 +11,19 @@ pub struct MenuPlugin;
|
||||||
impl Plugin for MenuPlugin {
|
impl Plugin for MenuPlugin {
|
||||||
/// A function that is called when the plugin is added to the application.
|
/// A function that is called when the plugin is added to the application.
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_plugins(EguiPlugin)
|
app.add_plugins(EguiPlugin).add_systems(Update,(
|
||||||
.add_systems(Update, ui_connect_window)
|
ui_connect_window.run_if(state_exists_and_equals(GameState::Menu)),
|
||||||
.add_systems(Update, ui_host_window);
|
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
|
/// Display a connect window, with a button to join a game and an input to enter
|
||||||
/// the address.
|
/// 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")
|
egui::Window::new("Connect")
|
||||||
.default_width(400.0)
|
.default_width(400.0)
|
||||||
.show(ctx.ctx_mut(), |ui| {
|
.show(ctx.ctx_mut(), |ui| {
|
||||||
|
|
Loading…
Reference in a new issue