mirror of
https://github.com/kristoferssolo/hexlab.git
synced 2025-10-21 19:40:34 +00:00
Merge branch 'refactor/maze'
This commit is contained in:
commit
cb52be461a
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -2063,7 +2063,7 @@ checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hexlab"
|
name = "hexlab"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
"hexx",
|
"hexx",
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "hexlab"
|
name = "hexlab"
|
||||||
authors = ["Kristofers Solo <dev@kristofers.xyz>"]
|
authors = ["Kristofers Solo <dev@kristofers.xyz>"]
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "A hexagonal maze generation and manipulation library"
|
description = "A hexagonal maze generation and manipulation library"
|
||||||
repository = "https://github.com/kristoferssolo/hexlab"
|
repository = "https://github.com/kristoferssolo/hexlab"
|
||||||
|
|||||||
30
src/maze.rs
30
src/maze.rs
@ -19,24 +19,6 @@ impl HexMaze {
|
|||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a hexagonal maze with the given radius
|
|
||||||
/// Uses axial coordinates (q, r) to create a perfect hexagon
|
|
||||||
pub fn with_radius(radius: u32) -> Self {
|
|
||||||
let mut maze = Self::default();
|
|
||||||
let radius = radius as i32;
|
|
||||||
for q in -radius..=radius {
|
|
||||||
let r1 = (-radius).max(-q - radius);
|
|
||||||
let r2 = radius.min(-q + radius);
|
|
||||||
for r in r1..=r2 {
|
|
||||||
let pos = Hex::new(q, r);
|
|
||||||
let tile = HexTile::new(pos);
|
|
||||||
maze.0.insert(pos, tile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
maze
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds a new tile at the specified coordinates
|
/// Adds a new tile at the specified coordinates
|
||||||
pub fn add_tile(&mut self, coords: Hex) {
|
pub fn add_tile(&mut self, coords: Hex) {
|
||||||
let tile = HexTile::new(coords);
|
let tile = HexTile::new(coords);
|
||||||
@ -256,18 +238,6 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn maze_builder() {
|
|
||||||
// Test builder pattern
|
|
||||||
let maze = HexMaze::with_radius(2);
|
|
||||||
|
|
||||||
assert_eq!(maze.len(), 19, "Radius 2 should create 19 hexes");
|
|
||||||
assert!(
|
|
||||||
maze.get_tile(&Hex::ZERO).is_some(),
|
|
||||||
"Center hex should exist"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_maze() {
|
fn empty_maze() {
|
||||||
let maze = HexMaze::default();
|
let maze = HexMaze::default();
|
||||||
|
|||||||
@ -15,8 +15,8 @@ use bevy::prelude::*;
|
|||||||
#[cfg_attr(feature = "bevy", reflect(Component))]
|
#[cfg_attr(feature = "bevy", reflect(Component))]
|
||||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||||
pub struct HexTile {
|
pub struct HexTile {
|
||||||
pub pos: Hex,
|
pub(crate) pos: Hex,
|
||||||
pub walls: Walls,
|
pub(crate) walls: Walls,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HexTile {
|
impl HexTile {
|
||||||
|
|||||||
14
src/walls.rs
14
src/walls.rs
@ -17,14 +17,20 @@ pub struct Walls(u8);
|
|||||||
impl Walls {
|
impl Walls {
|
||||||
/// Adds a wall in the specified direction
|
/// Adds a wall in the specified direction
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn add(&mut self, direction: EdgeDirection) {
|
pub fn add<T>(&mut self, direction: T)
|
||||||
self.0 |= Self::from(direction).0;
|
where
|
||||||
|
T: Into<Self>,
|
||||||
|
{
|
||||||
|
self.0 |= direction.into().0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes a wall in the specified direction
|
/// Removes a wall in the specified direction
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn remove(&mut self, direction: EdgeDirection) {
|
pub fn remove<T>(&mut self, direction: T)
|
||||||
self.0 &= !Self::from(direction).0;
|
where
|
||||||
|
T: Into<Self>,
|
||||||
|
{
|
||||||
|
self.0 &= !direction.into().0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if there is a wall in the specified direction
|
/// Returns true if there is a wall in the specified direction
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user