miduo_client/Assets/ManagedResources/~Lua/Modules/Battle/View/Unit/BattleUnit.lua

118 lines
2.7 KiB
Lua

BattleUnit = {}
function BattleUnit.New(go, role, position, root)
local o = {}
setmetatable(o, {__index = BattleUnit})
return o
end
function BattleUnit:ctor(go, role, position, root)
self._DelayFuncList = {}
self._LoopFuncList = {}
if self.onCreate then
self:onCreate(go, role, position, root)
end
end
--
function BattleUnit:OnSortingOrderChange(battleSorting)
return
end
function BattleUnit:Dispose()
-- 清空所有延迟方法
self:ClearDelayFunc()
self:ClearLoopFunc()
if self.onDispose then
self:onDispose()
end
end
function BattleUnit:onDispose()
end
--++++++++++++++DelayFunc 延迟执行方法
function BattleUnit:DelayFunc(time, func)
if not self._DelayFuncList then
self._DelayFuncList = {}
end
local timer
timer = Timer.New(function ()
if func then func() func = nil end
self:ClearDelayFunc(timer)
end, time)
timer:Start()
table.insert(self._DelayFuncList, timer)
end
function BattleUnit:ClearDelayFunc(t)
if not self._DelayFuncList then return end
if t then
local rIndex
for index, timer in ipairs(self._DelayFuncList) do
if timer == t then
rIndex = index
break
end
end
if rIndex then
self._DelayFuncList[rIndex]:Stop()
table.remove(self._DelayFuncList, rIndex)
end
else
for _, timer in ipairs(self._DelayFuncList) do
timer:Stop()
end
self._DelayFuncList = {}
end
end
--++++++++++++++DelayFunc
--++++++++++++++LoopFunc 循环执行方法
function BattleUnit:LoopFunc(time, count, func)
if count <= 0 then
LogError("无法用于无限循环的方法")
return
end
if not self._LoopFuncList then
self._LoopFuncList = {}
end
local timer = nil
local ctr = 0
timer = Timer.New(function ()
if func then func() end
ctr = ctr + 1
if ctr >= count then
self:ClearDelayFunc(timer)
end
end, time, count)
timer:Start()
table.insert(self._LoopFuncList, timer)
end
function BattleUnit:ClearLoopFunc(t)
if not self._LoopFuncList then return end
if t then
local rIndex
for index, timer in ipairs(self._LoopFuncList) do
if timer == t then
rIndex = index
break
end
end
if rIndex then
self._LoopFuncList[rIndex]:Stop()
table.remove(self._LoopFuncList, rIndex)
end
else
for _, timer in ipairs(self._LoopFuncList) do
timer:Stop()
end
self._LoopFuncList = {}
end
end
--++++++++++++++LoopFunc