diff --git a/crates/border-wars/src/main.rs b/crates/border-wars/src/main.rs index 8019933..d5a1f1b 100644 --- a/crates/border-wars/src/main.rs +++ b/crates/border-wars/src/main.rs @@ -5,6 +5,7 @@ use border_wars::camera::CameraPlugin; use border_wars::map::generation::MapGenerationPlugin; use border_wars::map::renderer::RendererPlugin; use border_wars::map::selected_tile::SelectTilePlugin; +use border_wars::map::spawnpoint::SpawnPointPlugin; use border_wars::networking::NetworkingPlugin; use border_wars::scenes::ScenesPlugin; use border_wars::ui::UiPlugin; @@ -19,5 +20,6 @@ fn main() { .add_plugins(NetworkingPlugin) .add_plugins(MapGenerationPlugin) .add_plugins(UiPlugin) + .add_plugins(SpawnPointPlugin) .run(); } diff --git a/crates/border-wars/src/map/mod.rs b/crates/border-wars/src/map/mod.rs index 839b7e8..c11dc8e 100644 --- a/crates/border-wars/src/map/mod.rs +++ b/crates/border-wars/src/map/mod.rs @@ -4,6 +4,7 @@ pub mod generation; pub mod hex; pub mod renderer; pub mod selected_tile; +pub mod spawnpoint; use bevy::prelude::*; diff --git a/crates/border-wars/src/map/spawnpoint.rs b/crates/border-wars/src/map/spawnpoint.rs new file mode 100644 index 0000000..c926855 --- /dev/null +++ b/crates/border-wars/src/map/spawnpoint.rs @@ -0,0 +1,55 @@ +//! TODO + +use bevy::prelude::*; +use bevy::utils::hashbrown::HashSet; +use bevy::utils::HashMap; + +use super::generation::EndMapGeneration; +use super::TilePosition; +use crate::Player; + +/// The plugin for the spawn point. +pub struct SpawnPointPlugin; + +impl Plugin for SpawnPointPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Update, init_spawn_point); + } +} + +/// Initialize the spawn point when the map is generated. +fn init_spawn_point( + mut commands: Commands, + mut end_map_event: EventReader, + players: Query<(Entity, &Player)>, + map: Query<(Entity, &TilePosition)>, +) { + if end_map_event.is_empty() { + return; + } + + let radius = map + .iter() + .max_by(|(_, a), (_, b)| a.0.cmp(&b.0)) + .unwrap() + .1 + .0 + .abs(); + + let map_hashmap: HashMap<&TilePosition, Entity> = map.iter().map(|(e, p)| (p, e)).collect(); + + let nb_player = players.iter().count(); + for (i, target_position) in TilePosition::new(0, 0) + .ring(radius as usize / 2) + .enumerate() + { + let Some(target_entity) = map_hashmap.get(&target_position) else { + return; + }; + + if i % (radius as usize * 3 / nb_player) != 0 { + continue; + } + commands.entity(*target_entity).despawn(); + } +} diff --git a/crates/border-wars/src/scenes/lobby.rs b/crates/border-wars/src/scenes/lobby.rs index d3762cc..8ce1de7 100644 --- a/crates/border-wars/src/scenes/lobby.rs +++ b/crates/border-wars/src/scenes/lobby.rs @@ -69,17 +69,17 @@ fn lobby_ui( return; } - ui.add(egui::Slider::new(&mut (*map_size), 1..=5).text("map size")); + ui.add(egui::Slider::new(&mut (*map_size), 1..=3).text("map size")); if !ui.button("Run the game").clicked() { return; } let seed = rand::thread_rng().gen::(); - let index = *map_size as usize; + let index = *map_size as u16; let nomber_of_players = all_players_query.iter().count() as u32; - let radius = get_map_sizes(nomber_of_players)[index] as u16 * 2; + let radius = nomber_of_players as u16 * 2 * index; // Start the game. for player in all_players_query.iter() { @@ -90,19 +90,3 @@ fn lobby_ui( } }); } - -/// Get the map sizes form a given number of players. -fn get_map_sizes(number_of_players: u32) -> Vec { - let mut result = Vec::with_capacity(6); - - let mut current = 0; - while result.len() < 6 { - current += 1; - - if (current * 6) % number_of_players == 0 { - result.push(current); - } - } - - result -}