mirror of
https://github.com/kristoferssolo/hexlab.git
synced 2025-10-21 19:40:34 +00:00
feat(hex): add basic structs
This commit is contained in:
parent
badfef6b08
commit
ec4b276ce7
35
Cargo.lock
generated
35
Cargo.lock
generated
@ -7,12 +7,17 @@ name = "glam"
|
|||||||
version = "0.27.0"
|
version = "0.27.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9e05e7e6723e3455f4818c7b26e855439f7546cf617ef669d1adedb8669e5cb9"
|
checksum = "9e05e7e6723e3455f4818c7b26e855439f7546cf617ef669d1adedb8669e5cb9"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hexlab"
|
name = "hexlab"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"hexx",
|
"hexx",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -25,6 +30,18 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.7.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.89"
|
version = "1.0.89"
|
||||||
@ -43,6 +60,12 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.214"
|
version = "1.0.214"
|
||||||
@ -63,6 +86,18 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.132"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"memchr",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.87"
|
version = "2.0.87"
|
||||||
|
|||||||
11
Cargo.toml
11
Cargo.toml
@ -2,6 +2,15 @@
|
|||||||
name = "hexlab"
|
name = "hexlab"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
description = "A hexagonal maze library"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
hexx = "0.18"
|
hexx = { version = "0.18" }
|
||||||
|
serde = { version = "1.0", features = ["derive"], optional = true }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = []
|
||||||
|
serde = ["dep:serde", "hexx/serde"]
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
serde_json = "1.0"
|
||||||
|
|||||||
15
src/lib.rs
15
src/lib.rs
@ -1,14 +1,3 @@
|
|||||||
pub fn add(left: u64, right: u64) -> u64 {
|
pub mod maze;
|
||||||
left + right
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
pub use maze::*;
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_works() {
|
|
||||||
let result = add(2, 2);
|
|
||||||
assert_eq!(result, 4);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user