Something went wrong. Try again.
cozy factory game
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use crate::automation::{ClientStartupConfig, Headless};use crate::protocol::*;use crate::shared::{SERVER_ADDR, shared_movement_behaviour};use bevy::prelude::*;use leafwing_input_manager::prelude::*;use lightyear::netcode::client_plugin::NetcodeConfig;use lightyear::netcode::{Key, NetcodeClient};use lightyear::prediction::rollback::DeterministicPredicted;use lightyear::prelude::*;#[cfg(feature = "webtransport")]use lightyear::webtransport::client::WebTransportClientIo;
pub struct ClientPlugin;
const CLIENT_ADDR: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 4000);
impl Plugin for ClientPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, startup);
app.add_systems(FixedUpdate, player_movement); app.add_observer(handle_interpolated_spawn); app.add_observer(handle_predicted_spawn); app.add_observer(handle_controlled_spawn); }}
fn player_movement( mut position_query: Query< (Entity, &mut PlayerPosition, &ActionState<PlayerActions>), ( Or<(With<Predicted>, With<DeterministicPredicted>)>, With<PlayerId>, ), >,) { for (_entity, position, action_state) in position_query.iter_mut() { if !action_state.get_pressed().is_empty() { shared_movement_behaviour(position, action_state); } }}
pub(crate) fn player_input_map() -> InputMap<PlayerActions> { InputMap::new([ (PlayerActions::Up, KeyCode::KeyW), (PlayerActions::Down, KeyCode::KeyS), (PlayerActions::Left, KeyCode::KeyA), (PlayerActions::Right, KeyCode::KeyD), ])}
// This runs on both self and other playersfn handle_predicted_spawn( trigger: On<Add, (PlayerId, Predicted)>, mut predicted: Query<&mut PlayerColor, With<Predicted>>,) { let entity = trigger.entity; if let Ok(mut color) = predicted.get_mut(entity) { let hsva = Hsva { saturation: 0.4, ..Hsva::from(color.0) }; color.0 = Color::from(hsva); }}
// Runs on you yourself twinfn handle_controlled_spawn( trigger: On<Add, Controlled>, mut commands: Commands, players: Query<&PlayerId, Without<InputMap<PlayerActions>>>,) { if players.get(trigger.entity).is_err() { return; }; commands.entity(trigger.entity).insert(player_input_map());}
/// Makes other players more desaturatedpub(crate) fn handle_interpolated_spawn( trigger: On<Add, PlayerColor>, mut interpolated: Query<&mut PlayerColor, Added<Interpolated>>,) { if let Ok(mut color) = interpolated.get_mut(trigger.entity) { let hsva = Hsva { saturation: 0.1, ..Hsva::from(color.0) }; color.0 = Color::from(hsva); }}
/// Spawn a client that connects to the serverfn startup( mut commands: Commands, config: Res<ClientStartupConfig>, headless: Res<Headless>,) -> Result { if !headless.0 { commands.spawn(Camera2d); } let client = if let Some(server) = config.host_server { commands .spawn((Client, Name::new("HostClient"), LinkOf { server })) .id() } else { let auth = Authentication::Manual { server_addr: SERVER_ADDR, client_id: config.client_id, private_key: Key::default(), protocol_id: 0, }; commands .spawn(( Client, LocalAddr(CLIENT_ADDR), PeerAddr(SERVER_ADDR), Link::default(), ReplicationReceiver, NetcodeClient::new(auth, NetcodeConfig::default())?, #[cfg(feature = "webtransport")] WebTransportClientIo { certificate_digest: { #[cfg(target_family = "wasm")] { include_str!("../../../certificates/digest.txt").to_string() } #[cfg(not(target_family = "wasm"))] { "".to_string() } }, target: None, }, #[cfg(all( not(feature = "webtransport"), feature = "udp", not(target_family = "wasm") ))] UdpIo::default(), )) .id() }; commands.trigger(Connect { entity: client }); Ok(())}