Created
June 12, 2013 22:23
-
-
Save fielding/5769666 to your computer and use it in GitHub Desktop.
Early version of entities as more than just IDs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unordered_map> | |
#include "common.h" | |
class Entity | |
{ | |
public: | |
Entity(); | |
template<typename T> void addComponent(T* comp) | |
{ | |
components.insert(std::pair<const std::type_info*, IComponent*>(&typeid(*comp), comp)); | |
} | |
template<typename T> T *getComponent() | |
{ | |
return static_cast<T*>(components[&typeid(T)]); | |
} | |
template<typename T> void removeComponent() | |
{ | |
if (components.count(&typeid(T))) | |
{ | |
T *component = static_cast<T*>(components[&typeid(T)]); | |
components.erase(&typeid(T)); | |
delete component; | |
} | |
} | |
std::unordered_map<const std::type_info*, IComponent*> components; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment