Creature: minor fixes

This commit is contained in:
Van
2026-02-09 22:19:20 +03:00
parent 21232bd1f7
commit 9426ac01dd

View File

@@ -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 ---@param entityDef Creature.CreatureDefinition
---@return EntityDefinition ---@return EntityDefinition
local function entityDefToLuantiDef(entityDef) local function entityDefToLuantiDef(entityDef)
entityDef = tableCopy(entityDef) entityDef = tableCopy(entityDef)
local s = entityDef.settings 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 return luantiDef
end end
---The alias `table.merge` for EmmyLua
---@type fun(...):(Creature.CreatureDefinition)
local defMerge = function(...)
return tableMerge(...)
end
---@param entityDef Creature.CreatureDefinition ---@param entityDef Creature.CreatureDefinition
---@return Creature ---@return Creature
function Creature:new(entityDef) function Creature:new(entityDef)
-- Adding default parameters -- Adding default parameters.
entityDef = defMerge(entityDef, self.defaultDef, true) ---@type Creature.CreatureDefinition
local s = entityDef.settings 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 if s._add_mod_name then
s._name = Mod.getInfo().shortName .. ':' .. s._name s._name = Mod.getInfo().shortName .. ':' .. s._name
end 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 ---@type Creature
local instance = getInstance(self, {}) local instance = getInstance(self, {})
@@ -71,7 +94,9 @@ end
---@param defaultDef Creature.CreatureDefinition ---@param defaultDef Creature.CreatureDefinition
---@return Creature ---@return Creature
function Creature:getExtended(defaultDef) function Creature:getExtended(defaultDef)
defaultDef = defMerge(self.defaultDef, defaultDef) -- Adding default parameters.
---@type Creature.CreatureDefinition
defaultDef = tableMerge(self.defaultDef, defaultDef)
---@type Creature ---@type Creature
local ChildClass = extend(self, { local ChildClass = extend(self, {