我一直在研究游戏引擎设计(特别是2d游戏引擎,但也适用于3d游戏),我对如何进行这方面的一些信息很感兴趣。我听说现在许多引擎都转向基于组件的设计,而不是传统的深度对象层次结构。
你知道关于这类设计通常是如何实现的信息的任何好的链接吗?我已经看到了层次结构的演进,但我真的找不到更多详细的信息(其中大多数似乎只是说“使用组件而不是层次结构”,但我发现在两种模型之间转换我的思想需要一些努力)。
任何好的链接或信息,这将是感激,甚至书籍,尽管链接和详细的答案在这里将是首选。
我一直在研究游戏引擎设计(特别是2d游戏引擎,但也适用于3d游戏),我对如何进行这方面的一些信息很感兴趣。我听说现在许多引擎都转向基于组件的设计,而不是传统的深度对象层次结构。
你知道关于这类设计通常是如何实现的信息的任何好的链接吗?我已经看到了层次结构的演进,但我真的找不到更多详细的信息(其中大多数似乎只是说“使用组件而不是层次结构”,但我发现在两种模型之间转换我的思想需要一些努力)。
任何好的链接或信息,这将是感激,甚至书籍,尽管链接和详细的答案在这里将是首选。
当前回答
我在上个学期的游戏开发课程中研究并实施了这一方法。希望这个示例代码可以为您指明正确的方向,告诉您如何处理这个问题。
class Entity {
public:
Entity(const unsigned int id, const std::string& enttype);
~Entity();
//Component Interface
const Component* GetComponent(const std::string& family) const;
void SetComponent(Component* newComp);
void RemoveComponent(const std::string& family);
void ClearComponents();
//Property Interface
bool HasProperty(const std::string& propName) const;
template<class T> T& GetPropertyDataPtr(const std::string& propName);
template<class T> const T& GetPropertyDataPtr(const std::string& propName) const;
//Entity Interface
const unsigned int GetID() const;
void Update(float dt);
private:
void RemoveProperty(const std::string& propName);
void ClearProperties();
template<class T> void AddProperty(const std::string& propName);
template<class T> Property<T>* GetProperty(const std::string& propName);
template<class T> const Property<T>* GetProperty(const std::string& propName) const;
unsigned int m_Id;
std::map<const string, IProperty*> m_Properties;
std::map<const string, Component*> m_Components;
};
组件指定行为并对属性进行操作。属性通过引用在所有组件之间共享,并免费获得更新。这意味着消息传递没有很大的开销。如果有任何问题,我会尽我所能回答。
其他回答
我目前正在GameDev.net上的许多帖子中研究这个确切的话题,并发现以下两个解决方案是我将为自己的游戏开发的良好候选方案:
批评我的基于组件的实体系统 外部基于组件的实体系统架构——> Lord_Evil的建议
虽然这不是一个关于游戏引擎设计主题的完整教程,但我发现这个页面有一些关于游戏组件架构使用的很好的细节和例子。
在这种情况下,组件对我来说就像是引擎的独立运行时部分,可以与其他组件并发执行。如果这是动机,那么您可能想要查看角色模型和使用它的系统。
我在上个学期的游戏开发课程中研究并实施了这一方法。希望这个示例代码可以为您指明正确的方向,告诉您如何处理这个问题。
class Entity {
public:
Entity(const unsigned int id, const std::string& enttype);
~Entity();
//Component Interface
const Component* GetComponent(const std::string& family) const;
void SetComponent(Component* newComp);
void RemoveComponent(const std::string& family);
void ClearComponents();
//Property Interface
bool HasProperty(const std::string& propName) const;
template<class T> T& GetPropertyDataPtr(const std::string& propName);
template<class T> const T& GetPropertyDataPtr(const std::string& propName) const;
//Entity Interface
const unsigned int GetID() const;
void Update(float dt);
private:
void RemoveProperty(const std::string& propName);
void ClearProperties();
template<class T> void AddProperty(const std::string& propName);
template<class T> Property<T>* GetProperty(const std::string& propName);
template<class T> const Property<T>* GetProperty(const std::string& propName) const;
unsigned int m_Id;
std::map<const string, IProperty*> m_Properties;
std::map<const string, Component*> m_Components;
};
组件指定行为并对属性进行操作。属性通过引用在所有组件之间共享,并免费获得更新。这意味着消息传递没有很大的开销。如果有任何问题,我会尽我所能回答。
有趣的artcle……
我在谷歌上快速搜索了一下,没有发现任何东西,但是你可能想要检查一些评论-很多人似乎已经尝试了实现一个简单的组件演示,你可能想要看看他们的一些灵感:
http://www.unseen-academy.de/componentSystem.html http://www.mcshaffry.com/GameCode/thread.php?threadid=732 http://www.codeplex.com/Wikipage?ProjectName=elephant
此外,评论本身似乎对如何编写这样一个系统进行了相当深入的讨论。