129 lines
3.7 KiB
Plaintext
129 lines
3.7 KiB
Plaintext
local Behaviour = {}
|
||
Behaviour.__index = Behaviour
|
||
|
||
--> Services
|
||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||
local ServerStorage = game:GetService("ServerStorage")
|
||
|
||
--> Events
|
||
local RE_PerformanceEvent = ReplicatedStorage:FindFirstChild("Events"):FindFirstChild("RE_PerformanceEvent")
|
||
local RE_CleanPlayerPerformance = ReplicatedStorage:FindFirstChild("Events"):FindFirstChild("RE_CleanPlayerPerformance")
|
||
|
||
--> Dependencies
|
||
local TypeList = require(ServerStorage.Base.TypeList)
|
||
local Communicate = require(ServerStorage.Modules.Tools.Communicate)
|
||
|
||
--------------------------------------------------------------------------------
|
||
|
||
-- 刷新时,重新载入,暂时不考虑性能
|
||
|
||
-- 初始化内容
|
||
function Behaviour:Init(PlayerAI, Character: TypeList.Character, ScriptName: string)
|
||
local self = {}
|
||
self.PlayerAI = PlayerAI
|
||
self.Character = Character
|
||
self.CheckData = nil
|
||
self.ExeTask = nil
|
||
self.UniqueIdList = {}
|
||
self.ScriptName = ScriptName
|
||
self.Cooldown = 0
|
||
self.OrgCooldown = 0
|
||
self.CooldownTask = nil
|
||
|
||
local Humanoid = self.Character.Humanoid
|
||
-- 监听属性变化
|
||
self.ConAttribtueChanged = Humanoid.AttributeChanged:Connect(function(attributeKey: string, attributeValue: any)
|
||
-- 以后这里要是有其他状态也可以加打断
|
||
if attributeKey == "Died" and attributeValue == true then
|
||
self:OnDied()
|
||
end
|
||
end)
|
||
return self
|
||
end
|
||
|
||
-- 执行检查(主要重写部分) return 优先级, 执行数据
|
||
function Behaviour:Check(CheckInfo: table)
|
||
-- 返回优先级,执行数据
|
||
return -1, self.CheckData
|
||
end
|
||
|
||
-- 具体执行内容(主要重写部分)
|
||
function Behaviour:Execute()
|
||
|
||
end
|
||
|
||
-- 启动冷却时间清除计时
|
||
function Behaviour:StartCooldownTask()
|
||
self.Cooldown = self.OrgCooldown
|
||
self.CooldownTask = task.spawn(function()
|
||
task.wait(self.OrgCooldown)
|
||
self.Cooldown = 0
|
||
end)
|
||
end
|
||
|
||
function Behaviour:SendPerformanceEvent(...)
|
||
Communicate:SendToClient(RE_PerformanceEvent, ...)
|
||
end
|
||
|
||
-- 检查当前状态是否可执行
|
||
function Behaviour:CheckStat()
|
||
if not self.Character then return true end
|
||
-- 死亡检查
|
||
if self.Character:GetState("Died") then return true end
|
||
|
||
-- 冷却中检查
|
||
if self.Cooldown > 0 then return true end
|
||
|
||
-- 执行状态中检查
|
||
local ExecutingState = self.PlayerAI.ExecutingState
|
||
-- 其他内容执行中,就false
|
||
if ExecutingState == true then return true end
|
||
return false
|
||
end
|
||
|
||
-- 改变当前执行状态标记
|
||
function Behaviour:ChangeExecutingState(State: boolean)
|
||
if not self.Character then warn("Behaviour Character not found") return end
|
||
self.PlayerAI.ExecutingState = State
|
||
end
|
||
|
||
-- 销毁
|
||
function Behaviour:Destroy()
|
||
-- 清除客户端对应行为表现
|
||
for _, UniqueId in self.UniqueIdList do
|
||
self:SendPerformanceEvent("Destroy", self.Player, self.ScriptName, true, {UniqueId = UniqueId})
|
||
end
|
||
if self.CooldownTask then
|
||
task.cancel(self.CooldownTask)
|
||
self.CooldownTask = nil
|
||
end
|
||
if self.ExeTask then
|
||
task.cancel(self.ExeTask)
|
||
self.ExeTask = nil
|
||
end
|
||
-- 清除数据
|
||
self.UniqueIdList = {}
|
||
if self.ConAttribtueChanged then
|
||
self.ConAttribtueChanged:Disconnect()
|
||
self.ConAttribtueChanged = nil
|
||
end
|
||
if self.LoopTask then
|
||
task.cancel(self.LoopTask)
|
||
self.LoopTask = nil
|
||
end
|
||
for _, UniqueId in self.UniqueIdList do
|
||
self.PlayerAI:RemoveBehaviourUniqueId(UniqueId)
|
||
end
|
||
self.UniqueIdList = {}
|
||
self = nil
|
||
end
|
||
|
||
-- 死亡时,主要打断当前执行状态
|
||
function Behaviour:OnDied()
|
||
if self.ExeTask then
|
||
task.cancel(self.ExeTask)
|
||
self.ExeTask = nil
|
||
end
|
||
end
|
||
|
||
return Behaviour |