This commit is contained in:
CoCo_Sol 2024-03-21 09:09:54 +01:00
parent 6dd6cfc3b7
commit 4401b64300
3 changed files with 68 additions and 9 deletions

View file

@ -1,9 +1,12 @@
//! The lobby of the game.
use bevnet::{Connection, SendTo};
use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts};
use crate::CurrentScene;
use crate::networkinig::connection::RemovePlayer;
use crate::networkinig::GameRank;
use crate::{AllPlayer, CurrentScene};
/// The plugin for the lobby.
pub struct LobbyPlugin;
@ -15,7 +18,13 @@ impl Plugin for LobbyPlugin {
}
/// Display the UI of the lobby.
fn lobby_ui(mut ctx: EguiContexts, mut next_scene: ResMut<NextState<CurrentScene>>) {
fn lobby_ui(
mut ctx: EguiContexts,
mut next_scene: ResMut<NextState<CurrentScene>>,
connection: Res<Connection>,
all_players: Res<AllPlayer>,
mut kick_player: EventWriter<SendTo<RemovePlayer>>,
) {
egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| {
ui.heading("Border Wars");
@ -23,14 +32,31 @@ fn lobby_ui(mut ctx: EguiContexts, mut next_scene: ResMut<NextState<CurrentScene
ui.label("Game created");
ui.horizontal(|ui| {
if all_players.get_by_connection(&connection).map(|p| p.rank) != Some(GameRank::Admin) {
return;
}
ui.label("Game ID: ");
// TODO : get the game ID and display it.
ui.label("connection_string");
ui.text_edit_singleline(&mut connection.identifier().unwrap_or_default().to_string());
});
ui.separator();
if ui.button("Run the game").clicked() {
for (uuid, player) in all_players.0.iter() {
ui.label(format!("{}: {:?}", player.name, player.rank));
if all_players.get_by_connection(&connection).map(|p| p.rank) == Some(GameRank::Admin)
&& ui.button("Remove").clicked()
{
for sender_id in all_players.0.keys() {
kick_player.send(SendTo(*sender_id, RemovePlayer(*uuid)));
}
}
ui.separator();
}
if all_players.get_by_connection(&connection).map(|p| p.rank) == Some(GameRank::Admin)
&& ui.button("Run the game").clicked()
{
next_scene.set(CurrentScene::Game);
// TODO: run the game
}

View file

@ -1,9 +1,12 @@
//! The main menu of the game.
use bevnet::{Connection, SendTo, Uuid};
use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts};
use crate::CurrentScene;
use crate::networkinig::connection::RequestJoin;
use crate::networkinig::GameRank;
use crate::{AllPlayer, CurrentScene, Player};
/// The plugin for the menu.
pub struct MenuPlugin;
@ -18,20 +21,43 @@ fn menu_ui(
mut ctx: EguiContexts,
mut connection_string: Local<String>,
mut next_scene: ResMut<NextState<CurrentScene>>,
mut request_join: EventWriter<SendTo<RequestJoin>>,
mut name: Local<String>,
mut all_player: ResMut<AllPlayer>,
connection: Res<Connection>,
) {
let Some(uuid) = connection.identifier() else {
return;
};
egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| {
ui.heading("Border Wars");
ui.separator();
ui.label("Name");
ui.text_edit_singleline(&mut *name);
ui.separator();
ui.label("Connect to an existing game:");
ui.horizontal(|ui| {
ui.label("Game ID: ");
ui.text_edit_singleline(&mut *connection_string);
let Ok(game_id) = Uuid::parse_str(&connection_string) else {
return;
};
if ui.button("Join").clicked() {
next_scene.set(CurrentScene::Game);
// TODO: connect to the game
next_scene.set(CurrentScene::Lobby);
request_join.send(SendTo(
game_id,
RequestJoin(Player {
name: name.clone(),
rank: GameRank::Player,
}),
));
}
});
@ -39,7 +65,13 @@ fn menu_ui(
if ui.button("Create new game").clicked() {
next_scene.set(CurrentScene::Lobby);
// TODO: create a new game
all_player.0.insert(
uuid,
Player {
name: name.clone(),
rank: GameRank::Admin,
},
);
}
});
}

View file

@ -80,7 +80,8 @@ impl Connection {
path.push(".relay-data");
// Check if the file exists.
match path.exists() {
match false {
// path.exists()
true => {
// Read the file and parse the identifier and secret key.
let contents = fs::read(&path)?;