88 lines
3.4 KiB
Plaintext
Raw Normal View History

2025-07-12 05:06:14 +08:00
-- 控制客户端表现管理
local PerformanceClient = {}
PerformanceClient.pData = {}
--> Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--> Events
local EventsFolder = ReplicatedStorage:FindFirstChild("Events")
local RE_PerformanceEvent = EventsFolder:FindFirstChild("RE_PerformanceEvent")
local RE_CleanPlayerPerformance = EventsFolder:FindFirstChild("RE_CleanPlayerPerformance")
--> Variables
local LocalPlayer = game.Players.LocalPlayer
--------------------------------------------------------------------------------
-- 生成本地化表现目录
local PerformanceFolder = Instance.new("Folder")
PerformanceFolder.Name = "Performance"
PerformanceFolder.Parent = game.Workspace
-- 加载所有客户端行为
local Behaviours = {}
local BehaviourFolder = ReplicatedStorage:FindFirstChild("Modules"):FindFirstChild("BehavioursClient")
if BehaviourFolder then
for _, behaviourModule in ipairs(BehaviourFolder:GetChildren()) do
if behaviourModule:IsA("ModuleScript") then
local success, result = pcall(require, behaviourModule)
if success then
-- 去掉文件名后缀
local name = behaviourModule.Name
Behaviours[name] = result
else
warn("加载代理模块失败: " .. behaviourModule.Name, result)
end
end
end
end
-- 监听表现事件调用
RE_PerformanceEvent.OnClientEvent:Connect(function(ServerTime: number, CastTag: string, CasterPlayer: Player, BehaviourName: string, CastState: boolean, Infos: table)
-- 初始化玩家存储
local UserId = CasterPlayer.UserId
if not PerformanceClient.pData[UserId] then PerformanceClient.pData[UserId] = {} end
local delayTime = ServerTime - tick()
if CastTag == "Init" then
print("Init", BehaviourName)
-- 暂时就新增一个表不调用初始化因为服务端不是多个new做的而是多次调用
if not PerformanceClient.pData[UserId][BehaviourName] then
PerformanceClient.pData[UserId][BehaviourName] = {}
end
elseif CastTag == "Show" then
print("Show", BehaviourName, Behaviours)
-- 直接调用init和对应的show(因为服务端只new一次客户端多次调用)
if not PerformanceClient.pData[UserId][BehaviourName] then return end
for _, CastInfo in pairs(Infos) do
local BehaviourTable = PerformanceClient.pData[UserId][BehaviourName]
BehaviourTable[CastInfo.UniqueId] = Behaviours[BehaviourName]:Init(CasterPlayer, CastInfo, delayTime, CastState)
BehaviourTable[CastInfo.UniqueId]:Show(CasterPlayer, CastInfo, delayTime, CastState)
end
elseif CastTag == "Destroy" then
if not PerformanceClient.pData[UserId][BehaviourName] then return end
for _, CastInfo in pairs(Infos) do
local Behaviour = PerformanceClient.pData[UserId][BehaviourName][CastInfo.UniqueId]
if Behaviour and Behaviour.Destroy then
Behaviour:Destroy()
end
end
end
end)
-- 清理玩家所有表现数据
RE_CleanPlayerPerformance.OnClientEvent:Connect(function(CleanedPlayer: Player)
-- print("CleanPlayerPerformance", CleanedPlayer)
local UserId = CleanedPlayer.UserId
if not PerformanceClient.pData[UserId] then return end
for _, BehaviourList in pairs(PerformanceClient.pData[UserId]) do
for _, Behaviour in pairs(BehaviourList) do
Behaviour:Destroy()
end
end
PerformanceClient.pData[UserId] = nil
end)
return PerformanceClient