generated from tipragot/rust
This commit is contained in:
parent
82a77dbdbb
commit
6a3723294e
163
crates/border-wars/src/generation.rs
Normal file
163
crates/border-wars/src/generation.rs
Normal file
|
@ -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<Inverse = Self>;
|
||||
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<Direction: TileDirection>(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::<TopLeftTile>(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),
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub mod generation;
|
||||
pub mod scenes;
|
||||
|
||||
/// The current scene of the game.
|
||||
|
|
Loading…
Reference in a new issue