generated from tipragot/rust
Add the main menu #31
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]
|
[lints]
|
||||||
workspace = true
|
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;
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
|
|||||||
|
use border_wars::GameState;
|
||||||
|
|
||||||
fn main() {
|
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);
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
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
tipragot
commented
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
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in a new issue
Module creation should be after imports, not in between.