~starkingdoms/starkingdoms

ref: c7da20800d035a6491e612693b2039aa99988c15 starkingdoms/crates/unified/src/client/net/incoming_planets.rs -rw-r--r-- 1.9 KiB
c7da2080 — core netcode: easing 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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");
        }

    }
}