//! Shared code between server and client :3 use crate::protocol::*; use bevy::prelude::*; #[cfg(all(feature = "webtransport", not(target_family = "wasm")))] use bevy::tasks::IoTaskPool; use core::net::{IpAddr, Ipv4Addr, SocketAddr}; use core::time::Duration; use leafwing_input_manager::prelude::*; use lightyear::prelude::*; use serde::{Deserialize, Serialize}; pub struct SharedPlugin; pub const FIXED_TIMESTEP_HZ: f64 = 64.0; pub const SERVER_REPLICATION_INTERVAL: Duration = Duration::from_millis(100); pub const SERVER_ADDR: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 5000); impl Plugin for SharedPlugin { fn build(&self, app: &mut App) { app.add_plugins(ProtocolPlugin); } } pub(crate) fn shared_movement_behaviour( mut position: Mut, action: &ActionState, ) { const MOVE_SPEED: f32 = 10.0; if action.pressed(&PlayerActions::Up) { position.0.y += MOVE_SPEED; } if action.pressed(&PlayerActions::Down) { position.0.y -= MOVE_SPEED; } if action.pressed(&PlayerActions::Left) { position.0.x -= MOVE_SPEED; } if action.pressed(&PlayerActions::Right) { position.0.x += MOVE_SPEED; } } #[cfg(all(feature = "webtransport", not(target_family = "wasm")))] pub(crate) fn webtransport_self_signed_certificate() -> Identity { // Keep this file-backed so native servers and wasm clients agree on certificates/digest.txt. // Runtime-generated self-signed identities have a new digest each run. let cert = format!("{}/../../certificates/cert.pem", env!("CARGO_MANIFEST_DIR")); let key = format!("{}/../../certificates/key.pem", env!("CARGO_MANIFEST_DIR")); IoTaskPool::get() .scope(|s| { s.spawn(async_compat::Compat::new(async move { Identity::load_pemfiles(&cert, &key).await.unwrap() })); }) .pop() .unwrap() }