//! # Server thrust handling //! The thrust solver runs on the client and sends a `ThrustSolution` message; //! this file receives it and applies it to the physics simulation. use bevy_replicon::prelude::{ClientId, FromClient}; use crate::client::components::Me; use crate::shared::ecs::{Part, Temperature}; use crate::shared::ecs::thruster::{Thruster, ThrusterOfPart}; use crate::prelude::*; use crate::server::ConnectedNetworkEntity; use crate::shared::attachment::Parts; use crate::shared::thrust::ThrustSolution; pub fn server_thrust_plugin(app: &mut App) { app .add_systems(Update, process_thrust_events) .add_systems(FixedUpdate, apply_thrust_solutions); } fn process_thrust_events( mut events: MessageReader>, q_ls_me: Query>, clients: Query<&ConnectedNetworkEntity>, mut commands: Commands, ) { for thrust_solution in events.read() { let player_entity = match thrust_solution.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() }; commands.entity(player_entity).insert(thrust_solution.message.clone()); } } /// 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