Hi, on my adventure to learn rust i've gotten stuck on finding a solution to the following.
Im trying to store a map of vectors containing an arbitrary component type.
I Manage fine adding / removing etc components when i know the type i am wanting to manipulate, however i am getting stuck on how to have a function that without knowing the specific type to remove all components with the associated owner id.
In c++ / java i would just extend from an abstract class
struct ComponentHandler<T> {
components: Vec<T>,
owner: Vec<u32>
}
#[derive(Debug)]
pub struct ComponentManager {
map: HashMap<TypeId, Box<dyn Any>>
}
impl<T> ComponentHandler<T> {
pub fn new()->Self {}
pub fn remove_all_components_from(&mut self, eid:u32) {}
pub fn get_mut_component(&mut self, eid:u32) -> Option<&mut T> {}
pub fn remove_component_from(&mut self, eid: u32) {}
pub fn add_component(&mut self, component: T, id: u32) {}
}
impl ComponentManager {
pub fn new() ->Self {}
pub fn remove_all_components_from(&mut self, id:u32) {
for comp_list in &mut self.map {
let list: Option<&mut ComponentHandler> = comp_list.1.downcast_mut::<ComponentHandler>();
if list.is_some() {
list.unwrap().remove_all_components_from(id);
}
}
}
pub fn add_component<T:'static>(&mut self, component:T, id: u32) {}
pub fn remove_component_from<T: 'static>(&mut self, eid:u32) {}
pub fn get_mut_component<T:'static>(&mut self, eid: u32) ->Option<&mut T> {}
}