diff options
Diffstat (limited to 'src/client.rs')
-rw-r--r-- | src/client.rs | 73 |
1 files changed, 48 insertions, 25 deletions
diff --git a/src/client.rs b/src/client.rs index 8793c4a..a5a7845 100644 --- a/src/client.rs +++ b/src/client.rs @@ -8,6 +8,7 @@ use bevy::prelude::*; use bevy::sprite::{MaterialMesh2dBundle, Mesh2dHandle}; use lightyear::client::input::InputSystemSet; use lightyear::prelude::*; +use std::net::SocketAddr; mod network; @@ -15,30 +16,42 @@ const PLAYER_RADIUS: f32 = 10.; const PLAYER_HOVER_INDICATOR_RADIUS: f32 = 13.; const PLAYER_HOVER_RADIUS: f32 = 20.; -pub fn main(transport: TransportConfig) { +pub fn main(server_addr: Option<SocketAddr>, client_id: u64, transport: TransportConfig) { App::new() .add_plugins(DefaultPlugins) - .add_plugins(ClientPlugin { transport }) + .add_plugins(ClientPlugin { + server_addr, + client_id, + transport, + }) .run(); } +#[derive(Resource)] +struct ClientId(pub u64); + struct ClientPlugin { + pub server_addr: Option<SocketAddr>, + pub client_id: u64, pub transport: TransportConfig, } impl Plugin for ClientPlugin { fn build(&self, app: &mut App) { - app.add_plugins(NetworkPlugin { - transport: self.transport.clone(), - }) - .add_systems(Startup, setup) - .add_systems(Update, (render_players, render_projectiles)) - .add_systems(Update, (move_players, move_projectiles)) - .add_systems( - FixedPreUpdate, - buffer_input.in_set(InputSystemSet::BufferInputs), - ) - .add_systems(Last, (gizmos_hover_indicator, gizmos_attack_indicator)); + app.insert_resource(ClientId(self.client_id)) + .add_plugins(NetworkPlugin { + server_addr: self.server_addr.clone(), + client_id: self.client_id, + transport: self.transport.clone(), + }) + .add_systems(Startup, setup) + .add_systems(Update, (render_players, render_projectiles)) + .add_systems(Update, (move_players, move_projectiles)) + .add_systems( + FixedPreUpdate, + buffer_input.in_set(InputSystemSet::BufferInputs), + ) + .add_systems(Last, (gizmos_hover_indicator, gizmos_attack_indicator)); } } @@ -98,12 +111,15 @@ fn move_players(mut players: Query<(&mut Transform, &PlayerPosition), Changed<Pl fn buffer_input( attackables: Query<(&PlayerId, &PlayerPosition)>, cameras: Query<(&Camera, &GlobalTransform)>, + client_id: Res<ClientId>, mouse_input: Res<ButtonInput<MouseButton>>, mut client: ClientMut, windows: Query<&Window>, ) { if mouse_input.just_pressed(MouseButton::Left) { - if let Some((target_player, _)) = hovered_other_player(&windows, &cameras, &attackables) { + if let Some((target_player, _)) = + hovered_other_player(&cameras, &client_id, &attackables, &windows) + { client.add_input(Inputs::Imperative(Imperative::Attack(target_player))); } else { if let Some(world_position) = cursor_world_position(&windows, &cameras) { @@ -114,12 +130,14 @@ fn buffer_input( } fn gizmos_hover_indicator( - mut gizmos: Gizmos, - hoverables: Query<(&PlayerId, &PlayerPosition)>, cameras: Query<(&Camera, &GlobalTransform)>, + client_id: Res<ClientId>, + hoverables: Query<(&PlayerId, &PlayerPosition)>, + mut gizmos: Gizmos, windows: Query<&Window>, ) { - let Some((_, position)) = hovered_other_player(&windows, &cameras, &hoverables) else { + let Some((_, position)) = hovered_other_player(&cameras, &client_id, &hoverables, &windows) + else { return; }; gizmos.circle_2d(position.0, PLAYER_HOVER_INDICATOR_RADIUS, Color::GREEN); @@ -138,23 +156,24 @@ fn cursor_world_position( } fn hovered_other_player( - windows: &Query<&Window>, cameras: &Query<(&Camera, &GlobalTransform)>, + client_id: &Res<ClientId>, hoverables: &Query<(&PlayerId, &PlayerPosition)>, + windows: &Query<&Window>, ) -> Option<(PlayerId, PlayerPosition)> { - let Some((id, position)) = hovered_player(windows, cameras, hoverables) else { + let Some((id, position)) = hovered_player(cameras, hoverables, windows) else { return None; }; - if id.0 == 0 { + if id.0 == client_id.0 { return None; } Some((id, position)) } fn hovered_player( - windows: &Query<&Window>, cameras: &Query<(&Camera, &GlobalTransform)>, hoverables: &Query<(&PlayerId, &PlayerPosition)>, + windows: &Query<&Window>, ) -> Option<(PlayerId, PlayerPosition)> { let Some(world_position) = cursor_world_position(&windows, &cameras) else { return None; @@ -170,22 +189,26 @@ fn hovered_player( fn gizmos_attack_indicator( cameras: Query<(&Camera, &GlobalTransform)>, + client_id: Res<ClientId>, hoverables: Query<(&PlayerId, &PlayerPosition)>, mut gizmos: Gizmos, players: Query<(&PlayerId, &PlayerPosition)>, windows: Query<&Window>, ) { - let Some(position) = player_position(&players) else { + let Some(position) = player_position(&client_id, &players) else { return; }; - if hovered_other_player(&windows, &cameras, &hoverables).is_some() { + if hovered_other_player(&cameras, &client_id, &hoverables, &windows).is_some() { gizmos.circle_2d(position.0, ATTACK_RANGE, Color::YELLOW); } } -fn player_position(players: &Query<(&PlayerId, &PlayerPosition)>) -> Option<PlayerPosition> { +fn player_position( + client_id: &Res<ClientId>, + players: &Query<(&PlayerId, &PlayerPosition)>, +) -> Option<PlayerPosition> { for (id, position) in players.iter() { - if id.0 == 0 { + if id.0 == client_id.0 { return Some(*position); } } |