//! # Server thrust handling //! The bulk of the actual thrust work is done in the thrust solver on the client. //! It sends us it's `ThrustSolution` when it's done; in this file we process it //! and apply it to the physics simulation. use crate::attachment::Parts; use crate::ecs::{Part, Temperature}; use crate::ecs::thruster::{Thruster, ThrusterOfPart}; use crate::prelude::*; use crate::server::ConnectedNetworkEntity; use crate::thrust::ThrustSolution; pub fn server_thrust_plugin(app: &mut App) { app .add_systems(Update, process_thrust_events) .add_systems(Update, apply_thrust_solutions); } /// Handle new `ThrustSolution`s from clients as they come in fn process_thrust_events( mut events: MessageReader>, clients: Query<&ConnectedNetworkEntity>, q_ls_me: Query>, mut commands: Commands ) { // For each event from a client... for FromClient { client_id, message: thrust_solution, } in events.read() { // Find the hearty entity of the player... let player_hearty_entity = match client_id { ClientId::Client(client_entity) => { let ConnectedNetworkEntity { game_entity: player_hearty_entity, } = clients.get(*client_entity).unwrap(); player_hearty_entity }, ClientId::Server => &q_ls_me.iter().next().unwrap() }; // and apply the new thrust solution commands.entity(*player_hearty_entity).insert(thrust_solution.clone()); trace!("installed thrust solution {:?}", thrust_solution); } } /// Find all players, and apply their current `ThrustSolution`s fn apply_thrust_solutions( players: Query<(Entity, &ThrustSolution, Option<&Parts>)>, thrusters: Query<(&Thruster, &ThrusterOfPart, &GlobalTransform)>, mut parts: Query<(Forces, &mut Temperature), With>, time: Res