~starkingdoms/starkingdoms

ref: aa66f9e7ccc943f231bba5bdd9d58d46d935a056 starkingdoms/crates/unified/src/client/net.rs -rw-r--r-- 750 bytes
aa66f9e7ghostly_zsh working on making the game work with this networking 9 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use crate::{prelude::*, shared::net::{ServerEntityMap, SpawnEntity}};

pub fn net_plugin(app: &mut App) {
    app
        .insert_resource(ServerEntityMap::default())
        .add_systems(PostUpdate, spawn_server_entities);
}

fn spawn_server_entities(
    mut spawn_entity_messages: MessageReader<SpawnEntity>,
    mut entity_mapper: ResMut<ServerEntityMap>,
    mut commands: Commands,
) {
    for spawn_entity in spawn_entity_messages.read() {
        let entity = commands.spawn_empty();
        debug!("client: {:?}, server: {:?}", entity.id(), spawn_entity.server);
        entity_mapper.server_to_client.set_mapped(spawn_entity.server, entity.id());
        entity_mapper.client_to_server.set_mapped(entity.id(), spawn_entity.server);
    }
}