use avian2d::prelude::{AngularVelocity, LinearVelocity};
use bevy::log::{debug, warn};
use bevy::prelude::{Commands, MessageReader, ResMut, Transform};
use bevy_transform_interpolation::hermite::TransformHermiteEasing;
use bevy_transform_interpolation::interpolation::TransformInterpolation;
use crate::client::net::AngularVelocitySource;
use crate::prelude::Query;
use crate::shared::config::planet::Planet;
use crate::shared::net::planet::{PlanetDto};
use crate::shared::net::ServerEntityMap;
pub fn handle_incoming_planets(
mut msgs: MessageReader<PlanetDto>,
mut q_planets: Query<(&mut Planet, &mut Transform, &mut LinearVelocity, &mut AngularVelocity)>,
mut entity_map: ResMut<ServerEntityMap>,
mut commands: Commands
) {
for planet in msgs.read() {
if let Some(local_entity) = entity_map.server_to_client.get(&planet.server_entity) {
let Ok((mut planet_data, mut transform, mut linvel, mut angvel)) = q_planets.get_mut(*local_entity) else {
warn!("local planet entity {:?} for planet srv:{:?} doesn't exist? skipping update, this is a bug", local_entity, planet.server_entity);
continue;
};
planet.planet.update(planet_data);
planet.transform.update(transform);
planet.linvel.update(linvel);
planet.angvel.update(angvel);
} else {
// Spawn new planet
let e = commands.spawn((
planet.planet.data().clone(),
*planet.transform.data(),
*planet.linvel.data(),
*planet.angvel.data(),
TransformInterpolation,
//TransformHermiteEasing
)).id();
entity_map.server_to_client.insert(planet.server_entity, e.clone());
entity_map.client_to_server.insert(e.clone(), planet.server_entity);
debug!(?planet.planet, "spawned new planet");
}
}
}