Just fishing for input
Code: Select all
#pragma once
#include "GameObject.h"
class ObjectManager
{
//hold class type in map (std::type_index) then have a container
// to hold all the object of std::type_index.
std::unordered_map<std::type_index, std::vector<std::unique_ptr<GameObject>>> m_objects;
public:
ObjectManager() = default;
~ObjectManager() {}
template<typename Obj>
size_t GetNumbObjects()
{
std::type_index index(typeid(Obj));
return m_objects[index].size();
}
template<typename Obj>// incoming class
Obj& AddObj(DataPack& data)
{
std::type_index index(typeid(Obj));// class type
// DataPack is a base struct
// the Obj then type casts to proper DataPack in
// it's constructor ie: DromeDataPack* pack = (DroneDataPack*)data
m_objects[index].push_back(std::make_unique<Obj>(&data));
// GameObject friends this class to hold it's index (mgr_index is a private member)
m_objects[index].back()->mgr_index = m_objects[index].size() - 1;
return static_cast<Obj&>(*m_objects[index].back());
}
template<typename Obj>
Obj& GetObj(int in_index)
{
Obj* empty = nullptr;// cheezy way to send a nullptr ??
std::type_index index(typeid(Obj));
assert(m_objects.count(index) != 0);// make sure the class type is here
if (in_index < 0 || in_index >= m_objects[index].size())
return *empty;
return static_cast<Obj&>(*m_objects[index][in_index]);
}
template<typename Obj>
void RemoveObj(Obj* object)// won't compile is classes are different
{
std::type_index index(typeid(Obj));
assert(m_objects.count(index) != 0);
int I = object->mgr_index;
if( I < 0 || I >= m_objects[index].size())
return;
m_objects[index][I].reset();
m_objects[index].erase(m_objects[index].begin() + I);
}
};