我真的很想将 Lua 脚本添加到我的游戏引擎中。我正在使用 Lua 并使用 luabind 将其绑定到 C++,但我需要帮助来设计使用 Lua 构建游戏实体的方式。
引擎信息:
面向组件,基本上每个都是在增量 T 间隔中更新GameEntity
的列表。components
基本上Game Scenes
由游戏实体的集合组成。
所以,这里的困境是:
假设我有这个 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
”定义。这两个完全不同的组件需要完全不同的初始化参数。健康需要一个整数和一个浮点数(总计和重新生成),另一方面,动画需要一个精灵表名称,并定义所有动画(帧、持续时间等)。
我很想用一些虚拟初始化方法制作某种抽象类,我的所有需要 Lua 绑定的组件都可以使用它,以便从 Lua 进行初始化,但这似乎很困难,因为虚拟类不会有一个虚拟初始化方法。这是因为所有组件初始化器(或大多数)都需要不同的初始化参数(健康组件需要不同的 init 与 Animated Sprite 组件或 AI 组件)。
你有什么建议让 Lua 绑定到这个组件的构造函数更容易?或者你会怎么做?
PS:我必须为这个项目使用 C++。