save
All checks were successful
Rust Checks / checks (push) Successful in 3m2s
Rust Checks / checks (pull_request) Successful in 2m38s

This commit is contained in:
CoCo_Sol 2024-04-04 20:57:57 +02:00
parent 4c0bb12d96
commit 49976391ec
2 changed files with 21 additions and 28 deletions

View file

@ -6,7 +6,7 @@ use border_wars::map::generation::MapGenerationPlugin;
use border_wars::map::ownership::OwnershipPlugin;
use border_wars::map::renderer::RendererPlugin;
use border_wars::map::selected_tile::SelectTilePlugin;
use border_wars::map::spawnpoint::SpawnPointPlugin;
use border_wars::map::spawnpoint::SpawnpointPlugin;
use border_wars::networking::NetworkingPlugin;
use border_wars::scenes::ScenesPlugin;
use border_wars::ui::UiPlugin;
@ -21,7 +21,7 @@ fn main() {
.add_plugins(NetworkingPlugin)
.add_plugins(MapGenerationPlugin)
.add_plugins(UiPlugin)
.add_plugins(SpawnPointPlugin)
.add_plugins(SpawnpointPlugin)
.add_plugins(OwnershipPlugin)
.run();
}

View file

@ -1,4 +1,4 @@
//! TODO
//! All code related to set the spawn point of the players.
use bevy::prelude::*;
@ -7,10 +7,10 @@ use super::ownership::Owner;
use super::{Tile, TilePosition};
use crate::Player;
/// The plugin for the spawn point.
pub struct SpawnPointPlugin;
/// The plugin that initialize the spawn point at the start of the game.
pub struct SpawnpointPlugin;
impl Plugin for SpawnPointPlugin {
impl Plugin for SpawnpointPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, init_spawn_point);
}
@ -24,53 +24,46 @@ fn init_spawn_point(
mut map: Query<(Entity, &TilePosition, &mut Tile)>,
) {
for _ in end_map_event.read() {
// Calculate the radius of the map.
let Some(radius) = map.iter().map(|(_, p, _)| p.0.abs()).max() else {
warn!("The map is empty");
return;
};
if radius == 0 {
warn!("The map radius is 0 ");
warn!("The map is empty");
return;
}
let mut sorted_players = players.iter().collect::<Vec<_>>();
sorted_players.sort_by(|a: &&Player, b: &&Player| compare_player(a, b));
sorted_players.sort_by(|a: &&Player, b: &&Player| a.uuid.cmp(&b.uuid));
let mut sorted_players = sorted_players.iter();
let number_players = sorted_players.len();
let interval = radius as usize * 3 / sorted_players.len();
for (i, target_pos) in TilePosition::new(0, 0)
for (i, position) in TilePosition::new(0, 0)
.ring(radius as usize / 2)
.enumerate()
{
let Some((entity, _, mut tile)) = map.iter_mut().find(|(_, p, _)| **p == target_pos)
// Find the target tile.
let Some((entity, _, mut tile)) = map.iter_mut().find(|(_, p, _)| **p == position)
else {
continue;
};
if i % ((radius as usize * 3) / number_players) != 0 {
// Check the interval between players.
if i % interval != 0 {
continue;
}
*tile = Tile::Castle;
// Get the current player.
let Some(player) = sorted_players.next() else {
continue;
};
// Set the spawn point.
*tile = Tile::Castle;
commands.entity(entity).insert(Owner(Player::clone(player)));
}
}
}
/// TODO
fn compare_player(a: &Player, b: &Player) -> std::cmp::Ordering {
a.uuid.cmp(&b.uuid)
}
/// TODO
fn compare_spawnpoint_entity(a: &TilePosition, b: &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
}
}