generated from tipragot/rust
Start game from the lobby by the admin #86
|
@ -2,6 +2,7 @@
|
|||
|
||||
use bevy::prelude::*;
|
||||
use noise::{NoiseFn, Perlin};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::hex::*;
|
||||
use super::{Tile, TilePosition};
|
||||
|
@ -25,7 +26,7 @@ impl Plugin for MapGenerationPlugin {
|
|||
}
|
||||
|
||||
/// An event to trigger the generation of the map.
|
||||
#[derive(Event)]
|
||||
#[derive(Event, Serialize, Deserialize, Clone, Copy)]
|
||||
pub struct StartMapGeneration {
|
||||
/// The seed used to generate the map.
|
||||
pub seed: u32,
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
//! All the code related to the networking.
|
||||
|
||||
use bevnet::NetworkPlugin;
|
||||
use bevnet::{NetworkAppExt, NetworkPlugin, Receive};
|
||||
use bevy::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use self::connection::ConnectionPlugin;
|
||||
use crate::map::generation::StartMapGeneration;
|
||||
use crate::CurrentScene;
|
||||
|
||||
pub mod connection;
|
||||
|
||||
|
@ -14,7 +16,9 @@ pub struct NetworkingPlugin;
|
|||
impl Plugin for NetworkingPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins(NetworkPlugin::new("relay.cocosol.fr".to_string()))
|
||||
.add_plugins(ConnectionPlugin);
|
||||
.add_plugins(ConnectionPlugin)
|
||||
.add_systems(Update, start_game)
|
||||
.add_network_event::<StartGame>();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,3 +34,19 @@ pub enum PlayerRank {
|
|||
/// The player. He can join the game and play.
|
||||
Player,
|
||||
}
|
||||
|
||||
/// TODO
|
||||
#[derive(Event, Serialize, Deserialize)]
|
||||
pub struct StartGame(pub StartMapGeneration);
|
||||
|
||||
/// TODO
|
||||
fn start_game(
|
||||
mut next_stats: ResMut<NextState<CurrentScene>>,
|
||||
mut start_game_events: EventReader<Receive<StartGame>>,
|
||||
mut start_map_generation_writer: EventWriter<StartMapGeneration>,
|
||||
) {
|
||||
for event in start_game_events.read() {
|
||||
next_stats.set(CurrentScene::Game);
|
||||
start_map_generation_writer.send(event.1.0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,9 @@ use bevnet::{Connection, SendTo};
|
|||
use bevy::prelude::*;
|
||||
use bevy_egui::{egui, EguiContexts};
|
||||
|
||||
use crate::map::generation::StartMapGeneration;
|
||||
use crate::networking::connection::RemovePlayer;
|
||||
use crate::networking::PlayerRank;
|
||||
use crate::networking::{PlayerRank, StartGame};
|
||||
use crate::{CurrentScene, Player};
|
||||
|
||||
/// The plugin for the lobby.
|
||||
|
@ -20,10 +21,11 @@ impl Plugin for LobbyPlugin {
|
|||
/// Display the UI of the lobby.
|
||||
fn lobby_ui(
|
||||
mut ctx: EguiContexts,
|
||||
mut next_scene: ResMut<NextState<CurrentScene>>,
|
||||
connection: Res<Connection>,
|
||||
all_players_query: Query<&Player>,
|
||||
mut kick_player: EventWriter<SendTo<RemovePlayer>>,
|
||||
mut map_size: Local<u32>,
|
||||
mut start_game_event: EventWriter<SendTo<StartGame>>,
|
||||
) {
|
||||
// Get our player info.
|
||||
let Some(self_player) = all_players_query
|
||||
|
@ -63,9 +65,22 @@ fn lobby_ui(
|
|||
ui.separator();
|
||||
}
|
||||
|
||||
if self_player.rank == PlayerRank::Admin && ui.button("Run the game").clicked() {
|
||||
next_scene.set(CurrentScene::Game);
|
||||
// TODO: run the game
|
||||
if self_player.rank != PlayerRank::Admin {
|
||||
return;
|
||||
}
|
||||
|
||||
ui.add(egui::Slider::new(&mut (*map_size), 0..=10).text("map size"));
|
||||
|
||||
if ui.button("Run the game").clicked() {
|
||||
for player in all_players_query.iter() {
|
||||
start_game_event.send(SendTo(
|
||||
player.uuid,
|
||||
StartGame(StartMapGeneration {
|
||||
seed: 0,
|
||||
radius: 10,
|
||||
}),
|
||||
));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue