c1_lua/lua/app/module/battle/helper/battle_helper.lua
2023-04-19 11:53:31 +08:00

131 lines
3.9 KiB
Lua

local UIPrefabObject = require "app/bf/unity/uiprefab_object"
local BattleHelper = {}
function BattleHelper:init()
self.isClear = false
self.characterPools = {}
self.characterMap = {}
self.buffEffectPool = {}
self.battleEffectTextMap = {}
self.battleEffectTextPool = {}
self.seed = tonumber(tostring(os.time()):reverse():sub(1,6))
end
function BattleHelper:setTimeScale(timeScale)
if self.effectMap then
for k, effect in pairs(self.effectMap) do
effect:setTimeScale(timeScale)
end
end
if self.characterMap then
for k, character in pairs(self.characterMap) do
character:setTimeScale(timeScale)
end
end
end
function BattleHelper:random(min, max)
self.seed = (self.seed*9301 + 49297)%233280
return min + self.seed*(max - min + 1)//233280
end
function BattleHelper:loadBattleHeroModel(id, parent, callback)
local pool = self.characterPools[id]
if pool and #pool > 0 then
local spineObject = table.remove(pool)
spineObject:setActive(true)
if self.characterMap then
self.characterMap[spineObject:getInstanceID()] = spineObject
end
local timeScale = DataManager.BattleData:getTimeScale()
spineObject:setTimeScale(timeScale)
callback(spineObject)
else
SpineManager:loadHeroAsync(id, parent, function(spineObject)
spineObject:setDefaultMix(0)
if self.characterMap then
self.characterMap[spineObject:getInstanceID()] = spineObject
local timeScale = DataManager.BattleData:getTimeScale()
spineObject:setTimeScale(timeScale)
callback(spineObject)
end
end)
end
end
function BattleHelper:recycleBattleHeroModel(modelId, character)
if character:isDestroyed() then
return
end
character:setActive(false)
if self.characterMap then
self.characterMap[character:getInstanceID()] = nil
end
if self.characterPools then
local pool = self.characterPools[modelId]
if pool == nil then
pool = {}
self.characterPools[modelId] = pool
end
table.insert(pool, character)
end
end
function BattleHelper:setEffectTextCache(effectTextCache)
self.effectTextCache = effectTextCache
end
function BattleHelper:getEffectText(parent)
if #self.battleEffectTextPool <= 0 then
local prefab = CS.UnityEngine.Object.Instantiate(self.effectTextCache:getGameObject())
local prefabObject = UIPrefabObject:create()
prefabObject:initWithPrefab(self.effectTextCache:getAssetPath(), prefab)
prefabObject:initPrefabHelper()
prefabObject:getTransform():SetAsLastSibling()
prefabObject:setParent(parent, false)
local comp = prefabObject:addLuaComponent(GConst.BattleConst.TYPEOF_LUA_COMP.BATTLE_NUMBER_COMPONENT)
comp:setEnabled(true)
return comp
else
local effectComp = table.remove(self.battleEffectTextPool)
effectComp:setEnabled(true)
effectComp.baseObject:getTransform():SetAsLastSibling()
self.battleEffectTextMap[effectComp.baseObject:getInstanceID()] = effectComp
return effectComp
end
end
function BattleHelper:recycleEffectText(comp)
if comp:isDestroyed() then
return
end
comp:setEnabled(false)
table.insert(self.battleEffectTextPool, comp)
if self.battleEffectTextMap then
self.battleEffectTextMap[comp.baseObject:getInstanceID()] = nil
end
end
function BattleHelper:getBuffEffect()
if #self.buffEffectPool > 0 then
return table.remove(self.buffEffectPool)
end
return {}
end
function BattleHelper:recycleBuffEffect(buffEffect)
table.insert(self.buffEffectPool, buffEffect)
end
function BattleHelper:clear()
self.isClear = true
self.characterPools = nil
self.characterMap = nil
self.effectTextCache = nil
self.buffEffectPool = nil
self.battleEffectTextMap = nil
self.battleEffectTextPool = nil
end
return BattleHelper