local EventSystem = require('Event/EventSystem') local LevelEventCondition = require("Battle/Logic/Level/LevelItemFunction/LevelEventCondition") local LevelEventAction = require("Battle/Logic/Level/LevelItemFunction/LevelEventAction") local LevelEvent = class("LevelEvent") local ipairs=ipairs local table=table local TablePool = require('Infrast/TablePool') local ConditionHandle = require('Battle/Logic/Level/LevelItemFunction/LevelConditionHandle') local ContextAnalyzer = ConditionHandle.GetContextAnalyzer() local ContextPool = TablePool( function(context) return context or { ExecutorIDs = {}, CoveredKillerID = nil, IsSkipTimeLine = false, } end, function(context) table.clearArray(context.ExecutorIDs) context.CoveredKillerID = nil context.IsSkipTimeLine = false end, 64 ) local ActionGroupPool = TablePool( function(group, levelEvent, context) if group then group.Context = context else group = { ActionList = {}, Context = context } end local actions = levelEvent._eventData.Actions for i = #actions, 1,-1 do table.insert(group.ActionList, LevelEventAction:new(actions[i], group, levelEvent)) end return group end, function(group) local actionList = group.ActionList for i = #group.ActionList, 1, -1 do actionList[i]:OnDestroy() actionList[i] = nil end group.Context = nil end, 64 ) --[[ 关卡事件类(LevelEvent) 1、每个运行时关卡事件都会创建一个LevelEvent类型对象,包含条件列表与正在执行的行为列表 2、每个事件都有各自的EventID,非策划配置的事件EventID为-1无法被索引到 3、事件可以被重置 ]]-- --region--***************生命周期***************-- -- 构造函数 ,使用的时候用new function LevelEvent:initialize(eventData) self._conditionGroup = {} self._actionGroupList = {} --存储行为组的列表,一次事件触发一组行为,每组行为进行存储 self._eventData = eventData self._leftCount = eventData.EventTimes -- 剩余可触发的次数 self._isActive = false self._context = nil --用于传递的上下文,当条件达成时,上下文会传递给事件组(ActionGroup) self:OnActive() end function LevelEvent:OnDestroy() self:OnDeactive() self._conditionGroup = nil self._actionGroupList = nil self._eventData = nil self._leftCount = nil self._isActive = nil end function LevelEvent:OnActive() if self._isActive then return end self._isActive = true self._context = ContextPool.TryGet() self:CreateConditionGroup() end function LevelEvent:OnDeactive() if not self._isActive then return end self._isActive = false if self._context then ContextPool.Recycle(self._context) self._context = nil end self:ClearConditionGroup() self:ClearActionGroupList() end function LevelEvent:OnUpdate() if not self._isActive then return end self:UpdateActions() end --endregion 生命周期 function LevelEvent:ResetEvent() if self._context then ContextPool.Reset(self._context) end self:ResetConditionGroup() self:ClearActionGroupList() end --region--***************条件部分***************-- function LevelEvent:CreateConditionGroup() for _,v in ipairs(self._eventData.Conditions) do local condition = LevelEventCondition:new(v,self) table.insert(self._conditionGroup, condition) end end function LevelEvent:ResetConditionGroup() if not self._conditionGroup then return end for _,v in ipairs(self._conditionGroup) do v:OnReset() end end --清除条件组 function LevelEvent:ClearConditionGroup() if not self._conditionGroup then return end for i = 1, #self._conditionGroup do local condition = self._conditionGroup[i] condition:OnDestroy() self._conditionGroup[i] = nil end end -- 收到每一个条件达成的信号,判断是否执行行为 function LevelEvent:OnConditionReach(condition, context) if self._leftCount == 0 then return end if context then --Analysis Context local analyzer = ContextAnalyzer[condition.Data.Type] if analyzer then analyzer(context, self._context) end end --如果达成条件,执行行为 if self:CheckConditionReach(condition) then if self._eventData.EventID > 0 then EventSystem.Dispatch(EventID.BI_LevelMiddle,BIManager.BIType.LevelLayout,self._eventData.EventID,0,"Event Occur") end --更新行为可执行的次数,放在前面,以免执行行为时再次触发条件,造成死循环 if self._leftCount > 0 then self._leftCount = self._leftCount -1 end --创建行为组,传递上下文 local group = ActionGroupPool.TryGet(self, self._context) table.insert(self._actionGroupList, group) --重置事件的条件 self._context = ContextPool.TryGet() --上下文已经传递给actionGroup self:ResetConditionGroup() --开始执行行为 for i = #group.ActionList, 1, -1 do --创建时候是倒序,因此这里是按照配置的顺序执行 local action = group.ActionList[i] if action then --在事件执行行为的过程中,有可能事件的其他行为会使自己Destroy掉,故添加判空 action:OnStart() end end end end --判断条件是否达成 function LevelEvent:CheckConditionReach(condition) if self._eventData.LogicType == 0 then for _,v in ipairs(self._conditionGroup) do if v ~= condition and not v.InReachState then return false end end return true end return true end --endregion 条件部分 --region--***************行为部分***************-- --清除完成了的事件组 function LevelEvent:OnActionDone(group) if #group.ActionList == 0 then table.removebyvalue(self._actionGroupList, group,false) ContextPool.Recycle(group.Context) ActionGroupPool.Recycle(group) end end function LevelEvent:ClearActionGroupList() if self._actionGroupList then for i = 1, #self._actionGroupList do ActionGroupPool.Recycle(self._actionGroupList[i]) self._actionGroupList[i] = nil end end end function LevelEvent:UpdateActions() for i = #self._actionGroupList, 1, -1 do local actionList = self._actionGroupList[i].ActionList for j = #actionList, 1, -1 do local action = actionList[j] if action then --在事件执行行为的过程中,有可能事件的其他行为会使自己Destroy掉,故添加判空 action:OnUpdate() end end end end --endregion 行为部分 return LevelEvent