generated from tipragot/rust
This commit is contained in:
parent
e1a191a539
commit
24ae64f2f3
117
crates/border-wars/src/generation.rs
Normal file
117
crates/border-wars/src/generation.rs
Normal 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 ¤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)
|
||||
}
|
Loading…
Reference in a new issue