-
-
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
9 changed files
with
137 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
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,89 @@ | ||
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; | ||
|
||
use bevy::prelude::*; | ||
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 CreateGamePlugin; | ||
|
||
impl Plugin for CreateGamePlugin { | ||
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<GameConfigRes>, mut multiplayer: EventWriter<StartMultiplayerEvent>) { | ||
multiplayer.send(StartMultiplayerEvent::new(NetGameConf::new( | ||
config.0.max_players().try_into().unwrap(), | ||
// TODO not localhost | ||
IpAddr::V4(Ipv4Addr::LOCALHOST), | ||
// TODO not fixed port | ||
ServerPort::Main(8082), | ||
))); | ||
} | ||
|
||
fn cleanup(mut commands: Commands) { | ||
commands.remove_resource::<GameConfigRes>(); | ||
|
||
// shutdown multiplayer if not entering MenuState::MultiPlayerGame | ||
} | ||
|
||
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; | ||
}; | ||
|
||
// TODO not localhost | ||
let server = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), opened_event.0); | ||
let game_setup = GameSetup::new(server, config.0.clone()); | ||
sender.send(CreateGameRequest::new(game_setup)); | ||
} | ||
|
||
fn handle_lobby_response( | ||
mut next_state: ResMut<NextState<MenuState>>, | ||
mut receiver: Receiver<CreateGameRequest>, | ||
mut multiplayer: EventWriter<ShutdownMultiplayerEvent>, | ||
mut toasts: EventWriter<ToastEvent>, | ||
) { | ||
if let Some(result) = receiver.receive() { | ||
match result { | ||
Ok(_) => next_state.set(MenuState::MultiPlayerGame), | ||
Err(error) => { | ||
multiplayer.send(ShutdownMultiplayerEvent); | ||
toasts.send(ToastEvent::new(error)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// 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