Creature: minor fixes
This commit is contained in:
@@ -17,41 +17,64 @@ local Creature = {
|
||||
}
|
||||
}
|
||||
|
||||
---The alias `table.merge` for EmmyLua
|
||||
---@type fun(...):(EntityDefinition)
|
||||
local defMerge = function(...)
|
||||
return tableMerge(...)
|
||||
end
|
||||
|
||||
---@param entityDef Creature.CreatureDefinition
|
||||
---@return EntityDefinition
|
||||
local function entityDefToLuantiDef(entityDef)
|
||||
entityDef = tableCopy(entityDef)
|
||||
local s = entityDef.settings
|
||||
local c = entityDef.callbacks
|
||||
local c = entityDef.callbacks or {}
|
||||
|
||||
local luantiDef = defMerge(s, c)
|
||||
---@type EntityDefinition
|
||||
local luantiDef = tableMerge(s, c)
|
||||
|
||||
return luantiDef
|
||||
end
|
||||
|
||||
---The alias `table.merge` for EmmyLua
|
||||
---@type fun(...):(Creature.CreatureDefinition)
|
||||
local defMerge = function(...)
|
||||
return tableMerge(...)
|
||||
end
|
||||
|
||||
---@param entityDef Creature.CreatureDefinition
|
||||
---@return Creature
|
||||
function Creature:new(entityDef)
|
||||
-- Adding default parameters
|
||||
entityDef = defMerge(entityDef, self.defaultDef, true)
|
||||
local s = entityDef.settings
|
||||
-- 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
|
||||
|
||||
-- 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(self, 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
|
||||
|
||||
---@type LuaEntity
|
||||
self = tableMerge(self, initCustomFields)
|
||||
|
||||
-- If an initialization callback was specified for the entity, then we call it.
|
||||
if c.on_activate ~= nil then
|
||||
onActivate(self, staticdata, dtime)
|
||||
end
|
||||
end
|
||||
|
||||
---@type Creature
|
||||
local instance = getInstance(self, {})
|
||||
|
||||
@@ -71,7 +94,9 @@ end
|
||||
---@param defaultDef Creature.CreatureDefinition
|
||||
---@return Creature
|
||||
function Creature:getExtended(defaultDef)
|
||||
defaultDef = defMerge(self.defaultDef, defaultDef)
|
||||
-- Adding default parameters.
|
||||
---@type Creature.CreatureDefinition
|
||||
defaultDef = tableMerge(self.defaultDef, defaultDef)
|
||||
|
||||
---@type Creature
|
||||
local ChildClass = extend(self, {
|
||||
|
||||
Reference in New Issue
Block a user