我正在制作一个游戏引擎,目前我正在研究实体组件系统。这里有一些代码可以更好地理解:
class Entity {
public:
...
void AddComponent(EntityComponent component, unsigned int id){
m_Components.insert({id, component});}
EntityComponent GetComponent(unsigned int id)
{
auto i = m_Components.find(id);
ASSERT(i != m_Components->end()); // ASSERT is a custom macro
return i->second;
}
private:
...
std::unordered_map<unsigned int, EntityComponent> m_Components;
};
还有一个简单的父类EntityComponent,它只处理 ID(我跳过了一些代码,因为它在这个问题中并不重要):
class EntityComponent {
public:
...
private:
unsigned int m_EntityID;
size_t m_PoolIndex;
};
而且是孩子。其中之一是TransformComponent:
class TransformComponent : public EntityComponent {
public:
TransformComponent(unsigned int entityID, unsigned int poolID = 0, glm::vec2 position = glm::vec2(1.0,1.0), glm::vec2 scale = glm::vec2(1.0,1.0), float rotation = 0) : EntityComponent(entityID, poolID),m_Rotation(rotation), m_Scale(scale), m_Position(position){}
inline glm::vec2 GetPosition() { return m_Position; }
inline glm::vec2 GetScale() { return m_Scale; }
private:
glm::vec2 m_Position;
float m_Rotation;
glm::vec2 m_Scale;
};
所以问题是我想创建一个实体,向它添加一个 TransformComponent 并使用它的函数 GetPosition()、GetScale()。
创建实体并添加 TransformComponent 是可行的,但是当我想使用 GetComponent(id) 时,它会返回父类EntityComponent,因此这意味着我不能使用 GetPosition() 等函数。
如何更改代码,以便将EntityComponent的不同子级添加到 unordered_map 并使用它们的 ID 接收它们并使用它们的公共方法?