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() {
App::new()
.add_plugins(DefaultPlugins)
.add_state::<GameState>()
.init_resource::<State<GameState>>()
.add_plugins(MenuPlugin)
.run();
}

View file

@ -1,8 +1,8 @@
//! The main menu of the game.
use crate::GameState;
use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts, EguiPlugin};
use border_wars::GameState;
/// The plugin for the menu.
pub struct MenuPlugin;
@ -11,41 +11,32 @@ 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.run_if(state_exists_and_equals(GameState::Menu)),
ui_host_window.run_if(state_exists_and_equals(GameState::Menu)),
));
println!("{:?}", app.world.resource::<State<GameState>>());
app.add_plugins(EguiPlugin).add_systems(
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
/// the address.
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| {
ui.heading("Please enter the password of the game: ");
/// Display a window, with a button to join a game and an input to enter
/// the address or create a new game.
fn menu_ui(mut ctx: EguiContexts, mut connection_string: Local<String>) {
egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| {
ui.heading("Border Wars");
ui.separator();
ui.label("Connect to an existing game:");
ui.horizontal(|ui| {
ui.label("Address: ");
ui.label("Game ID: ");
ui.text_edit_singleline(&mut *connection_string);
if ui.button("Join").clicked() {
println!("clicked");
}
CoCo_Sol marked this conversation as resolved Outdated

This example does not use the function.

This example does not use the function.
});
});
}
ui.separator();
CoCo_Sol marked this conversation as resolved Outdated

You should use a local resource instead.

You should use a local resource instead.
/// 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() {
println!("clicked");
}