use std::{
collections::HashMap,
ops::{Deref, DerefMut},
};
use bevy::{ecs::system::EntityCommands, math::Vec3};
use procedural_tilemaps_core::{
generator::model::{ModelIndex, ModelRotation},
grid::cartesian::coordinates::GridDelta,
};
pub trait BundleInserter: Sync + Send + Default + 'static {
fn insert_bundle(
&self,
command: &mut EntityCommands,
translation: Vec3,
scale: Vec3,
rotation: ModelRotation,
);
}
#[derive(Clone, Debug)]
pub struct ModelAsset<A: BundleInserter> {
pub assets_bundle: A,
pub spawn_commands: fn(&mut EntityCommands),
pub grid_offset: GridDelta,
pub world_offset: Vec3,
}
#[derive(Debug)]
pub struct ModelsAssets<A: BundleInserter> {
map: HashMap<ModelIndex, Vec<ModelAsset<A>>>,
}
impl<A: BundleInserter> Deref for ModelsAssets<A> {
type Target = HashMap<ModelIndex, Vec<ModelAsset<A>>>;
fn deref(&self) -> &Self::Target {
&self.map
}
}
impl<A: BundleInserter> DerefMut for ModelsAssets<A> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.map
}
}
impl<A: BundleInserter> ModelsAssets<A> {
pub fn new() -> Self {
Self {
map: Default::default(),
}
}
pub fn add(&mut self, index: ModelIndex, model_asset: ModelAsset<A>) {
match self.get_mut(&index) {
Some(assets) => {
assets.push(model_asset);
}
None => {
self.insert(index, vec![model_asset]);
}
}
}
}