Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
graphite/engine/ecs/include/entity.hpp
2024-01-03 16:05:02 -05:00

151 lines
2.7 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <unordered_map>
#include <map>
#include <typeindex>
#include <functional>
#include <utility.hpp>
#include "entitypool.hpp"
#include "typetracker.hpp"
#include "component.hpp"
class GameInstance;
class World;
class Entity
{
friend class EntityPool;
public:
uint32_t GetID()
{
return m_id;
}
template<class T>
T* Add()
{
T* t = new T;
GetEntPool().RegisterComponent(this, t);
return t;
}
Component* Add(Component* component)
{
GetEntPool().RegisterComponent(this, component);
return component;
}
Component* Add(const std::string& componentName)
{
auto types = TypeTracker::GetTypes();
for(auto type : types)
{
if(type.first == componentName)
return Add(type.second);
}
return nullptr;
}
Component* Add(std::type_index type)
{
return Add(Utility::GetTypeName(type));
}
template<class T>
void Remove()
{
GetEntPool().RemoveComponent(this, typeid(T));
}
void Remove(Component* component)
{
GetEntPool().RemoveComponent(this, component);
}
void RemoveAll()
{
GetEntPool().RemoveComponents(this);
}
void Duplicate(Component* component)
{
GetEntPool().DuplicateComponent(this, component);
}
template<class T>
T* Get()
{
auto components = GetEntPool().GetComponents(this);
for(auto& itr : components)
{
if(itr.first.type == typeid(T))
return static_cast<T*>(itr.second);
}
return nullptr;
}
Transform* GetTransform()
{
return Get<Transform>();
}
template<class T>
std::vector<T*> GetComponentsOf()
{
std::vector<T*> tmp;
auto components = GetEntPool().GetComponents(this);
for(auto& itr : components)
{
if(itr.first.type == typeid(T))
tmp.push_back(static_cast<T*>(itr.second));
}
return tmp;
}
std::vector<Component*> GetComponents()
{
std::vector<Component*> tmp;
auto components = GetEntPool().GetComponents(this);
for(auto& itr : components)
{
tmp.push_back(itr.second);
}
return tmp;
}
GameInstance* GetGame() const
{
return m_gameInstance;
}
World* GetWorld() const
{
return m_world;
}
std::string name;
std::string tag;
protected:
//you should be using the entity pool!
Entity(const std::string& requestedName = "Entity", uint32_t requestedID = -1);
GameInstance* m_gameInstance = nullptr;
World* m_world = nullptr;
private:
uint32_t m_id = -1;
};