From 49976391ec39da1401c9454b3f73ba9d525b68db Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Thu, 4 Apr 2024 20:57:57 +0200 Subject: [PATCH] save --- crates/border-wars/src/main.rs | 4 +-- crates/border-wars/src/map/spawnpoint.rs | 45 ++++++++++-------------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/crates/border-wars/src/main.rs b/crates/border-wars/src/main.rs index 3428129..b6b2301 100644 --- a/crates/border-wars/src/main.rs +++ b/crates/border-wars/src/main.rs @@ -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(); } diff --git a/crates/border-wars/src/map/spawnpoint.rs b/crates/border-wars/src/map/spawnpoint.rs index 0b46769..9bfff25 100644 --- a/crates/border-wars/src/map/spawnpoint.rs +++ b/crates/border-wars/src/map/spawnpoint.rs @@ -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::>(); - 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 - } -}