generated from tipragot/rust
Adding a lobby scene #34
|
@ -2,11 +2,11 @@
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
pub mod menus;
|
pub mod scenes;
|
||||||
|
|
||||||
/// The state of the game.
|
/// The current scene of the game.
|
||||||
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
|
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
|
||||||
pub enum GameState {
|
pub enum CurrentScene {
|
||||||
/// When we are in the main menu.
|
/// When we are in the main menu.
|
||||||
#[default]
|
#[default]
|
||||||
Menu,
|
Menu,
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
//! The main entry point of the game.
|
//! The main entry point of the game.
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use border_wars::menus::MenusPlugin;
|
use border_wars::scenes::ScenesPlugin;
|
||||||
use border_wars::GameState;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
.add_state::<GameState>()
|
.add_plugins(ScenesPlugin)
|
||||||
.add_plugins(MenusPlugin)
|
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
//! The file containing all menu programs.
|
|
||||||
|
|
||||||
use bevy::prelude::*;
|
|
||||||
use bevy_egui::EguiPlugin;
|
|
||||||
|
|
||||||
use self::lobby::LobbyPlugin;
|
|
||||||
|
|
||||||
pub mod lobby;
|
|
||||||
pub mod menu;
|
|
||||||
|
|
||||||
/// The plugin for all menus.
|
|
||||||
pub struct MenusPlugin;
|
|
||||||
|
|
||||||
impl Plugin for MenusPlugin {
|
|
||||||
fn build(&self, app: &mut App) {
|
|
||||||
app.add_plugins(EguiPlugin)
|
|
||||||
.add_plugins(menu::MenuPlugin)
|
|
||||||
.add_plugins(LobbyPlugin);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,22 +3,19 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_egui::{egui, EguiContexts};
|
use bevy_egui::{egui, EguiContexts};
|
||||||
|
|
||||||
use crate::GameState;
|
use crate::CurrentScene;
|
||||||
|
|
||||||
/// The plugin for the lobby.
|
/// The plugin for the lobby.
|
||||||
pub struct LobbyPlugin;
|
pub struct LobbyPlugin;
|
||||||
|
|
||||||
impl Plugin for LobbyPlugin {
|
impl Plugin for LobbyPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(
|
app.add_systems(Update, lobby_ui.run_if(in_state(CurrentScene::Lobby)));
|
||||||
Update,
|
|
||||||
lobby_ui.run_if(state_exists_and_equals(GameState::Lobby)),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Display the UI of the menu to host a game or join one.
|
/// Display the UI of the menu to host a game or join one.
|
||||||
fn lobby_ui(mut ctx: EguiContexts, mut next_state: ResMut<NextState<GameState>>) {
|
fn lobby_ui(mut ctx: EguiContexts, mut next_scene: ResMut<NextState<CurrentScene>>) {
|
||||||
egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| {
|
egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| {
|
||||||
ui.heading("Border Wars");
|
ui.heading("Border Wars");
|
||||||
|
|
||||||
|
@ -33,7 +30,7 @@ fn lobby_ui(mut ctx: EguiContexts, mut next_state: ResMut<NextState<GameState>>)
|
||||||
ui.separator();
|
ui.separator();
|
||||||
|
|
||||||
if ui.button("Run the game").clicked() {
|
if ui.button("Run the game").clicked() {
|
||||||
next_state.set(GameState::Game);
|
next_scene.set(CurrentScene::Game);
|
||||||
// TODO: run the game
|
// TODO: run the game
|
||||||
}
|
}
|
||||||
});
|
});
|
|
@ -3,24 +3,21 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_egui::{egui, EguiContexts};
|
use bevy_egui::{egui, EguiContexts};
|
||||||
|
|
||||||
use crate::GameState;
|
use crate::CurrentScene;
|
||||||
|
|
||||||
/// The plugin for the menu.
|
/// The plugin for the menu.
|
||||||
pub struct MenuPlugin;
|
pub struct MenuPlugin;
|
||||||
|
|
||||||
impl Plugin for MenuPlugin {
|
impl Plugin for MenuPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(
|
app.add_systems(Update, menu_ui.run_if(in_state(CurrentScene::Menu)));
|
||||||
Update,
|
|
||||||
menu_ui.run_if(state_exists_and_equals(GameState::Menu)),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Display the UI of the menu to host a game or join one.
|
/// Display the UI of the menu to host a game or join one.
|
||||||
fn menu_ui(
|
fn menu_ui(
|
||||||
mut ctx: EguiContexts,
|
mut ctx: EguiContexts,
|
||||||
mut connection_string: Local<String>,
|
mut connection_string: Local<String>,
|
||||||
mut next_state: ResMut<NextState<GameState>>,
|
mut next_scene: ResMut<NextState<CurrentScene>>,
|
||||||
) {
|
) {
|
||||||
egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| {
|
egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| {
|
||||||
ui.heading("Border Wars");
|
ui.heading("Border Wars");
|
||||||
|
@ -33,7 +30,7 @@ fn menu_ui(
|
||||||
ui.text_edit_singleline(&mut *connection_string);
|
ui.text_edit_singleline(&mut *connection_string);
|
||||||
|
|
||||||
if ui.button("Join").clicked() {
|
if ui.button("Join").clicked() {
|
||||||
next_state.set(GameState::Game);
|
next_scene.set(CurrentScene::Game);
|
||||||
// TODO: connect to the game
|
// TODO: connect to the game
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -41,7 +38,7 @@ fn menu_ui(
|
||||||
ui.separator();
|
ui.separator();
|
||||||
|
|
||||||
if ui.button("Create new game").clicked() {
|
if ui.button("Create new game").clicked() {
|
||||||
next_state.set(GameState::Lobby);
|
next_scene.set(CurrentScene::Lobby);
|
||||||
// TODO: create a new game
|
// TODO: create a new game
|
||||||
}
|
}
|
||||||
});
|
});
|
21
crates/border-wars/src/scenes/mod.rs
Normal file
21
crates/border-wars/src/scenes/mod.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
//! The file containing all scenes programs.
|
||||||
|
|
||||||
|
use bevy::prelude::*;
|
||||||
|
use bevy_egui::EguiPlugin;
|
||||||
|
|
||||||
|
use crate::CurrentScene;
|
||||||
|
|
||||||
|
pub mod lobby;
|
||||||
|
pub mod menu;
|
||||||
|
|
||||||
|
/// The plugin for all scenes.
|
||||||
|
pub struct ScenesPlugin;
|
||||||
|
|
||||||
|
impl Plugin for ScenesPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_plugins(EguiPlugin)
|
||||||
|
.add_state::<CurrentScene>()
|
||||||
|
.add_plugins(menu::MenuPlugin)
|
||||||
|
.add_plugins(lobby::LobbyPlugin);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue