Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Component base class
- /// </summary>
- class Component
- {
- public:
- Entity *entity; //ref to owner
- virtual void init() {}
- virtual void update() {}
- virtual void draw() {}
- virtual ~Component() {}
- };
- class Entity
- {
- private:
- Manager &manager; //manager ref
- bool active = true;
- std::vector<std::unique_ptr<Component>> components;
- ComponentArray componentArray;
- ComponentBitSet componentBitSet;
- GroupBitset groupBitset;
- public:
- Entity(Manager &mManager) : manager(mManager) {}
- void update()
- {
- for(auto &c : components) c->update();
- }
- void draw()
- {
- for(auto &c : components) c->draw();
- }
- bool isActive() { return active; }
- void destroy() { active = false; }
- bool hasGroup(Group mGroup)
- {
- return groupBitset[mGroup];
- }
- void addGroup(Group mGroup);
- void delGroup(Group mGroup)
- {
- groupBitset[mGroup] = false;
- }
- template <typename T> bool hasComponent()const
- {
- return componentBitSet[getComponentTypeID<T>()];
- }
- template <typename T, typename... TArgs>
- T &addComponent(TArgs&&... mArgs)
- {
- T *c(new T(std::forward<TArgs>(mArgs)...));
- c->entity = this;
- std::unique_ptr<Component> uPtr{ c };
- components.emplace_back(std::move(uPtr));
- componentArray[getComponentTypeID<T>()] = c;
- componentBitSet[getComponentTypeID<T>()] = true;
- c->init();
- return *c;
- }
- template<typename T> T &getComponent() const
- {
- auto ptr(componentArray[getComponentTypeID<T>()]);
- return *static_cast<T *>(ptr);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement