-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
109 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,111 @@ | ||
use bevy::prelude::*; | ||
use de_core::state::AppState; | ||
use de_multiplayer::ShutdownMultiplayerEvent; | ||
use de_core::{ | ||
assets::asset_path, | ||
gconfig::{GameConfig, LocalPlayers}, | ||
player::Player, | ||
state::AppState, | ||
}; | ||
use de_gui::ToastEvent; | ||
use de_lobby_client::GetGameRequest; | ||
use de_lobby_model::GameConfig as GameConfigModel; | ||
use de_map::hash::MapHash; | ||
use de_multiplayer::{GameReadinessEvent, ShutdownMultiplayerEvent}; | ||
use de_net::Readiness; | ||
|
||
use super::MultiplayerState; | ||
use super::{ | ||
current::GameNameRes, | ||
requests::{Receiver, Sender}, | ||
MultiplayerState, | ||
}; | ||
|
||
pub(crate) struct JoinedGamePlugin; | ||
|
||
impl Plugin for JoinedGamePlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(OnExit(MultiplayerState::GameJoined), cleanup); | ||
app.add_systems(OnExit(MultiplayerState::GameJoined), cleanup) | ||
.add_systems( | ||
Update, | ||
( | ||
handle_readiness.run_if(on_event::<GameReadinessEvent>()), | ||
handle_get_response, | ||
) | ||
.run_if(in_state(MultiplayerState::GameJoined)), | ||
); | ||
} | ||
} | ||
|
||
fn cleanup(state: Res<State<AppState>>, mut shutdown: EventWriter<ShutdownMultiplayerEvent>) { | ||
#[derive(Resource)] | ||
struct GameConfigRes(GameConfigModel); | ||
|
||
fn cleanup( | ||
mut commands: Commands, | ||
state: Res<State<AppState>>, | ||
mut shutdown: EventWriter<ShutdownMultiplayerEvent>, | ||
) { | ||
commands.remove_resource::<GameConfigRes>(); | ||
|
||
if state.as_ref() != &AppState::InGame { | ||
shutdown.send(ShutdownMultiplayerEvent); | ||
} | ||
} | ||
|
||
fn handle_readiness( | ||
mut events: EventReader<GameReadinessEvent>, | ||
game_name: Res<GameNameRes>, | ||
mut sender: Sender<GetGameRequest>, | ||
) { | ||
if events.iter().all(|e| **e != Readiness::Ready) { | ||
return; | ||
} | ||
|
||
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>, | ||
) { | ||
while let Some(result) = receiver.receive() { | ||
match result { | ||
Ok(game) => { | ||
let game_conf = game.setup().config(); | ||
let map_path = match MapHash::from_hex(game_conf.map().hash()) { | ||
Ok(hash) => hash.construct_path(asset_path("maps")), | ||
Err(error) => { | ||
toasts.send(ToastEvent::new(error)); | ||
multi_state.set(MultiplayerState::SignIn); | ||
return; | ||
} | ||
}; | ||
|
||
// TODO players may be sparse? | ||
let max_player = match Player::try_from(game_conf.max_players()) { | ||
Ok(player) => player, | ||
Err(error) => { | ||
toasts.send(ToastEvent::new(format!( | ||
"Game max players err: {:?}", | ||
error | ||
))); | ||
multi_state.set(MultiplayerState::SignIn); | ||
return; | ||
} | ||
}; | ||
|
||
commands.insert_resource(GameConfig::new( | ||
map_path, | ||
max_player, | ||
// TODO use real player | ||
LocalPlayers::new(Player::Player1), | ||
)); | ||
app_state.set(AppState::InGame); | ||
} | ||
Err(error) => { | ||
toasts.send(ToastEvent::new(error)); | ||
multi_state.set(MultiplayerState::SignIn); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters