mod.rs (3624B)
1 use std::net::Ipv4Addr; 2 3 use bevy::prelude::*; 4 use bevy::render::RenderPlugin; 5 use bevy::utils::Duration; 6 7 use lightyear::{client::components::Confirmed, prelude::*}; 8 use serde::{Deserialize, Serialize}; 9 10 use self::protocol::{Inputs, PlayerPosition}; 11 12 pub mod client; 13 pub mod protocol; 14 #[cfg(not(target_family = "wasm"))] 15 pub mod server; 16 17 pub fn shared_config() -> SharedConfig { 18 SharedConfig { 19 client_send_interval: Duration::default(), 20 server_send_interval: Duration::from_millis(40), 21 // server_send_interval: Duration::from_millis(100), 22 tick: TickConfig { 23 tick_duration: Duration::from_secs_f64(1.0 / 64.0), 24 }, 25 } 26 } 27 28 pub struct SharedPlugin; 29 30 impl Plugin for SharedPlugin { 31 fn build(&self, app: &mut App) { 32 if app.is_plugin_added::<RenderPlugin>() { 33 app.add_systems(PostUpdate, draw_elements); 34 // app.add_plugins(LogDiagnosticsPlugin { 35 // filter: Some(vec![ 36 // IoDiagnosticsPlugin::BYTES_IN, 37 // IoDiagnosticsPlugin::BYTES_OUT, 38 // ]), 39 // ..default() 40 // }); 41 } 42 } 43 } 44 45 // This system defines how we update the player's positions when we receive an input 46 pub(crate) fn shared_movement_behaviour(mut position: Mut<PlayerPosition>, input: &Inputs) { 47 const MOVE_SPEED: f32 = 10.0; 48 if let Inputs::Direction(direction) = input { 49 if direction.up { 50 position.y += MOVE_SPEED; 51 } 52 if direction.down { 53 position.y -= MOVE_SPEED; 54 } 55 if direction.left { 56 position.x -= MOVE_SPEED; 57 } 58 if direction.right { 59 position.x += MOVE_SPEED; 60 } 61 } 62 } 63 64 /// System that draws the player's boxes and cursors 65 pub fn draw_elements(mut gizmos: Gizmos, players: Query<&PlayerPosition, Without<Confirmed>>) { 66 for position in &players { 67 gizmos.rect_2d( 68 Vec2::new(position.x, position.y), 69 0.0, 70 Vec2::ONE * 40.0, 71 Color::GREEN, 72 ); 73 } 74 } 75 76 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] 77 pub enum ClientTransports { 78 #[cfg(not(target_family = "wasm"))] 79 Udp, 80 WebSocket, 81 } 82 83 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] 84 pub enum ServerTransports { 85 Udp { local_port: u16 }, 86 WebSocket { local_port: u16 }, 87 } 88 89 #[derive(Clone, Debug, Deserialize, Serialize)] 90 pub struct ServerSettings { 91 /// If true, disable any rendering-related plugins 92 pub headless: bool, 93 94 /// If true, enable bevy_inspector_egui 95 pub inspector: bool, 96 97 /// Which transport to use 98 pub transport: Vec<ServerTransports>, 99 } 100 101 #[derive(Clone, Debug, Deserialize, Serialize)] 102 pub struct ClientSettings { 103 /// If true, enable bevy_inspector_egui 104 pub inspector: bool, 105 106 /// The client id 107 pub client_id: u64, 108 109 /// The client port to listen on 110 pub client_port: u16, 111 112 /// The ip address of the server 113 pub server_addr: Ipv4Addr, 114 115 /// The port of the server 116 pub server_port: u16, 117 118 /// Which transport to use 119 pub transport: ClientTransports, 120 } 121 122 #[derive(Copy, Clone, Debug, Deserialize, Serialize)] 123 pub struct SharedSettings { 124 /// An id to identify the protocol version 125 pub protocol_id: u64, 126 127 /// a 32-byte array to authenticate via the Netcode.io protocol 128 pub private_key: [u8; 32], 129 } 130 131 #[derive(Debug, Clone, Deserialize, Serialize)] 132 pub struct Settings { 133 pub server: ServerSettings, 134 pub client: ClientSettings, 135 pub shared: SharedSettings, 136 }