2023-07-14 19:35:33 +00:00
|
|
|
--[[--红点事件个体类
|
|
|
|
RedPointEvent.id 唯一Id
|
|
|
|
RedPointEvent.conditionGroup 类型 XRedPointConditionGroup
|
|
|
|
RedPointEvent.listener 类型 XRedPointListener
|
|
|
|
RedPointEvent.node 持有的节点用于判断释放
|
|
|
|
]]
|
|
|
|
--
|
2024-09-01 20:49:41 +00:00
|
|
|
---@class XRedPointEvent 红点事件个体类
|
|
|
|
---@field
|
2023-07-14 19:35:33 +00:00
|
|
|
local XRedPointEvent = XClass(nil, "XRedPointEvent")
|
2024-09-01 20:49:41 +00:00
|
|
|
---@type XObjectPool
|
|
|
|
local RedPointConditionGroupPool
|
|
|
|
---@type XObjectPool
|
|
|
|
local RedPointListenerPool
|
2023-07-14 19:35:33 +00:00
|
|
|
|
|
|
|
local EventHandler = function(method, eventId)
|
|
|
|
return function(obj, ...)
|
|
|
|
return method(obj, eventId, ...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--构造
|
2024-09-01 20:49:41 +00:00
|
|
|
function XRedPointEvent:Ctor()
|
|
|
|
end
|
|
|
|
|
|
|
|
function XRedPointEvent:Init(id, node, conditionGroup, listener, func, args)
|
|
|
|
self.EventListener = {}
|
2023-07-14 19:35:33 +00:00
|
|
|
self.id = id
|
2024-09-01 20:49:41 +00:00
|
|
|
self.condition = RedPointConditionGroupPool:Create(conditionGroup)
|
|
|
|
self.listener = RedPointListenerPool:Create(listener, func)
|
2023-07-14 19:35:33 +00:00
|
|
|
self.node = node
|
|
|
|
self.args = args
|
2024-09-01 20:49:41 +00:00
|
|
|
self:AddConditionsChangeEvent()
|
2023-07-14 19:35:33 +00:00
|
|
|
|
|
|
|
self.checkExist = nil
|
|
|
|
|
|
|
|
if node.Exist then
|
|
|
|
self.checkExist = function() return node:Exist() end
|
|
|
|
else
|
|
|
|
local gameObject = node.GameObject or node.gameObject or node.Transform or node.transform
|
|
|
|
if gameObject and gameObject.Exist then
|
|
|
|
self.checkExist = function() return gameObject:Exist() end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--检测红点条件
|
|
|
|
function XRedPointEvent:Check(args)
|
|
|
|
|
|
|
|
if not self:CheckNode() then
|
|
|
|
self:Release()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.condition then
|
|
|
|
--如果条件参数改变,则替换
|
|
|
|
if args then
|
|
|
|
self.args = args
|
|
|
|
end
|
|
|
|
|
|
|
|
--条件检测
|
|
|
|
local result = self.condition:Check(self.args)
|
|
|
|
|
|
|
|
--回调
|
|
|
|
if self.listener then
|
|
|
|
if self.listener.func then
|
|
|
|
self.listener:Call(result, self.args)
|
|
|
|
else
|
|
|
|
self.node.gameObject:SetActive(result >= 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--添加事件監聽
|
2024-09-01 20:49:41 +00:00
|
|
|
function XRedPointEvent:AddConditionsChangeEvent()
|
2023-07-14 19:35:33 +00:00
|
|
|
if not self.condition then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local events = self.condition.Events
|
|
|
|
|
|
|
|
if not events then
|
|
|
|
return
|
|
|
|
end
|
2024-09-01 20:49:41 +00:00
|
|
|
|
2023-07-14 19:35:33 +00:00
|
|
|
for _, var in pairs(events) do
|
2024-09-01 20:49:41 +00:00
|
|
|
local func = EventHandler(self.OnConditionChange, var.EventId)
|
|
|
|
XEventManager.AddEventListener(var.EventId, func, self)
|
|
|
|
self.EventListener[var.EventId] = func
|
2023-07-14 19:35:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--删除事件監聽
|
2024-09-01 20:49:41 +00:00
|
|
|
function XRedPointEvent:RemoveConditionsChangeEvent()
|
2023-07-14 19:35:33 +00:00
|
|
|
if not self.condition then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local events = self.condition.Events
|
|
|
|
|
|
|
|
if not events then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, var in pairs(events) do
|
2024-09-01 20:49:41 +00:00
|
|
|
local func = self.EventListener[var.EventId]
|
|
|
|
if func then
|
|
|
|
XEventManager.RemoveEventListener(var.EventId, func, self)
|
|
|
|
end
|
|
|
|
self.EventListener[var.EventId] = nil
|
2023-07-14 19:35:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--条件改变事件回调
|
2024-09-01 20:49:41 +00:00
|
|
|
function XRedPointEvent:OnConditionChange(eventId, args)
|
2023-07-14 19:35:33 +00:00
|
|
|
|
|
|
|
-- 分析参数
|
|
|
|
if self.condition and self.condition.Events and self.condition.Events[eventId] then
|
|
|
|
local element = self.condition.Events[eventId]
|
|
|
|
if element:Equal(eventId, args) then
|
|
|
|
self:Check()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.args == nil or args == nil then
|
|
|
|
self:Check(args)
|
|
|
|
elseif self.args == args and args ~= nil then
|
|
|
|
self:Check(args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--检测是否已经被释放
|
|
|
|
function XRedPointEvent:CheckNode()
|
|
|
|
if self.checkExist == nil then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if not self.checkExist() then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--释放
|
|
|
|
function XRedPointEvent:Release()
|
2024-09-01 20:49:41 +00:00
|
|
|
self:RemoveConditionsChangeEvent()
|
2023-07-14 19:35:33 +00:00
|
|
|
self.checkExist = nil
|
2024-09-01 20:49:41 +00:00
|
|
|
if self.listener then
|
|
|
|
self.listener:Release()
|
|
|
|
RedPointListenerPool:Recycle(self.listener)
|
|
|
|
self.listener = nil
|
|
|
|
end
|
2023-07-14 19:35:33 +00:00
|
|
|
self.node = nil
|
2024-09-01 20:49:41 +00:00
|
|
|
if self.condition then
|
|
|
|
self.condition:Release()
|
|
|
|
RedPointConditionGroupPool:Recycle(self.condition)
|
|
|
|
self.condition = nil
|
|
|
|
end
|
2023-07-14 19:35:33 +00:00
|
|
|
XRedPointManager.RemoveRedPointEventOnly(self.id)
|
2024-09-01 20:49:41 +00:00
|
|
|
|
|
|
|
for eventId, func in pairs(self.EventListener or {}) do
|
|
|
|
XEventManager.RemoveEventListener(eventId, func, self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--导入时调用一次
|
|
|
|
local function OnRequire()
|
|
|
|
if not RedPointConditionGroupPool then
|
|
|
|
RedPointConditionGroupPool = XObjectPool.New(XRedPointConditionGroup.New)
|
|
|
|
end
|
|
|
|
if not RedPointListenerPool then
|
|
|
|
RedPointListenerPool = XObjectPool.New(XRedPointListener.New)
|
|
|
|
end
|
2023-07-14 19:35:33 +00:00
|
|
|
end
|
2024-09-01 20:49:41 +00:00
|
|
|
OnRequire()
|
2023-07-14 19:35:33 +00:00
|
|
|
|
|
|
|
return XRedPointEvent
|