use bevy::{math::UVec3, platform::collections::HashMap};
use std::hash::{Hash, Hasher};
use crate::{chunk::Chunk, dir::Dir, path::Path};
#[derive(Debug, Clone)]
pub(crate) struct Node {
pub(crate) pos: UVec3,
pub(crate) chunk_index: (usize, usize, usize),
pub(crate) edges: HashMap<UVec3, Path>,
pub(crate) dir: Option<Dir>,
pub(crate) portal: bool,
}
impl Node {
pub(crate) fn new(pos: UVec3, chunk: Chunk, dir: Option<Dir>) -> Self {
Node {
pos,
chunk_index: chunk.index(),
dir,
edges: HashMap::new(),
portal: false,
}
}
pub(crate) fn edges(&self) -> Vec<UVec3> {
self.edges.keys().cloned().collect()
}
pub(crate) fn remove_edges_to_positions(&mut self, positions: &[UVec3]) {
for pos in positions {
self.edges.remove(pos);
}
}
}
impl PartialEq for Node {
fn eq(&self, other: &Self) -> bool {
self.pos == other.pos
}
}
impl Eq for Node {}
impl Hash for Node {
fn hash<H: Hasher>(&self, state: &mut H) {
self.pos.hash(state);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_node_eq() {
let node1 = Node::new(
UVec3::new(1, 2, 3),
Chunk::new((0, 0, 0), UVec3::new(0, 0, 0), UVec3::new(16, 16, 16)),
None,
);
let node2 = Node::new(
UVec3::new(1, 2, 3),
Chunk::new((0, 0, 0), UVec3::new(0, 0, 0), UVec3::new(16, 16, 16)),
None,
);
assert_eq!(node1, node2);
}
#[test]
fn test_node_hash() {
let node1 = Node::new(
UVec3::new(1, 2, 3),
Chunk::new((0, 0, 0), UVec3::new(0, 0, 0), UVec3::new(16, 16, 16)),
None,
);
let node2 = Node::new(
UVec3::new(1, 2, 3),
Chunk::new((0, 0, 0), UVec3::new(0, 0, 0), UVec3::new(16, 16, 16)),
None,
);
let mut hasher1 = std::collections::hash_map::DefaultHasher::new();
let mut hasher2 = std::collections::hash_map::DefaultHasher::new();
node1.hash(&mut hasher1);
node2.hash(&mut hasher2);
assert_eq!(hasher1.finish(), hasher2.finish());
}
}