use crate::components::Cell;
#[cfg(feature = "2D")]
use crate::components::MooreCell2d;
#[cfg(feature = "3D")]
use crate::components::NeumannCell3d;
use bevy::{
platform::collections::{HashMap, HashSet},
prelude::{Entity, Resource},
};
#[cfg(feature = "2D")]
pub type Map2d = CellMap<MooreCell2d>;
#[cfg(feature = "3D")]
pub type Map3d = CellMap<NeumannCell3d>;
#[derive(Debug, Clone, Resource)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
pub struct CellMap<C: Cell> {
cells: HashMap<C::Coordinates, Entity>,
}
impl<C: Cell> Default for CellMap<C> {
fn default() -> Self {
Self {
cells: Default::default(),
}
}
}
impl<C: Cell> CellMap<C> {
pub fn get_cell_entities<'a>(
&'a self,
coords: &'a [C::Coordinates],
) -> impl Iterator<Item = &'a Entity> + 'a {
coords.iter().filter_map(|c| self.cells.get(c))
}
pub fn insert_cell(&mut self, coordinates: C::Coordinates, entity: Entity) -> Option<Entity> {
self.cells.insert(coordinates, entity)
}
pub fn remove_cell(&mut self, coordinates: &C::Coordinates) -> Option<Entity> {
self.cells.remove(coordinates)
}
pub fn remove_entities(&mut self, entities: impl Iterator<Item = Entity>) {
let entities: HashSet<_> = entities.collect();
if entities.is_empty() {
return;
}
self.cells.retain(|_, entity| !entities.contains(entity));
}
pub fn get_cell(&self, coordinates: &C::Coordinates) -> Option<Entity> {
self.cells.get(coordinates).copied()
}
pub fn clear(&mut self) {
self.cells.clear();
}
}