WIP: Add a selection of the spawnpoint for every players #108

Draft
CoCo_Sol wants to merge 12 commits from select-spawnpoint into main
Showing only changes of commit ad4c55678a - Show all commits

View file

@ -1,5 +1,6 @@
//! TODO //! TODO
use bevnet::Connection;
use bevy::prelude::*; use bevy::prelude::*;
use bevy::utils::hashbrown::HashSet; use bevy::utils::hashbrown::HashSet;
use bevy::utils::HashMap; use bevy::utils::HashMap;
@ -23,6 +24,7 @@ fn init_spawn_point(
mut end_map_event: EventReader<EndMapGeneration>, mut end_map_event: EventReader<EndMapGeneration>,
players: Query<(Entity, &Player)>, players: Query<(Entity, &Player)>,
map: Query<(Entity, &TilePosition)>, map: Query<(Entity, &TilePosition)>,
connection: Res<Connection>,
) { ) {
if end_map_event.is_empty() { if end_map_event.is_empty() {
return; return;
@ -36,9 +38,16 @@ fn init_spawn_point(
.0 .0
.abs(); .abs();
if radius == 0 {
panic!("Map radius must be greater than 0");
}
let map_hashmap: HashMap<&TilePosition, Entity> = map.iter().map(|(e, p)| (p, e)).collect(); let map_hashmap: HashMap<&TilePosition, Entity> = map.iter().map(|(e, p)| (p, e)).collect();
let nb_player = players.iter().count(); let nb_player = players.iter().count();
let mut spawnpoints = Vec::with_capacity(nb_player);
for (i, target_position) in TilePosition::new(0, 0) for (i, target_position) in TilePosition::new(0, 0)
.ring(radius as usize / 2) .ring(radius as usize / 2)
.enumerate() .enumerate()
@ -50,6 +59,35 @@ fn init_spawn_point(
if i % (radius as usize * 3 / nb_player) != 0 { if i % (radius as usize * 3 / nb_player) != 0 {
continue; continue;
} }
spawnpoints.push((*target_entity, target_position));
}
let mut sorted_players = players.iter().collect::<Vec<_>>();
sorted_players.sort_by(compare_player);
spawnpoints.sort_by(compare_spawnpoint_entity);
for (i, (target_entity, target_position)) in spawnpoints.iter().enumerate() {
let player = sorted_players[i].1;
if Some(player.uuid) == connection.identifier() {
commands.entity(*target_entity).despawn(); commands.entity(*target_entity).despawn();
} }
}
}
/// TODO
fn compare_player((_, a): &(Entity, &Player), (_, b): &(Entity, &Player)) -> std::cmp::Ordering {
a.uuid.cmp(&b.uuid)
}
/// TODO
fn compare_spawnpoint_entity(
(_, a): &(Entity, TilePosition),
(_, b): &(Entity, TilePosition),
) -> std::cmp::Ordering {
let r = a.0.abs().cmp(&b.0.abs());
if r == std::cmp::Ordering::Equal {
a.1.cmp(&b.1)
} else {
r
}
} }