~starkingdoms/starkingdoms

ref: eabc84c8e290163d25ea21f153ae811bf09fc23a starkingdoms/crates/unified/src/server/priority.rs -rw-r--r-- 1.0 KiB
eabc84c8 — core feat: add basic save file authentication infrastructure a day 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
use bevy_replicon::prelude::{AuthorizedClient, PriorityMap};
use crate::prelude::*;
use crate::server::ConnectedNetworkEntity;
use crate::shared::attachment::Parts;
use crate::shared::ecs::{Part, Player};

const OTHER_SHIP_PART_PRIORITY: f32 = 1.0;

pub fn replication_priority_plugin(app: &mut App) {
    app.add_systems(Update, prioritize_own_ship_parts);
}

fn prioritize_own_ship_parts(
    mut clients: Query<(&ConnectedNetworkEntity, &mut PriorityMap), With<AuthorizedClient>>,
    hearties: Query<Option<&Parts>, With<Player>>,
    parts: Query<Entity, With<Part>>,
) {
    for (connected, mut priority) in &mut clients {
        let own_parts = hearties.get(connected.game_entity).ok().flatten();

        for part in &parts {
            let is_own = part == connected.game_entity
                || own_parts.is_some_and(|owned| owned.contains(&part));

            if is_own {
                priority.remove(&part);
            } else {
                priority.insert(part, OTHER_SHIP_PART_PRIORITY);
            }
        }
    }
}