save
All checks were successful
Rust Checks / checks (push) Successful in 2m43s

This commit is contained in:
CoCo_Sol 2024-02-13 01:23:23 +01:00
parent e1a191a539
commit 24ae64f2f3

View file

@ -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<TilePosition, Entity>,
new_tiles: &mut LinkedList<TilePosition>,
) {
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 &current_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)
}