-
-
Notifications
You must be signed in to change notification settings - Fork 25
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
10 changed files
with
137 additions
and
31 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
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,97 @@ | ||
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 crate::{ | ||
requests::{Receiver, RequestsPlugin, Sender}, | ||
MenuState, | ||
}; | ||
|
||
pub(crate) struct SetupGamePlugin; | ||
|
||
impl Plugin for SetupGamePlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_plugin(RequestsPlugin::<CreateGameRequest>::new()) | ||
.add_system(setup_network.in_schedule(OnEnter(MenuState::GameSetup))) | ||
.add_system(cleanup.in_schedule(OnExit(MenuState::GameSetup))) | ||
.add_system( | ||
create_game_in_lobby | ||
.run_if(in_state(MenuState::GameSetup)) | ||
.run_if(on_event::<GameOpenedEvent>()), | ||
) | ||
.add_system(handle_lobby_response.run_if(in_state(MenuState::GameSetup))); | ||
} | ||
} | ||
|
||
#[derive(Resource)] | ||
pub(crate) struct GameConfigRes(GameConfig); | ||
|
||
impl GameConfigRes { | ||
pub(crate) fn new(config: GameConfig) -> Self { | ||
Self(config) | ||
} | ||
} | ||
|
||
fn setup_network( | ||
config: Res<Configuration>, | ||
game_config: Res<GameConfigRes>, | ||
mut multiplayer: EventWriter<StartMultiplayerEvent>, | ||
) { | ||
let connector_conf = config.multiplayer().connector(); | ||
multiplayer.send(StartMultiplayerEvent::new(NetGameConf::new( | ||
game_config.0.max_players().try_into().unwrap(), | ||
connector_conf.ip(), | ||
ServerPort::Main(connector_conf.port()), | ||
))); | ||
} | ||
|
||
fn cleanup( | ||
mut commands: Commands, | ||
state: Res<State<MenuState>>, | ||
mut shutdown: EventWriter<ShutdownMultiplayerEvent>, | ||
) { | ||
commands.remove_resource::<GameConfigRes>(); | ||
|
||
if state.0 != MenuState::MultiPlayerGame { | ||
shutdown.send(ShutdownMultiplayerEvent); | ||
} | ||
} | ||
|
||
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<MenuState>>, | ||
mut receiver: Receiver<CreateGameRequest>, | ||
mut toasts: EventWriter<ToastEvent>, | ||
) { | ||
while let Some(result) = receiver.receive() { | ||
match result { | ||
Ok(_) => { | ||
info!("Game successfully created."); | ||
next_state.set(MenuState::MultiPlayerGame); | ||
} | ||
Err(error) => { | ||
toasts.send(ToastEvent::new(error)); | ||
next_state.set(MenuState::GameCreation); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// TODO handle multiplayer errors |
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