mirror of
https://github.com/kristoferssolo/hexlab.git
synced 2025-10-21 19:40:34 +00:00
docs: update
This commit is contained in:
parent
83f2e47e27
commit
cd4f369108
180
src/builder.rs
180
src/builder.rs
@ -24,60 +24,59 @@ pub enum MazeBuilderError {
|
|||||||
GenerationError(String),
|
GenerationError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// A builder pattern for creating hexagonal mazes.
|
||||||
A builder pattern for creating hexagonal mazes.
|
///
|
||||||
|
/// This struct provides a fluent interface for configuring and building hexagonal mazes.
|
||||||
This struct provides a fluent interface for configuring and building hexagonal mazes.
|
/// It offers flexibility in specifying the maze size, random seed, generation algorithm,
|
||||||
It offers flexibility in specifying the maze size, random seed, and generation algorithm.
|
/// and starting position.
|
||||||
|
///
|
||||||
# Examples
|
/// # Examples
|
||||||
|
///
|
||||||
Basic usage:
|
/// Basic usage:
|
||||||
```
|
/// ```
|
||||||
use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
|
///
|
||||||
let maze = MazeBuilder::new()
|
/// let maze = MazeBuilder::new()
|
||||||
.with_radius(5)
|
/// .with_radius(5)
|
||||||
.build()
|
/// .build()
|
||||||
.expect("Failed to create maze");
|
/// .expect("Failed to create maze");
|
||||||
|
///
|
||||||
// A radius of 5 creates 61 hexagonal tiles
|
/// // A radius of 5 creates 61 hexagonal tiles
|
||||||
assert!(!maze.is_empty());
|
/// assert!(!maze.is_empty());
|
||||||
assert_eq!(maze.len(), 91);
|
/// assert_eq!(maze.len(), 91);
|
||||||
```
|
/// ```
|
||||||
|
///
|
||||||
Using a seed for reproducible results:
|
/// Using a seed for reproducible results:
|
||||||
```
|
/// ```
|
||||||
use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
|
///
|
||||||
let maze1 = MazeBuilder::new()
|
/// let maze1 = MazeBuilder::new()
|
||||||
.with_radius(3)
|
/// .with_radius(3)
|
||||||
.with_seed(12345)
|
/// .with_seed(12345)
|
||||||
.build()
|
/// .build()
|
||||||
.expect("Failed to create maze");
|
/// .expect("Failed to create maze");
|
||||||
|
///
|
||||||
let maze2 = MazeBuilder::new()
|
/// let maze2 = MazeBuilder::new()
|
||||||
.with_radius(3)
|
/// .with_radius(3)
|
||||||
.with_seed(12345)
|
/// .with_seed(12345)
|
||||||
.build()
|
/// .build()
|
||||||
.expect("Failed to create maze");
|
/// .expect("Failed to create maze");
|
||||||
|
///
|
||||||
// Same seed should produce identical mazes
|
/// // Same seed should produce identical mazes
|
||||||
assert_eq!(maze1.len(), maze2.len());
|
/// assert_eq!(maze1.len(), maze2.len());
|
||||||
assert_eq!(maze1, maze2);
|
/// assert_eq!(maze1, maze2);
|
||||||
```
|
/// ```
|
||||||
|
///
|
||||||
Specifying a custom generator:
|
/// Specifying a custom generator:
|
||||||
```
|
/// ```
|
||||||
use hexlab::prelude::*;
|
/// use hexlab::prelude::*;
|
||||||
|
///
|
||||||
let maze = MazeBuilder::new()
|
/// let maze = MazeBuilder::new()
|
||||||
.with_radius(7)
|
/// .with_radius(7)
|
||||||
.with_generator(GeneratorType::RecursiveBacktracking)
|
/// .with_generator(GeneratorType::RecursiveBacktracking)
|
||||||
.build()
|
/// .build()
|
||||||
.expect("Failed to create maze");
|
/// .expect("Failed to create maze");
|
||||||
```
|
/// ```
|
||||||
*/
|
|
||||||
#[allow(clippy::module_name_repetitions)]
|
#[allow(clippy::module_name_repetitions)]
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct MazeBuilder {
|
pub struct MazeBuilder {
|
||||||
@ -88,7 +87,7 @@ pub struct MazeBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MazeBuilder {
|
impl MazeBuilder {
|
||||||
/// Creates a new [`MazeBuilder`] instance.
|
/// Creates a new [`MazeBuilder`] instance with default settings.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
@ -97,9 +96,14 @@ impl MazeBuilder {
|
|||||||
|
|
||||||
/// Sets the radius for the hexagonal maze.
|
/// Sets the radius for the hexagonal maze.
|
||||||
///
|
///
|
||||||
|
/// The radius determines the size of the maze, specifically the number of tiles
|
||||||
|
/// from the center (0,0) to the edge of the hexagon, not including the center tile.
|
||||||
|
/// For example, a radius of 3 would create a maze with 3 tiles from center to edge,
|
||||||
|
/// resulting in a total diameter of 7 tiles (3 + 1 + 3).
|
||||||
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `radius` - The size of the maze (number of tiles along one edge).
|
/// - `radius` - The number of tiles from the center to the edge of the hexagon.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn with_radius(mut self, radius: u32) -> Self {
|
pub const fn with_radius(mut self, radius: u32) -> Self {
|
||||||
@ -109,9 +113,11 @@ impl MazeBuilder {
|
|||||||
|
|
||||||
/// Sets the random seed for maze generation.
|
/// Sets the random seed for maze generation.
|
||||||
///
|
///
|
||||||
|
/// Using the same seed will produce identical mazes, allowing for reproducible results.
|
||||||
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `seed` - The random seed value.
|
/// - `seed` - The random seed value.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn with_seed(mut self, seed: u64) -> Self {
|
pub const fn with_seed(mut self, seed: u64) -> Self {
|
||||||
@ -125,14 +131,18 @@ impl MazeBuilder {
|
|||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `generator_type` - The maze generation algorithm to use.
|
/// - `generator_type` - The maze generation algorithm to use.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn with_generator(mut self, generator_type: GeneratorType) -> Self {
|
pub const fn with_generator(mut self, generator_type: GeneratorType) -> Self {
|
||||||
self.generator_type = generator_type;
|
self.generator_type = generator_type;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
/// Sets the starting position for maze generation.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `pos` - The hexagonal coordinates for the starting position.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn with_start_position(mut self, pos: Hex) -> Self {
|
pub const fn with_start_position(mut self, pos: Hex) -> Self {
|
||||||
@ -140,33 +150,31 @@ impl MazeBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Builds the hexagonal maze based on the configured parameters.
|
||||||
Builds the hexagonal maze based on the configured parameters.
|
///
|
||||||
|
/// # Errors
|
||||||
# Errors
|
///
|
||||||
|
/// Returns [`MazeBuilderError::NoRadius`] if no radius is specified.
|
||||||
Returns [`MazeBuilderError::NoRadius`] if no radius is specified.
|
/// Returns [`MazeBuilderError::InvalidStartPosition`] if the start position is outside maze bounds.
|
||||||
Returns [`MazeBuilderError::InvalidStartPosition`] if the start position is outside maze bounds.
|
///
|
||||||
|
/// # Examples
|
||||||
# Examples
|
///
|
||||||
|
/// ```
|
||||||
```
|
/// use hexlab::prelude::*;
|
||||||
use hexlab::prelude::*;
|
///
|
||||||
|
/// // Should fail without radius
|
||||||
// Should fail without radius
|
/// let result = MazeBuilder::new().build();
|
||||||
let result = MazeBuilder::new().build();
|
/// assert!(result.is_err());
|
||||||
assert!(result.is_err());
|
///
|
||||||
|
/// // Should succeed with radius
|
||||||
// Should succeed with radius
|
/// let result = MazeBuilder::new()
|
||||||
let result = MazeBuilder::new()
|
/// .with_radius(3)
|
||||||
.with_radius(3)
|
/// .build();
|
||||||
.build();
|
/// assert!(result.is_ok());
|
||||||
assert!(result.is_ok());
|
///
|
||||||
|
/// let maze = result.unwrap();
|
||||||
let maze = result.unwrap();
|
/// assert!(!maze.is_empty());
|
||||||
assert!(!maze.is_empty());
|
/// ```
|
||||||
```
|
|
||||||
*/
|
|
||||||
pub fn build(self) -> Result<HexMaze, MazeBuilderError> {
|
pub fn build(self) -> Result<HexMaze, MazeBuilderError> {
|
||||||
let radius = self.radius.ok_or(MazeBuilderError::NoRadius)?;
|
let radius = self.radius.ok_or(MazeBuilderError::NoRadius)?;
|
||||||
let mut maze = create_hex_maze(radius);
|
let mut maze = create_hex_maze(radius);
|
||||||
|
|||||||
@ -8,7 +8,10 @@ use hexx::{EdgeDirection, Hex};
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
/// Represents a hexagonal maze with tiles and walls
|
/// Represents a hexagonal maze with tiles and walls.
|
||||||
|
///
|
||||||
|
/// This struct stores the layout of a hexagonal maze, including the positions
|
||||||
|
/// of tiles and their associated walls.
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
|
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
|
||||||
#[cfg_attr(feature = "bevy", derive(Component))]
|
#[cfg_attr(feature = "bevy", derive(Component))]
|
||||||
@ -25,44 +28,67 @@ impl HexMaze {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a new tile at the specified coordinates
|
/// Adds a new tile at the specified coordinates
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `coords` - The hexagonal coordinates where the tile should be added.
|
||||||
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);
|
||||||
self.0.insert(coords, tile);
|
self.0.insert(coords, tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a wall in the specified direction at the given coordinates
|
/// Adds a wall in the specified direction at the given coordinates.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `coord` - The hexagonal coordinates of the tile.
|
||||||
|
/// - `direction` - The direction in which to add the wall.
|
||||||
pub fn add_wall(&mut self, coord: Hex, direction: EdgeDirection) {
|
pub fn add_wall(&mut self, coord: Hex, direction: EdgeDirection) {
|
||||||
if let Some(tile) = self.0.get_mut(&coord) {
|
if let Some(tile) = self.0.get_mut(&coord) {
|
||||||
tile.walls.add(direction);
|
tile.walls.add(direction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a reference to the tile at the specified coordinates
|
/// Returns a reference to the tile at the specified coordinates.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `coord` - The hexagonal coordinates of the tile to retrieve.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn get_tile(&self, coord: &Hex) -> Option<&HexTile> {
|
pub fn get_tile(&self, coord: &Hex) -> Option<&HexTile> {
|
||||||
self.0.get(coord)
|
self.0.get(coord)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a reference to the walls at the specified coordinates
|
/// Returns an optional reference to the walls at the specified coordinates.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `coord` - The hexagonal coordinates of the tile whose walls to retrieve.
|
||||||
pub fn get_walls(&self, coord: &Hex) -> Option<&Walls> {
|
pub fn get_walls(&self, coord: &Hex) -> Option<&Walls> {
|
||||||
self.0.get(coord).map(HexTile::walls)
|
self.0.get(coord).map(HexTile::walls)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of tiles in the maze
|
/// Returns the number of tiles in the maze.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.0.len()
|
self.0.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the maze is empty
|
/// Returns `true` if the maze contains no tiles.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.0.is_empty()
|
self.0.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes a wall from a tile in the specified direction.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `coord` - The hexagonal coordinates of the tile.
|
||||||
|
/// - `direction` - The direction of the wall to remove.
|
||||||
pub fn remove_tile_wall(&mut self, coord: &Hex, direction: EdgeDirection) {
|
pub fn remove_tile_wall(&mut self, coord: &Hex, direction: EdgeDirection) {
|
||||||
if let Some(tile) = self.0.get_mut(coord) {
|
if let Some(tile) = self.0.get_mut(coord) {
|
||||||
tile.walls.remove(direction);
|
tile.walls.remove(direction);
|
||||||
|
|||||||
@ -7,6 +7,8 @@ use hexx::HexLayout;
|
|||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
/// Represents a single hexagonal tile in the maze
|
/// Represents a single hexagonal tile in the maze
|
||||||
|
///
|
||||||
|
/// Each tile has a position and a set of walls defining its boundaries.
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
|
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
|
||||||
#[cfg_attr(feature = "bevy", derive(Component))]
|
#[cfg_attr(feature = "bevy", derive(Component))]
|
||||||
@ -18,7 +20,11 @@ pub struct HexTile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl HexTile {
|
impl HexTile {
|
||||||
/// Creates a new tile with pos and default walls
|
/// Creates a new tile with the given position and default walls.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `pos` - The hexagonal coordinates of the tile.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new(pos: Hex) -> Self {
|
pub fn new(pos: Hex) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -40,7 +46,11 @@ impl HexTile {
|
|||||||
pub const fn pos(&self) -> Hex {
|
pub const fn pos(&self) -> Hex {
|
||||||
self.pos
|
self.pos
|
||||||
}
|
}
|
||||||
|
/// Converts the tile's position to a 2D vector based on the given layout.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `layout` - The hexagonal layout used for conversion.
|
||||||
#[cfg(feature = "bevy_reflect")]
|
#[cfg(feature = "bevy_reflect")]
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
@ -48,6 +58,11 @@ impl HexTile {
|
|||||||
layout.hex_to_world_pos(self.pos)
|
layout.hex_to_world_pos(self.pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Converts the tile's position to a 3D vector based on the given layout.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `layout` - The hexagonal layout used for conversion.
|
||||||
#[cfg(feature = "bevy_reflect")]
|
#[cfg(feature = "bevy_reflect")]
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
|||||||
362
src/walls.rs
362
src/walls.rs
@ -2,48 +2,31 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use hexx::EdgeDirection;
|
use hexx::EdgeDirection;
|
||||||
|
|
||||||
/**
|
/// A bit-flag representation of walls in a hexagonal tile.
|
||||||
A bit-flag representation of walls in a hexagonal tile.
|
///
|
||||||
|
/// `Walls` uses an efficient bit-flag system to track the presence or absence of walls
|
||||||
`Walls` uses an efficient bit-flag system to track the presence or absence of walls
|
/// along each edge of a hexagonal tile. Each of the six possible walls is represented
|
||||||
along each edge of a hexagonal tile. Each of the six possible walls is represented
|
/// by a single bit in an 8-bit integer, allowing for fast operations and minimal memory usage.
|
||||||
by a single bit in an 8-bit integer, allowing for fast operations and minimal memory usage.
|
///
|
||||||
|
/// # Examples
|
||||||
# Examples
|
///
|
||||||
|
/// Creating and manipulating walls:
|
||||||
Creating and manipulating walls:
|
/// ```
|
||||||
```
|
/// use hexlab::prelude::*;
|
||||||
use hexlab::prelude::*;
|
///
|
||||||
|
/// // Create a hexagon with all walls
|
||||||
// Create a hexagon with all walls
|
/// let walls = Walls::new();
|
||||||
let walls = Walls::new();
|
/// assert!(walls.is_closed());
|
||||||
assert!(walls.is_closed());
|
///
|
||||||
|
/// // Create a hexagon with no walls
|
||||||
// Create a hexagon with no walls
|
/// let mut walls = Walls::empty();
|
||||||
let mut walls = Walls::empty();
|
/// assert!(walls.is_empty());
|
||||||
assert!(walls.is_empty());
|
///
|
||||||
|
/// // Add specific walls
|
||||||
// Add specific walls
|
/// walls.add(EdgeDirection::FLAT_NORTH);
|
||||||
walls.add(EdgeDirection::FLAT_NORTH);
|
/// walls.add(EdgeDirection::FLAT_SOUTH);
|
||||||
walls.add(EdgeDirection::FLAT_SOUTH);
|
/// assert_eq!(walls.count(), 2);
|
||||||
assert_eq!(walls.count(), 2);
|
/// ```
|
||||||
```
|
|
||||||
|
|
||||||
Using walls in game logic:
|
|
||||||
|
|
||||||
```
|
|
||||||
use hexlab::prelude::*;
|
|
||||||
let mut walls = Walls::empty();
|
|
||||||
|
|
||||||
// Add walls to create a corner
|
|
||||||
walls.add(EdgeDirection::FLAT_NORTH);
|
|
||||||
walls.add(EdgeDirection::FLAT_SOUTH_EAST);
|
|
||||||
|
|
||||||
// Check if a specific direction has a wall
|
|
||||||
assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
|
||||||
assert!(!walls.contains(EdgeDirection::FLAT_SOUTH));
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
|
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
|
||||||
#[cfg_attr(feature = "bevy", derive(Component))]
|
#[cfg_attr(feature = "bevy", derive(Component))]
|
||||||
@ -52,89 +35,34 @@ assert!(!walls.contains(EdgeDirection::FLAT_SOUTH));
|
|||||||
pub struct Walls(u8);
|
pub struct Walls(u8);
|
||||||
|
|
||||||
impl Walls {
|
impl Walls {
|
||||||
/**
|
/// Creates a new set of walls with all edges closed.
|
||||||
Creates a new set of walls with all edges closed.
|
///
|
||||||
|
/// This is the default state where all six edges of the hexagon have walls.
|
||||||
This is the default state where all six edges of the hexagon have walls.
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```
|
|
||||||
use hexlab::prelude::*;
|
|
||||||
|
|
||||||
let walls = Walls::new();
|
|
||||||
assert!(walls.is_closed());
|
|
||||||
assert_eq!(walls.count(), 6);
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Creates a new set of walls with no edges (completely open).
|
||||||
Creates a new set of walls with no edges (completely open).
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```
|
|
||||||
use hexlab::prelude::*;
|
|
||||||
|
|
||||||
let walls = Walls::empty();
|
|
||||||
assert!(walls.is_empty());
|
|
||||||
assert_eq!(walls.count(), 0);
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn empty() -> Self {
|
pub const fn empty() -> Self {
|
||||||
Self(0)
|
Self(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Checks if the walls are currently empty (no walls present).
|
||||||
Checks if the walls are currently empty
|
|
||||||
|
|
||||||
Returns `true` if all directions have no walls set.
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```
|
|
||||||
use hexlab::prelude::*;
|
|
||||||
|
|
||||||
let walls = Walls::empty();
|
|
||||||
assert!(walls.is_empty());
|
|
||||||
|
|
||||||
let walls = Walls::new();
|
|
||||||
assert!(!walls.is_empty());
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn is_empty(&self) -> bool {
|
pub const fn is_empty(&self) -> bool {
|
||||||
self.0 == 0
|
self.0 == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Adds a wall in the specified direction.
|
||||||
Adds a wall in the specified direction
|
///
|
||||||
|
/// # Arguments
|
||||||
This method uses bitwise operations to efficiently set the wall flag
|
///
|
||||||
for the given direction. Multiple walls can be added to the same hexagon.
|
/// 0 `direction` - The direction in which to add the wall.
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```
|
|
||||||
use hexlab::prelude::*;
|
|
||||||
|
|
||||||
let mut walls = Walls::empty();
|
|
||||||
walls.add(EdgeDirection::FLAT_NORTH);
|
|
||||||
assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
|
||||||
assert!(!walls.contains(EdgeDirection::FLAT_SOUTH));
|
|
||||||
|
|
||||||
walls.add(EdgeDirection::FLAT_SOUTH);
|
|
||||||
assert!(walls.contains(EdgeDirection::FLAT_SOUTH));
|
|
||||||
assert_eq!(walls.count(), 2);
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn add<T>(&mut self, direction: T)
|
pub fn add<T>(&mut self, direction: T)
|
||||||
where
|
where
|
||||||
@ -143,25 +71,11 @@ impl Walls {
|
|||||||
self.0 |= direction.into().0;
|
self.0 |= direction.into().0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Removes a wall in the specified direction.
|
||||||
Removes a wall in the specified direction
|
///
|
||||||
|
/// # Arguments
|
||||||
Returns `true` if a wall was actually removed, `false` if there was no wall
|
///
|
||||||
in the specified direction.
|
/// - `direction` - The direction from which to remove the wall.
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```
|
|
||||||
use hexlab::prelude::*;
|
|
||||||
|
|
||||||
let mut walls = Walls::new();
|
|
||||||
assert!(walls.remove(EdgeDirection::FLAT_NORTH));
|
|
||||||
assert!(!walls.contains(EdgeDirection::FLAT_NORTH));
|
|
||||||
|
|
||||||
// Removing a non-existent wall returns false
|
|
||||||
assert!(!walls.remove(EdgeDirection::FLAT_NORTH));
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn remove<T>(&mut self, direction: T) -> bool
|
pub fn remove<T>(&mut self, direction: T) -> bool
|
||||||
where
|
where
|
||||||
@ -174,23 +88,11 @@ impl Walls {
|
|||||||
was_removed
|
was_removed
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Checks if there is a wall in the specified direction.
|
||||||
Returns true if there is a wall in the specified direction
|
///
|
||||||
|
/// # Arguments
|
||||||
Uses efficient bitwise operations to check for the presence of a wall.
|
///
|
||||||
|
/// - `other` - The direction to check for a wall.
|
||||||
# Examples
|
|
||||||
|
|
||||||
```
|
|
||||||
use hexlab::prelude::*;
|
|
||||||
|
|
||||||
let mut walls = Walls::empty();
|
|
||||||
|
|
||||||
walls.add(EdgeDirection::FLAT_NORTH);
|
|
||||||
assert!(walls.contains(EdgeDirection::FLAT_NORTH));
|
|
||||||
assert!(!walls.contains(EdgeDirection::FLAT_SOUTH));
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn contains<T>(&self, other: T) -> bool
|
pub fn contains<T>(&self, other: T) -> bool
|
||||||
where
|
where
|
||||||
@ -199,98 +101,39 @@ impl Walls {
|
|||||||
self.0 & other.into().0 != 0
|
self.0 & other.into().0 != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Returns the raw bit representation of the walls
|
||||||
Returns the raw bit representation of the walls
|
|
||||||
|
|
||||||
This method provides access to the underlying bit flags for advanced usage.
|
|
||||||
The bits are ordered according to the `EdgeDirection` indices.
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```
|
|
||||||
use hexlab::prelude::*;
|
|
||||||
|
|
||||||
let mut walls = Walls::new();
|
|
||||||
|
|
||||||
assert_eq!(walls.as_bits(), 0b111111);
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn as_bits(&self) -> u8 {
|
pub const fn as_bits(&self) -> u8 {
|
||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Returns the total number of walls present
|
||||||
Returns the total number of walls present
|
|
||||||
|
|
||||||
Efficiently counts the number of set bits in the internal representation.
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```
|
|
||||||
use hexlab::prelude::*;
|
|
||||||
|
|
||||||
let mut walls = Walls::empty();
|
|
||||||
|
|
||||||
assert_eq!(walls.count(), 0);
|
|
||||||
|
|
||||||
walls.add(EdgeDirection::FLAT_NORTH);
|
|
||||||
walls.add(EdgeDirection::FLAT_SOUTH);
|
|
||||||
assert_eq!(walls.count(), 2);
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn count(&self) -> u8 {
|
pub fn count(&self) -> u8 {
|
||||||
u8::try_from(self.0.count_ones()).unwrap_or_default()
|
u8::try_from(self.0.count_ones()).unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Returns a `Walls` value representing all possible directions.
|
||||||
Returns all possible directions as a `Walls` value
|
|
||||||
|
|
||||||
This represents a hexagon with walls in all six directions.
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```
|
|
||||||
use hexlab::prelude::*;
|
|
||||||
|
|
||||||
let all_walls = Walls::all_directions();
|
|
||||||
|
|
||||||
assert_eq!(all_walls.count(), 6);
|
|
||||||
assert!(all_walls.is_closed());
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn all_directions() -> Self {
|
pub const fn all_directions() -> Self {
|
||||||
Self(0b11_1111)
|
Self(0b11_1111)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Toggles a wall in the specified direction.
|
||||||
Toggles a wall in the specified direction.
|
///
|
||||||
|
/// If a wall exists in the given direction, it will be removed.
|
||||||
If a wall exists in the given direction, it will be removed.
|
/// If no wall exists, one will be added.
|
||||||
If no wall exists, one will be added.
|
///
|
||||||
Returns the previous state (`true` if a wall was present).
|
/// # Arguments
|
||||||
|
///
|
||||||
# Examples
|
/// - `direction` - The direction in which to toggle the wall.
|
||||||
|
///
|
||||||
```
|
/// # Returns
|
||||||
use hexlab::prelude::*;
|
///
|
||||||
|
/// The previous state (`true` if a wall was present before toggling, `false` otherwise).
|
||||||
let mut walls = Walls::empty();
|
|
||||||
|
|
||||||
assert!(!walls.toggle(EdgeDirection::FLAT_NORTH)); // Returns false, wall was not present
|
|
||||||
assert!(walls.contains(EdgeDirection::FLAT_NORTH)); // Wall is now present
|
|
||||||
|
|
||||||
let mut walls = Walls::new();
|
|
||||||
|
|
||||||
assert!(walls.toggle(EdgeDirection::FLAT_NORTH)); // Returns true, wall was present
|
|
||||||
assert!(!walls.contains(EdgeDirection::FLAT_NORTH)); // Wall is now removed
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
pub fn toggle<T>(&mut self, direction: T) -> bool
|
pub fn toggle<T>(&mut self, direction: T) -> bool
|
||||||
where
|
where
|
||||||
T: Into<Self> + Copy,
|
T: Into<Self> + Copy,
|
||||||
@ -304,54 +147,55 @@ impl Walls {
|
|||||||
is_present
|
is_present
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Checks if walls are present in all six directions.
|
||||||
Checks if walls are present in all six directions.
|
///
|
||||||
|
/// # Returns
|
||||||
Returns `true` if the hexagon has all possible walls, making it completely enclosed.
|
///
|
||||||
|
/// `true` if the hexagon has all possible walls, making it completely enclosed.
|
||||||
# Examples
|
///
|
||||||
|
/// # Deprecated
|
||||||
```
|
///
|
||||||
use hexlab::prelude::*;
|
/// This method is deprecated since version 0.3.1. Use `is_enclosed()` instead.
|
||||||
|
|
||||||
let walls = Walls::new();
|
|
||||||
assert!(walls.is_closed());
|
|
||||||
|
|
||||||
let mut walls = Walls::empty();
|
|
||||||
assert!(!walls.is_closed());
|
|
||||||
// Add all walls manually
|
|
||||||
for direction in EdgeDirection::iter() {
|
|
||||||
walls.add(direction);
|
|
||||||
}
|
|
||||||
assert!(walls.is_closed());
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
#[deprecated(since = "0.3.1", note = "use `walls::Walls::is_enclosed()`")]
|
||||||
pub fn is_closed(&self) -> bool {
|
pub fn is_closed(&self) -> bool {
|
||||||
|
self.is_enclosed()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if walls are present in all six directions.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// `true` if the hexagon has all possible walls, making it completely enclosed.
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub fn is_enclosed(&self) -> bool {
|
||||||
self.count() == 6
|
self.count() == 6
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Sets walls for multiple directions at once.
|
||||||
Sets walls for multiple directions at once.
|
///
|
||||||
|
/// This method efficiently adds multiple walls in a single operation while
|
||||||
This method efficiently adds multiple walls in a single operation while
|
/// preserving any existing walls not specified in the input.
|
||||||
preserving any existing walls not specified in the input.
|
///
|
||||||
|
/// # Arguments
|
||||||
# Examples
|
///
|
||||||
|
/// - `other` - The walls to add, specified as a `Walls` instance or any type
|
||||||
```
|
/// that can be converted into `Walls`.
|
||||||
use hexlab::prelude::*;
|
///
|
||||||
|
///
|
||||||
let mut walls = Walls::empty();
|
/// # Examples
|
||||||
walls.add(EdgeDirection::FLAT_NORTH);
|
///
|
||||||
|
/// ```
|
||||||
walls.fill([EdgeDirection::FLAT_SOUTH, EdgeDirection::FLAT_SOUTH_EAST]);
|
/// use hexlab::prelude::*;
|
||||||
|
///
|
||||||
assert!(walls.contains(EdgeDirection::FLAT_SOUTH));
|
/// let mut walls = Walls::empty();
|
||||||
assert_eq!(walls.count(), 3);
|
/// walls.fill([EdgeDirection::FLAT_NORTH ,EdgeDirection::FLAT_SOUTH, EdgeDirection::FLAT_SOUTH_EAST]);
|
||||||
```
|
///
|
||||||
*/
|
/// assert!(walls.contains(EdgeDirection::FLAT_SOUTH));
|
||||||
|
/// assert_eq!(walls.count(), 3);
|
||||||
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn fill<T>(&mut self, other: T)
|
pub fn fill<T>(&mut self, other: T)
|
||||||
where
|
where
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user