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

122 lines
2.7 KiB
Lua
Raw Normal View History

2021-04-20 13:58:00 +08:00
BattleUnit = {}
2020-11-01 15:46:48 +08:00
2021-04-27 13:32:41 +08:00
function BattleUnit.New(go, role, position, root, isBoss)
2020-11-01 15:46:48 +08:00
local o = {}
setmetatable(o, {__index = BattleUnit})
return o
end
function BattleUnit:ctor(go, role, position, root, isBoss,enemyId)
2020-11-01 15:46:48 +08:00
self._DelayFuncList = {}
self._LoopFuncList = {}
if self.onCreate then
self:onCreate(go, role, position, root, isBoss,enemyId)
2020-11-01 15:46:48 +08:00
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 time or time <= 0 then
if func then
func()
end
return
end
--
2020-11-01 15:46:48 +08:00
if not self._DelayFuncList then
self._DelayFuncList = {}
end
local timer
timer = Timer.New(function ()
if self._DelayFuncList[timer] then
self._DelayFuncList[timer]()
end
2020-11-01 15:46:48 +08:00
self:ClearDelayFunc(timer)
end, time)
timer:Start()
--
self._DelayFuncList[timer] = func
2020-11-01 15:46:48 +08:00
end
function BattleUnit:ClearDelayFunc(t)
if not self._DelayFuncList then return end
if t then
if self._DelayFuncList[t] then
t:Stop()
self._DelayFuncList[t] = nil
2020-11-01 15:46:48 +08:00
end
else
for timer, func in pairs(self._DelayFuncList) do
2020-11-01 15:46:48 +08:00
timer:Stop()
end
self._DelayFuncList = {}
end
end
--++++++++++++++DelayFunc
--++++++++++++++LoopFunc 循环执行方法
function BattleUnit:LoopFunc(time, count, func)
if count <= 0 then
2021-03-02 15:59:29 +08:00
LogError("无法用于无限循环的方法")
2020-11-01 15:46:48 +08:00
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
2021-03-04 15:16:23 +08:00
--++++++++++++++LoopFunc