generated from tipragot/rust
Add the main menu #31
|
@ -1,8 +1,11 @@
|
||||||
//! The file that contains utility functions, enums, structs of the game.
|
//! The file that contains utility functions, enums, structs of the game.
|
||||||
|
|
||||||
use bevy::prelude::*;
|
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
|
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
pub mod menu;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
|
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
|
||||||
pub enum GameState {
|
pub enum GameState {
|
||||||
#[default]
|
#[default]
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
//! The main entry point of the game.
|
//! The main entry point of the game.
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use lib::GameState;
|
use border_wars::menu::MenuPlugin;
|
||||||
use menu::MenuPlugin;
|
use border_wars::GameState;
|
||||||
|
|
||||||
mod lib;
|
|
||||||
mod menu;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_egui::{egui, EguiContexts, EguiPlugin};
|
use bevy_egui::{egui, EguiContexts, EguiPlugin};
|
||||||
use border_wars::GameState;
|
|
||||||
|
use crate::GameState;
|
||||||
|
|
||||||
/// The plugin for the menu.
|
/// The plugin for the menu.
|
||||||
pub struct MenuPlugin;
|
pub struct MenuPlugin;
|
||||||
|
@ -11,10 +12,10 @@ 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,(
|
app.add_plugins(EguiPlugin).add_systems(
|
||||||
ui_connect_window.run_if(state_exists_and_equals(GameState::Menu)),
|
Update,
|
||||||
ui_host_window.run_if(state_exists_and_equals(GameState::Menu)),
|
(ui_connect_window, ui_host_window).run_if(in_state(GameState::Menu)),
|
||||||
));
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Display a connect window, with a button to join a game and an input to enter
|
/// Display a connect window, with a button to join a game and an input to enter
|
||||||
|
@ -22,7 +23,7 @@ impl Plugin for MenuPlugin {
|
||||||
fn ui_connect_window(
|
fn ui_connect_window(
|
||||||
mut ctx: EguiContexts,
|
mut ctx: EguiContexts,
|
||||||
mut connection_string: Local<String>,
|
mut connection_string: Local<String>,
|
||||||
mut state: ResMut<State<GameState>>,
|
mut state: ResMut<NextState<GameState>>,
|
||||||
) {
|
) {
|
||||||
egui::Window::new("Connect")
|
egui::Window::new("Connect")
|
||||||
.default_width(400.0)
|
.default_width(400.0)
|
||||||
|
@ -35,19 +36,19 @@ fn ui_connect_window(
|
||||||
ui.text_edit_singleline(&mut *connection_string);
|
ui.text_edit_singleline(&mut *connection_string);
|
||||||
|
|
||||||
if ui.button("Join").clicked() {
|
if ui.button("Join").clicked() {
|
||||||
println!("clicked");
|
state.set(GameState::Game);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Display an host window, with a button to create a new game.
|
/// Display an host window, with a button to create a new game.
|
||||||
fn ui_host_window(mut ctx: EguiContexts) {
|
fn ui_host_window(mut ctx: EguiContexts, mut state: ResMut<NextState<GameState>>) {
|
||||||
egui::Window::new("Host")
|
egui::Window::new("Host")
|
||||||
.default_width(400.0)
|
.default_width(400.0)
|
||||||
.show(ctx.ctx_mut(), |ui| {
|
.show(ctx.ctx_mut(), |ui| {
|
||||||
if ui.button("Create new game").clicked() {
|
if ui.button("Create new game").clicked() {
|
||||||
println!("clicked");
|
state.set(GameState::Lobby);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue