c++ - 集成 Lua 以在我的游戏引擎中构建我的 GameEntities?

标签 c++ lua components game-engine

我真的很想将 Lua 脚本添加到我的游戏引擎中。我正在使用 Lua 并使用 luabind 将其绑定(bind)到 C++,但我需要帮助来设计使用 Lua 构建游戏实体的方式。

引擎信息:

面向组件,基本上每个 GameEntity 都是 components 的列表,它们在 delta T 间隔内更新。基本上,游戏场景 由游戏实体的集合组成。

所以,这是一个难题:

假设我有这个 Lua 文件来定义一个 GameEntity 及其组件:

GameEntity = 
{
 -- Entity Name
 "ZombieFighter",

 -- All the components that make the entity.
 Components = 
 {
  -- Component to define the health of the entity
  health = 
  {
   "compHealth",  -- Component In-Engine Name
   100,    -- total health
   0.1,    -- regeneration rate
  },

  -- Component to define the Animations of the entity
  compAnimation =
  {
   "compAnimatedSprite",

   spritesheet = 
   {
    "spritesheet.gif", -- Spritesheet file name.
    80,     -- Spritesheet frame width.
    80,     -- Spritesheet frame height.
   },

   animations =
   {
    -- Animation name, Animation spritesheet coords, Animation frame duration.
    {"stand", {0,0,1,0,2,0,3,0}, 0.10},
    {"walk", {4,0,5,0,6,0,7,0}, 0.10},
    {"attack",{8,0,9,0,10,0}, 0.08},
   },
  },
 },
}

如您所见,这个 GameEntity 由 2 个组件定义,“compHealth”和“compAnimatedSprite”。这两个完全不同的组件需要完全不同的初始化参数。 Health 需要一个整数和一个 float (total 和 regeneration),另一方面,动画需要一个 Sprite 表名称,并定义所有动画(帧、持续时间等)。

我很想用一些虚拟初始化方法制作某种抽象类,我所有需要 Lua 绑定(bind)的组件都可以使用这些方法,以便于从 Lua 进行初始化,但这似乎很难,因为虚拟类不会有一个虚拟初始化方法。这是因为所有组件初始化程序(或其中大部分)需要不同的初始化参数(健康组件需要与动画 Sprite 组件或 AI 组件不同的初始化)。

您有什么建议可以使 Lua 绑定(bind)到此组件的构造函数更容易?或者你会怎么做?

PS:这个项目我必须使用C++。

最佳答案

我建议使用 composite structure而不是您的游戏实体。当您在解析 Lua 配置表时遇到它们时,将继承自公共(public)游戏实体组​​件的对象添加到每个游戏实体。此任务非常适合 factory method .请注意,以这种方式组合您的实体仍然需要在 GameEntity 类中实现所有方法,除非您使用消息传递等替代调度方法(请参阅 visitor pattern)

在 Lua 方面,我发现使用带有单个表参数的回调函数比在 C/C++ 中遍历复杂的表结构更方便。这是我使用您的数据的纯 Lua 示例。

-- factory functions
function Health(t) return { total = t[1], regeneration = t[2] } end
function Animation(t) return { spritesheet = t[1], animations = t[2] } end
function Spritesheet(t)
    return { filename = t[1], width = t[2], height = t[3] }
end
function Animations(t)
    return { name = t[1], coords = t[2], duration = t[3] }
end

-- game entity class prototype and constructor
GameEntity = {}
setmetatable(GameEntity, {
    __call = function(self, t)
        setmetatable(t, self)
        self.__index = self
        return t
    end,
})

-- example game entity definition
entity = GameEntity{
    "ZombieFighter",
    Components = {
        Health{ 100, 0.1, },
        Animation{
            Spritesheet{ "spritesheet.gif", 80, 80 },
            Animations{
                {"stand",  {0,0,1,0,2,0,3,0}, 0.10},
                {"walk",   {4,0,5,0,6,0,7,0}, 0.10},
                {"attack", {8,0,9,0,10,0},    0.08},
            },
        },
    }
}

-- recursively walk the resulting table and print all key-value pairs
function print_table(t, prefix)
    prefix = prefix or ''
    for k, v in pairs(t) do
        print(prefix, k, v)
        if 'table' == type(v) then
            print_table(v, prefix..'\t')
        end
    end
end    
print_table(entity)

关于c++ - 集成 Lua 以在我的游戏引擎中构建我的 GameEntities?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3593959/

相关文章:

c++ - 什么是结构的非成员?

c++ - 在成员函数中返回类对象是什么意思?

c++ - 作为函数指针的静态方法

c++ - 在没有 new 和 delete 关键字的情况下编写 C++?最好的方法?

lua - 如何在ubuntu/linux中使用luadoc?

angular - 如何在 Angular 2 项目中使用 Bower 组件

android - Corona SDK 显示多行通知

c - 使用 Lua C Api 从索引中获取数组值

Delphi - 如何在运行时删除所有子组件?

java - 如何禁用 JPanel 中的所有组件