-
-
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
139 additions
and
30 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
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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use bevy::prelude::*; | ||
use de_conf::Configuration; | ||
use de_gui::ToastEvent; | ||
use de_lobby_client::CreateGameRequest; | ||
use de_lobby_model::{GameConfig, GameSetup}; | ||
use de_multiplayer::{ | ||
GameOpenedEvent, NetGameConf, ServerPort, ShutdownMultiplayerEvent, StartMultiplayerEvent, | ||
}; | ||
|
||
use super::{ | ||
requests::{Receiver, Sender}, | ||
MultiplayerState, | ||
}; | ||
use crate::MenuState; | ||
|
||
pub(crate) struct SetupGamePlugin; | ||
|
||
impl Plugin for SetupGamePlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_event::<SetupGameEvent>() | ||
.add_systems(OnEnter(MultiplayerState::GameSetup), setup_network) | ||
.add_systems(OnExit(MultiplayerState::GameSetup), cleanup) | ||
.add_systems( | ||
PreUpdate, | ||
handle_setup_event.run_if(in_state(MenuState::Multiplayer)), | ||
) | ||
.add_systems( | ||
Update, | ||
(create_game_in_lobby, handle_lobby_response) | ||
.run_if(in_state(MultiplayerState::GameSetup)), | ||
); | ||
} | ||
} | ||
|
||
/// Send this event to initiate new multiplayer setup. | ||
/// | ||
/// The game will be opened at a DE Connector and registered at a DE Lobby. | ||
/// Once this is done, the menu transitions to | ||
/// [`MultiplayerState::GameJoined`]. | ||
#[derive(Event)] | ||
pub(super) struct SetupGameEvent { | ||
config: GameConfig, | ||
} | ||
|
||
impl SetupGameEvent { | ||
pub(super) fn new(config: GameConfig) -> Self { | ||
Self { config } | ||
} | ||
} | ||
|
||
#[derive(Resource)] | ||
pub(crate) struct GameConfigRes(GameConfig); | ||
|
||
fn handle_setup_event( | ||
mut commands: Commands, | ||
mut next_state: ResMut<NextState<MultiplayerState>>, | ||
mut events: EventReader<SetupGameEvent>, | ||
) { | ||
let Some(event) = events.iter().last() else { | ||
return; | ||
}; | ||
|
||
commands.insert_resource(GameConfigRes(event.config.clone())); | ||
next_state.set(MultiplayerState::GameSetup); | ||
} | ||
|
||
fn cleanup( | ||
mut commands: Commands, | ||
state: Res<State<MultiplayerState>>, | ||
mut shutdown: EventWriter<ShutdownMultiplayerEvent>, | ||
) { | ||
commands.remove_resource::<GameConfigRes>(); | ||
|
||
if state.as_ref() != &MultiplayerState::GameJoined { | ||
shutdown.send(ShutdownMultiplayerEvent); | ||
} | ||
} | ||
|
||
fn setup_network( | ||
config: Res<Configuration>, | ||
game_config: Res<GameConfigRes>, | ||
mut multiplayer: EventWriter<StartMultiplayerEvent>, | ||
) { | ||
let connector_conf = config.multiplayer().connector(); | ||
let net_game_conf = NetGameConf::new( | ||
game_config.0.max_players().try_into().unwrap(), | ||
connector_conf.ip(), | ||
ServerPort::Main(connector_conf.port()), | ||
); | ||
multiplayer.send(StartMultiplayerEvent::new(net_game_conf)); | ||
} | ||
|
||
fn create_game_in_lobby( | ||
config: Res<GameConfigRes>, | ||
mut opened_events: EventReader<GameOpenedEvent>, | ||
mut sender: Sender<CreateGameRequest>, | ||
) { | ||
let Some(opened_event) = opened_events.iter().last() else { | ||
return; | ||
}; | ||
|
||
let game_setup = GameSetup::new(opened_event.0, config.0.clone()); | ||
sender.send(CreateGameRequest::new(game_setup)); | ||
} | ||
|
||
fn handle_lobby_response( | ||
mut next_state: ResMut<NextState<MultiplayerState>>, | ||
mut receiver: Receiver<CreateGameRequest>, | ||
mut toasts: EventWriter<ToastEvent>, | ||
) { | ||
while let Some(result) = receiver.receive() { | ||
match result { | ||
Ok(_) => { | ||
info!("Game successfully created."); | ||
next_state.set(MultiplayerState::GameJoined); | ||
} | ||
Err(error) => { | ||
toasts.send(ToastEvent::new(error)); | ||
next_state.set(MultiplayerState::SignIn); | ||
} | ||
} | ||
} | ||
} |