mirror of
https://github.com/kristoferssolo/hexlab.git
synced 2026-02-04 06:22:06 +00:00
feat(hex): add basic structs
This commit is contained in:
15
src/lib.rs
15
src/lib.rs
@@ -1,14 +1,3 @@
|
||||
pub fn add(left: u64, right: u64) -> u64 {
|
||||
left + right
|
||||
}
|
||||
pub mod maze;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
||||
pub use maze::*;
|
||||
|
||||
38
src/maze/maze.rs
Normal file
38
src/maze/maze.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use hexx::{EdgeDirection, Hex, HexLayout};
|
||||
|
||||
use super::{HexTile, Walls};
|
||||
|
||||
pub struct HexMaze {
|
||||
pub tiles: HashMap<Hex, HexTile>,
|
||||
pub layout: HexLayout,
|
||||
}
|
||||
|
||||
impl HexMaze {
|
||||
pub fn new(layout: HexLayout) -> Self {
|
||||
Self {
|
||||
tiles: HashMap::new(),
|
||||
layout,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_tile(&mut self, coords: Hex) {
|
||||
let tile = HexTile::new(coords);
|
||||
self.tiles.insert(coords, tile);
|
||||
}
|
||||
|
||||
pub fn add_wall(&mut self, coord: Hex, direction: EdgeDirection) {
|
||||
if let Some(tile) = self.tiles.get_mut(&coord) {
|
||||
tile.walls.add(direction)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_tile(&self, coord: &Hex) -> Option<&HexTile> {
|
||||
self.tiles.get(coord)
|
||||
}
|
||||
|
||||
pub fn get_walls(&self, coord: &Hex) -> Option<&Walls> {
|
||||
self.tiles.get(coord).map(|tile| &tile.walls)
|
||||
}
|
||||
}
|
||||
7
src/maze/mod.rs
Normal file
7
src/maze/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
pub mod maze;
|
||||
pub mod tile;
|
||||
pub mod walls;
|
||||
|
||||
pub use maze::HexMaze;
|
||||
pub use tile::HexTile;
|
||||
pub use walls::Walls;
|
||||
19
src/maze/tile.rs
Normal file
19
src/maze/tile.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use hexx::Hex;
|
||||
|
||||
use super::Walls;
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct HexTile {
|
||||
pub pos: Hex,
|
||||
pub walls: Walls,
|
||||
}
|
||||
|
||||
impl HexTile {
|
||||
pub fn new(pos: Hex) -> Self {
|
||||
Self {
|
||||
pos,
|
||||
walls: Walls::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
60
src/maze/walls.rs
Normal file
60
src/maze/walls.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use hexx::EdgeDirection;
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Walls(u8);
|
||||
|
||||
impl Walls {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn add(&mut self, direction: EdgeDirection) {
|
||||
self.0 |= Self::from(direction).0
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, direction: EdgeDirection) {
|
||||
self.0 &= !Self::from(direction).0
|
||||
}
|
||||
|
||||
pub fn has(&self, direction: EdgeDirection) -> bool {
|
||||
self.0 & Self::from(direction).0 != 0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EdgeDirection> for Walls {
|
||||
fn from(value: EdgeDirection) -> Self {
|
||||
let bits = match value.index() {
|
||||
0 => 0b000001,
|
||||
1 => 0b000010,
|
||||
2 => 0b000011,
|
||||
3 => 0b000100,
|
||||
4 => 0b000101,
|
||||
5 => 0b000110,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
Self(bits)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Walls {
|
||||
type Target = u8;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for Walls {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Walls {
|
||||
fn default() -> Self {
|
||||
Self(0b111111)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user