Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 committed Aug 16, 2023
1 parent b0bfd01 commit 968b292
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
25 changes: 11 additions & 14 deletions crates/menu/src/multiplayer/joined.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
use bevy::prelude::*;
use de_core::{
assets::asset_path,
gconfig::{GameConfig, LocalPlayers},
player::Player,
state::AppState,
};
use de_core::state::AppState;
use de_gui::ToastEvent;
use de_lobby_client::GetGameRequest;
use de_map::hash::MapHash;
use de_messages::Readiness;
use de_multiplayer::{GameReadinessEvent, ShutdownMultiplayerEvent};
use de_multiplayer::{PeerJoinedEvent, PeerLeftEvent, ShutdownMultiplayerEvent};

use super::{
current::GameNameRes,
Expand All @@ -21,10 +14,16 @@ pub(crate) struct JoinedGamePlugin;

impl Plugin for JoinedGamePlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnExit(MultiplayerState::GameJoined), cleanup)
app.add_systems(OnEnter(MultiplayerState::GameJoined), refresh)
.add_systems(OnExit(MultiplayerState::GameJoined), cleanup)
.add_systems(
Update,
(refresh, handle_get_response).run_if(in_state(MultiplayerState::GameJoined)),
(
refresh
.run_if(on_event::<PeerJoinedEvent>().or_else(on_event::<PeerLeftEvent>())),
handle_get_response,
)
.run_if(in_state(MultiplayerState::GameJoined)),
);
}
}
Expand All @@ -36,14 +35,12 @@ fn cleanup(state: Res<State<AppState>>, mut shutdown: EventWriter<ShutdownMultip
}

fn refresh(game_name: Res<GameNameRes>, mut sender: Sender<GetGameRequest>) {
// TODO when player joins or first time
info!("Refreshing game info...");
sender.send(GetGameRequest::new(game_name.name_owned()));
}

fn handle_get_response(
mut commands: Commands,
mut multi_state: ResMut<NextState<MultiplayerState>>,
mut app_state: ResMut<NextState<AppState>>,
mut receiver: Receiver<GetGameRequest>,
mut toasts: EventWriter<ToastEvent>,
) {
Expand Down
22 changes: 22 additions & 0 deletions crates/multiplayer/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ impl GameJoinedEvent {
}
}

#[derive(Event)]
pub struct PeerJoinedEvent(u8);

impl PeerJoinedEvent {
pub fn id(&self) -> u8 {
self.0
}
}

#[derive(Event)]
pub struct PeerLeftEvent(u8);

impl PeerLeftEvent {
pub fn id(&self) -> u8 {
self.0
}
}

fn open_or_join(
conf: Res<NetGameConfRes>,
mut main_server: EventWriter<ToMainServerEvent>,
Expand Down Expand Up @@ -118,6 +136,8 @@ fn process_from_game(
mut fatals: EventWriter<FatalErrorEvent>,
state: Res<State<NetState>>,
mut joined_events: EventWriter<GameJoinedEvent>,
mut peer_joined_events: EventWriter<PeerJoinedEvent>,
mut peer_left_events: EventWriter<PeerLeftEvent>,
mut next_state: ResMut<NextState<NetState>>,
) {
for event in inputs.iter() {
Expand Down Expand Up @@ -169,9 +189,11 @@ fn process_from_game(
}
FromGame::PeerJoined(id) => {
info!("Peer {id} joined.");
peer_joined_events.send(PeerJoinedEvent(*id));
}
FromGame::PeerLeft(id) => {
info!("Peer {id} left.");
peer_left_events.send(PeerLeftEvent(*id));
}
FromGame::GameReadiness(readiness) => {
info!("Game readiness changed to: {readiness:?}");
Expand Down
2 changes: 1 addition & 1 deletion crates/multiplayer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use stats::StatsPlugin;

pub use crate::{
config::{ConnectionType, NetGameConf},
game::{GameJoinedEvent, GameOpenedEvent},
game::{GameJoinedEvent, GameOpenedEvent, PeerJoinedEvent, PeerLeftEvent},
lifecycle::{MultiplayerShuttingDownEvent, ShutdownMultiplayerEvent, StartMultiplayerEvent},
netstate::NetState,
};
Expand Down

0 comments on commit 968b292

Please sign in to comment.