在游戏中组织实体的最佳方式? [英] Best way to organize entities in a game?

查看:133
本文介绍了在游戏中组织实体的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在C ++中创建了一个OpenGL游戏,它将创建许多对象(敌人,玩家角色,项目等)。我想知道最好的方式来组织这些,因为他们将根据时间,玩家位置/动作等实时创建和销毁。

Let's say I'm creating an OpenGL game in C++ that will have many objects created (enemies, player characters, items, etc.). I'm wondering the best way to organize these since they will be created and destroyed real-time based on time, player position/actions etc.

这里是我想到目前为止:
我可以有一个全局数组来存储指向这些对象的指针。这些对象的纹理/上下文被加载到它们的构造函数中。这些对象将有不同的类型,所以我可以转换指针获得它们在数组,但我想以后有一个renderObjects()函数,将使用一个循环来为每个现有对象调用ObjectN.render()函数。

Here's what I've thought of so far: I can have a global array to store pointers to these objects. The textures/context for these objects are loaded in their constructors. These objects will have different types, so I can cast the pointers to get them in the array, but I want to later have a renderObjects() function that will use a loop to call an ObjectN.render() function for each existing object.

我想我之前尝试过,但我不知道什么类型来初始化数组,所以我选择了一个任意的对象类型,然后投射任何东西这种类型。如果我记得,这没有工作,因为编译器不希望我dereferencing指针,如果它不再知道他们的类型,即使一个给定的成员函数具有相同的名称:(* Object5).render()<不工作?

I think I've tried this before but I didn't know what type to initialize the array with, so I picked an arbitrary object type, then cast anything that wasn't of that type. If I remember, this didn't work because the compiler didn't want me dereferencing the pointers if it no longer knew their type, even if a given member function had the same name: (*Object5).render() <-doesn't work?

有更好的方法吗?如何商业游戏像HL2处理这个?我想象一定有一些模块等跟踪所有的对象。

Is there a better way? How to commercial games like HL2 handle this? I imagine there must be some module etc. that keeps track of all the objects.

推荐答案

我不知道我完全理解问题,但我想你想要创建一个多态对象的集合。访问多态对象时,必须始终使用指针引用它。

I'm not sure I fully understand the question but I think you are wanting to create a collection of polymorphic objects. When accessing a polymorphic object, you must always refer to it by a pointer.

这里是一个示例。首先,您需要设置一个基类来从中获取对象:

Here is an example. First you need to set up a base class to derive your objects from:

class BaseObject
{
public:
    virtual void Render() = 0;
};

然后创建指针数组。我使用STL集,因为这样可以随意添加和删除成员。

Then create the array of pointers. I use an STL set because that makes it easy to add and remove members at random:

#include <set>

typedef std::set<BaseObject *> GAMEOBJECTS;
GAMEOBJECTS g_gameObjects;

要添加对象,请创建一个派生类并实例化它:

To add an object, create a derived class and instantiate it:

class Enemy : public BaseObject
{
public:
    Enemy() { }
    virtual void Render()
    {
      // Rendering code goes here...
    }
};

g_gameObjects.insert(new Enemy());

然后访问对象,只需遍历它们:

Then to access objects, just iterate through them:

for(GAMEOBJECTS::iterator it = g_gameObjects.begin();
    it != g_gameObjects.end();
    it++)
{
    (*it)->Render();
}

要创建不同类型的对象,只需从类BaseObject派生更多的类。当您从集合中删除对象时,不要忘记删除对象。

To create different types of object, just derive more classes from class BaseObject. Don't forget to delete the objects when you remove them from the collection.

这篇关于在游戏中组织实体的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆