Add the main menu #31

Merged
CoCo_Sol merged 25 commits from main-menu into main 2024-02-09 23:42:37 +00:00
5 changed files with 3986 additions and 12 deletions

3915
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -8,3 +8,7 @@ authors = ["CoCoSol"]
[lints] [lints]
workspace = true workspace = true
[dependencies]
bevy = "0.12.1"
bevy_egui = "0.24.0"

View file

@ -0,0 +1,19 @@
//! The file that contains utility functions, enums, structs for the game.
use bevy::prelude::*;
pub mod menu;
/// The state of the game.
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
pub enum GameState {
/// When we are in the main menu.
#[default]
Menu,
/// When we are in the lobby waiting for players to join the game.
Lobby,
/// When we play this wonderful game.
Game,
}

View file

@ -1,5 +1,13 @@
//! A simple program that prints "Hello, world!" to the terminal. //! The main entry point of the game.
use bevy::prelude::*;
use border_wars::menu::MenuPlugin;
CoCo_Sol marked this conversation as resolved Outdated

Module creation should be after imports, not in between.

Module creation should be after imports, not in between.
use border_wars::GameState;
fn main() { fn main() {
println!("Hello, world!"); App::new()
.add_plugins(DefaultPlugins)
.add_state::<GameState>()
.add_plugins(MenuPlugin)
.run();
} }

View file

@ -0,0 +1,48 @@
//! The main menu of the game.
use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts, EguiPlugin};
use crate::GameState;
/// The plugin for the menu.
pub struct MenuPlugin;
impl Plugin for MenuPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(EguiPlugin).add_systems(
Update,
menu_ui.run_if(state_exists_and_equals(GameState::Menu)),
);
}
}
/// Display the UI of the menu to host a game or join one.
fn menu_ui(
mut ctx: EguiContexts,
mut connection_string: Local<String>,
mut next_state: ResMut<NextState<GameState>>,
) {
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("Game ID: ");
ui.text_edit_singleline(&mut *connection_string);
if ui.button("Join").clicked() {
next_state.set(GameState::Game);
CoCo_Sol marked this conversation as resolved Outdated

This example does not use the function.

This example does not use the function.
// TODO: connect to the game
}
CoCo_Sol marked this conversation as resolved Outdated

You should use a local resource instead.

You should use a local resource instead.
});
ui.separator();
if ui.button("Create new game").clicked() {
next_state.set(GameState::Lobby);
// TODO: create a new game
}
});
}