aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client.rs73
-rw-r--r--src/client/network.rs7
-rw-r--r--src/main.rs51
-rw-r--r--src/shared.rs1
4 files changed, 94 insertions, 38 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);
}
}
diff --git a/src/client/network.rs b/src/client/network.rs
index 4d1a128..df55cc9 100644
--- a/src/client/network.rs
+++ b/src/client/network.rs
@@ -8,8 +8,11 @@ use lightyear::client::resource::Authentication;
use lightyear::prelude::client::NetConfig;
use lightyear::prelude::*;
use lightyear::transport::LOCAL_SOCKET;
+use std::net::SocketAddr;
pub struct NetworkPlugin {
+ pub server_addr: Option<SocketAddr>,
+ pub client_id: u64,
pub transport: TransportConfig,
}
@@ -20,8 +23,8 @@ impl Plugin for NetworkPlugin {
net: NetConfig::Netcode {
config: Default::default(),
auth: Authentication::Manual {
- server_addr: LOCAL_SOCKET,
- client_id: CLIENT_ID,
+ server_addr: self.server_addr.unwrap_or(LOCAL_SOCKET),
+ client_id: self.client_id,
private_key: KEY,
protocol_id: PROTOCOL_ID,
},
diff --git a/src/main.rs b/src/main.rs
index cdd7c17..f213197 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,6 @@
+use clap::Parser;
use lightyear::transport::io::TransportConfig;
+use rand::Rng;
use shared::SERVER_PORT;
use std::net::Ipv4Addr;
use std::net::SocketAddr;
@@ -9,19 +11,48 @@ mod protocol;
mod server;
mod shared;
+#[derive(Parser, Debug)]
+struct Cli {
+ #[arg(long, value_name = "HOST:PORT")]
+ connect_to: Option<SocketAddr>,
+ #[arg(long, num_args = 0)]
+ server: bool,
+}
+
fn main() {
- let (from_server_send, from_server_recv) = crossbeam_channel::unbounded();
- let (to_server_send, to_server_recv) = crossbeam_channel::unbounded();
+ let cli = Cli::parse();
+
+ let mut rng = rand::thread_rng();
+ let client_id = rng.gen_range(1..=SERVER_PORT);
- thread::spawn(|| {
+ if cli.connect_to.is_some() && cli.server {
+ panic!("cannot specify both `--connect-to` and `--server`");
+ }
+
+ if cli.server {
let server_addr = SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), SERVER_PORT);
- server::main(TransportConfig::Channels {
- channels: [(server_addr, to_server_recv, from_server_send)].to_vec(),
+ server::main(TransportConfig::UdpSocket(server_addr));
+ } else if let Some(server_addr) = cli.connect_to {
+ let client_addr = SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), SERVER_PORT + client_id);
+ client::main(Some(server_addr), client_id as u64, TransportConfig::UdpSocket(client_addr));
+ } else {
+ let (from_server_send, from_server_recv) = crossbeam_channel::unbounded();
+ let (to_server_send, to_server_recv) = crossbeam_channel::unbounded();
+
+ thread::spawn(|| {
+ let server_addr = SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), SERVER_PORT);
+ server::main(TransportConfig::Channels {
+ channels: [(server_addr, to_server_recv, from_server_send)].to_vec(),
+ });
});
- });
- client::main(TransportConfig::LocalChannel {
- recv: from_server_recv,
- send: to_server_send,
- });
+ client::main(
+ None,
+ client_id as u64,
+ TransportConfig::LocalChannel {
+ recv: from_server_recv,
+ send: to_server_send,
+ },
+ );
+ }
}
diff --git a/src/shared.rs b/src/shared.rs
index 4db5224..b3a7cc4 100644
--- a/src/shared.rs
+++ b/src/shared.rs
@@ -11,7 +11,6 @@ pub mod cooldown;
pub mod imperative;
pub mod projectile;
-pub const CLIENT_ID: u64 = 0;
pub const KEY: [u8; 32] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];