Appkit: /unit_link.lua code sample - Stingray Lua API Reference
--[[
A component to control linking (attaching) one unit to another. This is a very
thin wrapper around World.link_unit and World.unlink_unit.
]]--
require 'core/appkit/lua/class'
require 'core/appkit/lua/component_manager'
Appkit.UnitLink = Appkit.class(Appkit.UnitLink)
local UnitLink = Appkit.UnitLink
local World = stingray.World
Appkit.ComponentManager.give_manager(UnitLink)
-- If is_linked is not supplied then will link by default.
function UnitLink:init(entity, level, child_unit, child_node_index, parent_unit, parent_node_index, is_linked)
local world = stingray.Level.world(level)
self.world = world
self.entity = entity
self.child_unit = child_unit
self.child_index = child_node_index
self.parent_unit = parent_unit
self.parent_index = parent_node_index
if is_linked == nil or is_linked == true then
World.link_unit(world, child_unit, child_node_index, parent_unit, parent_node_index)
end
self.is_linked = is_linked
UnitLink.manager:add_component(entity, self, level) -- pass level to register for on_level_shutdown
end
function UnitLink:set_parent(parent_unit, parent_node_index)
self.parent_unit = parent_unit
self.parent_index = parent_node_index
end
function UnitLink:set_child(child_unit, child_node_index)
self.child_unit = child_unit
self.child_index = child_node_index
end
function UnitLink:link()
if self.is_linked == true then
print "UnitLink:link: Cannot link, already linked to a unit!"
return
end
if self.child_unit and self.child_index and self.parent_unit and self.parent_index then
World.link_unit(self.world, self.child_unit, self.child_index, self.parent_unit, self.parent_index)
self.is_linked = true
end
end
function UnitLink:unlink()
if self.is_linked == false then
print "UnitLink:unlink: Not linked to a unit!"
return
end
World.unlink_unit(self.world, self.child_unit)
self.is_linked = false
end
function UnitLink:update(dt)
end
function UnitLink:on_level_shutdown()
self:unlink()
end
function UnitLink:shutdown()
self:unlink()
end
return UnitLink