generated from tipragot/rust
Add the main menu (#31)
All checks were successful
Rust Checks / checks (push) Successful in 1m2s
All checks were successful
Rust Checks / checks (push) Successful in 1m2s
Reviewed-on: corentin/border-wars#31 Reviewed-by: Tipragot <contact@tipragot.fr> Co-authored-by: CoCoSol007 <solois.corentin@gmail.com> Co-committed-by: CoCoSol007 <solois.corentin@gmail.com>
This commit is contained in:
parent
36a1e282c8
commit
9dee5ad781
3915
Cargo.lock
generated
3915
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -8,3 +8,7 @@ authors = ["CoCoSol"]
|
|||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.12.1"
|
||||
bevy_egui = "0.24.0"
|
||||
|
|
19
crates/border-wars/src/lib.rs
Normal file
19
crates/border-wars/src/lib.rs
Normal 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,
|
||||
}
|
|
@ -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;
|
||||
use border_wars::GameState;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_state::<GameState>()
|
||||
.add_plugins(MenuPlugin)
|
||||
.run();
|
||||
}
|
||||
|
|
48
crates/border-wars/src/menu.rs
Normal file
48
crates/border-wars/src/menu.rs
Normal 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);
|
||||
// TODO: connect to the game
|
||||
}
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
if ui.button("Create new game").clicked() {
|
||||
next_state.set(GameState::Lobby);
|
||||
// TODO: create a new game
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue