Add the main menu #31

Merged
CoCo_Sol merged 25 commits from main-menu into main 2024-02-09 23:42:37 +00:00
3 changed files with 25 additions and 34 deletions
Showing only changes of commit f1832c4186 - Show all commits

View file

@ -10,7 +10,7 @@ mod menu;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_state::<GameState>() .init_resource::<State<GameState>>()
.add_plugins(MenuPlugin) .add_plugins(MenuPlugin)
.run(); .run();
} }

View file

@ -1,8 +1,8 @@
//! The main menu of the game. //! The main menu of the game.
use crate::GameState;
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;
@ -11,41 +11,32 @@ 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).add_systems(Update,( println!("{:?}", app.world.resource::<State<GameState>>());
ui_connect_window.run_if(state_exists_and_equals(GameState::Menu)), app.add_plugins(EguiPlugin).add_systems(
ui_host_window.run_if(state_exists_and_equals(GameState::Menu)), Update,
)); menu_ui.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 window, with a button to join a game and an input to enter
/// the address. /// the address or create a new game.
fn ui_connect_window( fn menu_ui(mut ctx: EguiContexts, mut connection_string: Local<String>) {
mut ctx: EguiContexts, egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| {
mut connection_string: Local<String>, ui.heading("Border Wars");
mut state: ResMut<State<GameState>>,
) { ui.separator();
egui::Window::new("Connect") ui.label("Connect to an existing game:");
.default_width(400.0)
.show(ctx.ctx_mut(), |ui| {
ui.heading("Please enter the password of the game: ");
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Address: "); ui.label("Game ID: ");
ui.text_edit_singleline(&mut *connection_string); ui.text_edit_singleline(&mut *connection_string);
if ui.button("Join").clicked() { if ui.button("Join").clicked() {
println!("clicked"); println!("clicked");
} }
}); });
}); ui.separator();
}
/// Display an host window, with a button to create a new game.
fn ui_host_window(mut ctx: EguiContexts) {
egui::Window::new("Host")
.default_width(400.0)
.show(ctx.ctx_mut(), |ui| {
if ui.button("Create new game").clicked() { if ui.button("Create new game").clicked() {
println!("clicked"); println!("clicked");
} }