From 24ae64f2f33968f1514e6ce73b5db70acf087cc1 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Tue, 13 Feb 2024 01:23:23 +0100 Subject: [PATCH] save --- crates/border-wars/src/generation.rs | 117 +++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 crates/border-wars/src/generation.rs diff --git a/crates/border-wars/src/generation.rs b/crates/border-wars/src/generation.rs new file mode 100644 index 0000000..20d67c4 --- /dev/null +++ b/crates/border-wars/src/generation.rs @@ -0,0 +1,117 @@ +//! TODO + +use std::collections::{HashMap, LinkedList}; + +use bevy::prelude::*; + +/// TODO +#[derive(Component)] +struct Tile; + +/// TODO +#[derive(Component, Eq, PartialEq, Hash, Clone, Copy)] +struct TilePosition { + /// TODO + q: i32, + + /// TODO + r: i32, +} + +impl TilePosition { + /// TODO + pub fn get_s(&self) -> i32 { + -self.r - self.q + } +} + +/// TODO +struct AdjacentsPositions { + top_left: TilePosition, + top_right: TilePosition, + bottom_left: TilePosition, + bottom_right: TilePosition, + left: TilePosition, + right: TilePosition, +} + +impl AdjacentsPositions { + /// TODO + pub fn new_from(pos: TilePosition) -> Self { + Self { + top_left: TilePosition { + q: pos.q, + r: pos.r - 1, + }, + top_right: TilePosition { + q: pos.q + 1, + r: pos.r - 1, + }, + bottom_left: TilePosition { + q: pos.q - 1, + r: pos.r + 1, + }, + bottom_right: TilePosition { + q: pos.q, + r: pos.r + 1, + }, + left: TilePosition { + q: pos.q - 1, + r: pos.r, + }, + right: TilePosition { + q: pos.q + 1, + r: pos.r, + }, + } + } +} + +/// TODO +fn create_adjacent_tiles( + commands: &mut Commands, + tile_pos: TilePosition, + tiles: &mut HashMap, + new_tiles: &mut LinkedList, +) { + let _entity = *tiles + .entry(tile_pos) + .or_insert_with(|| commands.spawn((Tile, tile_pos)).id()); + + new_tiles.push_back(tile_pos); +} + +fn test(mut commands: Commands, raduis: u32) { + let mut tiles = HashMap::new(); + + // TODO + let origin_pos = TilePosition { q: 0, r: 0 }; + tiles.insert(origin_pos.clone(), commands.spawn((Tile, origin_pos)).id()); + + let mut current = LinkedList::from([origin_pos]); + let mut new_tiles = LinkedList::from([origin_pos]); + + for _i in 0..raduis { + for ¤t_pos in current.iter() { + let adjacents = AdjacentsPositions::new_from(current_pos); + + for adjacent_pos in [ + adjacents.top_left, + adjacents.top_right, + adjacents.bottom_left, + adjacents.bottom_right, + adjacents.left, + adjacents.right, + ] { + create_adjacent_tiles(&mut commands, adjacent_pos, &mut tiles, &mut new_tiles); + } + } + + current.clear(); + std::mem::swap(&mut current, &mut new_tiles); + } +} + +pub fn setup(commands: Commands) { + test(commands, 2) +}