109 lines
2.8 KiB
Lua
109 lines
2.8 KiB
Lua
local getInstance, extend = Class.getInstance, Class.extend
|
|
|
|
local tableCopy, tableMerge = table.copy, table.merge
|
|
|
|
---@class Creature
|
|
---@field entityDef Creature.CreatureDefinition
|
|
---@field luantiDef EntityDefinition
|
|
---@field defaultDef Creature.CreatureDefinition
|
|
local Creature = {
|
|
defaultDef = {
|
|
settings = {
|
|
_name = '',
|
|
_add_mod_name = true,
|
|
initial_properties = {},
|
|
},
|
|
callbacks = {}
|
|
}
|
|
}
|
|
|
|
---@param entityDef Creature.CreatureDefinition
|
|
---@return EntityDefinition
|
|
local function entityDefToLuantiDef(entityDef)
|
|
entityDef = tableCopy(entityDef)
|
|
local s = entityDef.settings
|
|
local c = entityDef.callbacks or {}
|
|
|
|
local luantiDef = tableMerge(s, c) --[[@as EntityDefinition]]
|
|
|
|
return luantiDef
|
|
end
|
|
|
|
---@param entityDef Creature.CreatureDefinition
|
|
---@return Creature
|
|
function Creature:new(entityDef)
|
|
-- Adding default parameters.
|
|
---@type Creature.CreatureDefinition
|
|
entityDef = tableMerge(entityDef, self.defaultDef, true)
|
|
|
|
-- The callback table may not be specified.
|
|
if entityDef.callbacks == nil then
|
|
---@type Creature.Callbacks
|
|
entityDef.callbacks = {}
|
|
end
|
|
|
|
local s = entityDef.settings
|
|
local c = entityDef.callbacks
|
|
assert(c ~= nil)
|
|
|
|
-- Add the short mod name prefix to the entity name.
|
|
if s._add_mod_name then
|
|
s._name = Mod.getInfo().shortName .. ':' .. s._name
|
|
end
|
|
|
|
local onActivate = c.on_activate or function() end
|
|
|
|
-- We rewrite the entity initialization function so that custom fields
|
|
-- can be passed to it directly via `core.add_entity(pos, name, [staticdata])`.
|
|
c.on_activate = function(luaEntity, staticdata, dtime)
|
|
local initCustomFields = {}
|
|
|
|
if staticdata and staticdata ~= '' then
|
|
initCustomFields = core.deserialize(staticdata) or {}
|
|
end
|
|
|
|
-- We reset the fields that should not be overwritten.
|
|
initCustomFields.name = nil
|
|
initCustomFields.object = nil
|
|
|
|
self = tableMerge(self, initCustomFields) --[[@as LuaEntity]]
|
|
|
|
-- If an initialization callback was specified for the entity, then we call it.
|
|
if c.on_activate ~= nil then
|
|
onActivate(luaEntity, staticdata, dtime)
|
|
end
|
|
end
|
|
|
|
---@type Creature
|
|
local instance = getInstance(self, {})
|
|
|
|
local luantiDef = entityDefToLuantiDef(entityDef)
|
|
|
|
instance.entityDef = entityDef
|
|
instance.luantiDef = luantiDef
|
|
|
|
core.register_entity(':'..s._name, luantiDef)
|
|
|
|
return instance
|
|
end
|
|
|
|
---Allows you to create a child class of an entity by specifying
|
|
---default parameters that will be added to the entity's parameters
|
|
---when an instance is received.
|
|
---@param defaultDef Creature.CreatureDefinition
|
|
---@return Creature
|
|
function Creature:getExtended(defaultDef)
|
|
-- Adding default parameters.
|
|
---@type Creature.CreatureDefinition
|
|
defaultDef = tableMerge(self.defaultDef, defaultDef)
|
|
|
|
---@type Creature
|
|
local ChildClass = extend(self, {
|
|
defaultDef = defaultDef,
|
|
})
|
|
|
|
return ChildClass
|
|
end
|
|
|
|
return Creature
|