generated from tipragot/rust
Add the main menu #31
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,43 +11,34 @@ 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>>,
|
|
||||||
) {
|
|
||||||
egui::Window::new("Connect")
|
|
||||||
.default_width(400.0)
|
|
||||||
.show(ctx.ctx_mut(), |ui| {
|
|
||||||
ui.heading("Please enter the password of the game: ");
|
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
ui.separator();
|
||||||
ui.label("Address: ");
|
ui.label("Connect to an existing game:");
|
||||||
|
|
||||||
ui.text_edit_singleline(&mut *connection_string);
|
ui.horizontal(|ui| {
|
||||||
|
ui.label("Game ID: ");
|
||||||
|
ui.text_edit_singleline(&mut *connection_string);
|
||||||
|
|
||||||
if ui.button("Join").clicked() {
|
if ui.button("Join").clicked() {
|
||||||
println!("clicked");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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");
|
println!("clicked");
|
||||||
}
|
}
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
|
|||||||
});
|
});
|
||||||
|
ui.separator();
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
You should use a local resource instead. You should use a local resource instead.
|
|||||||
|
|
||||||
|
if ui.button("Create new game").clicked() {
|
||||||
|
println!("clicked");
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue
This example does not use the function.