From 6a3723294ed8bb4f15c82081a81aeba3fa792795 Mon Sep 17 00:00:00 2001 From: Tipragot Date: Mon, 12 Feb 2024 19:17:19 +0100 Subject: [PATCH] save --- crates/border-wars/src/generation.rs | 163 +++++++++++++++++++++++++++ crates/border-wars/src/lib.rs | 1 + 2 files changed, 164 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..223bb13 --- /dev/null +++ b/crates/border-wars/src/generation.rs @@ -0,0 +1,163 @@ +//! TODO + +use std::collections::hash_map::Entry; +use std::collections::{HashMap, HashSet, LinkedList}; + +use bevy::prelude::*; + +/// TODO +#[derive(Component)] +pub struct Tile; + +pub trait TileDirection: Component { + type Inverse: TileDirection; + fn new(entity: Entity) -> Self; +} + +/// TODO +#[derive(Component)] +pub struct TopLeftTile(pub Entity); + +impl TileDirection for TopLeftTile { + type Inverse = BottomRightTile; + + fn new(entity: Entity) -> Self { + Self(entity) + } +} + +/// TODO +#[derive(Component)] +pub struct TopRightTile(pub Entity); + +impl TileDirection for TopRightTile { + type Inverse = BottomLeftTile; + + fn new(entity: Entity) -> Self { + Self(entity) + } +} + +/// TODO +#[derive(Component)] +pub struct LeftTile(pub Entity); + +impl TileDirection for LeftTile { + type Inverse = RightTile; + + fn new(entity: Entity) -> Self { + Self(entity) + } +} + +/// TODO +#[derive(Component)] +pub struct RightTile(pub Entity); + +impl TileDirection for RightTile { + type Inverse = LeftTile; + + fn new(entity: Entity) -> Self { + Self(entity) + } +} + +/// TODO +#[derive(Component)] +pub struct BottomLeftTile(pub Entity); + +impl TileDirection for BottomLeftTile { + type Inverse = TopRightTile; + + fn new(entity: Entity) -> Self { + Self(entity) + } +} + +/// TODO +#[derive(Component)] +pub struct BottomRightTile(pub Entity); + +impl TileDirection for BottomRightTile { + type Inverse = TopLeftTile; + + fn new(entity: Entity) -> Self { + Self(entity) + } +} + +/// TODO +fn link_tiles(commands: &mut Commands, from: Entity, to: Entity) { + commands.entity(from).insert(Direction::new(to)); + commands.entity(to).insert(Direction::Inverse::new(from)); +} + +/// TODO +fn generate_circle(commands: &mut Commands, radius: u32) { + let mut old_tiles = HashSet::new(); + let mut tiles = HashMap::new(); + tiles.insert((0, 0), commands.spawn(Tile).id()); + let mut new_tiles = LinkedList::from([(0, 0)]); + let mut current = LinkedList::from([(0, 0)]); + for i in 0..radius { + for &(x, y) in current.iter() { + let AdjacentPositions { + top_left, + top_right, + left, + right, + bottom_left, + bottom_right, + } = get_adjacent_positions(x, y); + + // Top left + if !old_tiles.contains(&top_left) { + let top_left_entity = *tiles + .entry(top_left) + .or_insert_with(|| commands.spawn(Tile).id()); + link_tiles::(commands, top_left_entity, tiles[&(x, y)]); + } + + new_tiles.push_back(top_left); + } + for &(x, y) in current.iter() { + old_tiles.insert((x, y)); + } + current.clear(); + std::mem::swap(&mut new_tiles, &mut current); + } +} + +/// TODO +struct AdjacentPositions { + /// TODO + top_left: (i32, i32), + + /// TODO + top_right: (i32, i32), + + /// TODO + left: (i32, i32), + + /// TODO + right: (i32, i32), + + /// TODO + bottom_left: (i32, i32), + + /// TODO + bottom_right: (i32, i32), +} + +/// TODO +const fn get_adjacent_positions(x: i32, y: i32) -> AdjacentPositions { + let offset: i32 = y % 2; + AdjacentPositions { + left: (x - 1, y), + right: (x + 1, y), + top_left: (x - offset, y + 1), + top_right: (x - offset + 1, y + 1), + bottom_left: (x - offset, y - 1), + bottom_right: (x - offset + 1, y - 1), + } +} diff --git a/crates/border-wars/src/lib.rs b/crates/border-wars/src/lib.rs index c85940e..8f91785 100644 --- a/crates/border-wars/src/lib.rs +++ b/crates/border-wars/src/lib.rs @@ -2,6 +2,7 @@ use bevy::prelude::*; +pub mod generation; pub mod scenes; /// The current scene of the game.