~starkingdoms/starkingdoms

ref: 302ee06fb6c522c2bf5d6908329d24a72dffa671 starkingdoms/crates/unified/src/client/net/incoming_planets.rs -rw-r--r-- 1.4 KiB
302ee06f — core netcode: fix up PlanetDto and try to figure out flickering 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
use bevy::log::{debug, warn};
use bevy::prelude::{Commands, MessageReader, ResMut, Transform};
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 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)) = 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);
        } else {
            // Spawn new planet
            let e = commands.spawn((
                planet.planet.data().clone(),
                *planet.transform.data(),
            )).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");
        }

    }
}