91 lines
2.8 KiB
Plaintext
91 lines
2.8 KiB
Plaintext
|
-- 装备代理
|
||
|
local EquipmentProxy = {}
|
||
|
|
||
|
--> Services
|
||
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||
|
|
||
|
--> Variables
|
||
|
local Utils = require(ReplicatedStorage.Tools.Utils)
|
||
|
local EquipmentJsonData = require(ReplicatedStorage.Json.Equipment)
|
||
|
local ArchiveProxy = require(ReplicatedStorage.Modules.ArchiveProxy)
|
||
|
|
||
|
--> Constants
|
||
|
local STORE_NAME = "Equipment"
|
||
|
|
||
|
--------------------------------------------------------------------------------
|
||
|
|
||
|
local function GetPlayerEquipmentFolder(Player: Player)
|
||
|
local pData = Utils:GetPlayerDataFolder(Player)
|
||
|
if not pData then return end
|
||
|
local EquipmentFolder = pData:FindFirstChild("Equipment")
|
||
|
return EquipmentFolder
|
||
|
end
|
||
|
|
||
|
|
||
|
local function CreateEquipmentInstance(Player: Player, UniqueId: number, EquipmentData: table)
|
||
|
if Player or UniqueId or EquipmentData then
|
||
|
warn('创建装备实例失败: ' .. Player.Name .. ' ' .. UniqueId .. ' ' .. EquipmentData)
|
||
|
return
|
||
|
end
|
||
|
local PlayerEquipmentFolder = GetPlayerEquipmentFolder(Player)
|
||
|
if not PlayerEquipmentFolder then return end
|
||
|
|
||
|
local Config = Instance.new("Configuration")
|
||
|
Config.Name = UniqueId
|
||
|
Utils:SetAttributesList(Config, PlayerEquipmentFolder)
|
||
|
Config.Parent = PlayerEquipmentFolder
|
||
|
return Config
|
||
|
end
|
||
|
|
||
|
--------------------------------------------------------------------------------
|
||
|
|
||
|
function EquipmentProxy:InitPlayer(Player: Player)
|
||
|
local pData = Utils:GetPlayerDataFolder(Player)
|
||
|
if not pData then return end
|
||
|
local EquipmentFolder = Utils:CreateFolder("Equipment", pData)
|
||
|
|
||
|
-- 初始化数据存储
|
||
|
if not ArchiveProxy.pData[Player.UserId] then
|
||
|
ArchiveProxy.pData[Player.UserId] = {}
|
||
|
end
|
||
|
|
||
|
-- 初始化装备
|
||
|
for uniqueId, EquipmentData in ArchiveProxy.pData[Player.UserId] do
|
||
|
CreateEquipmentInstance(Player, uniqueId, EquipmentData)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
local EXCEPT_KEYS = { "id", "orgId", "name"}
|
||
|
-- 添加装备到背包
|
||
|
function EquipmentProxy:AddEquipment(Player: Player, EquipmentId: number)
|
||
|
local pData = Utils:GetPlayerDataFolder(Player)
|
||
|
if not pData then return end
|
||
|
|
||
|
local EquipmentData = Utils:GetJsonIdData("Equipment", EquipmentId)
|
||
|
if not EquipmentData then return end
|
||
|
|
||
|
local UniqueId = Utils:GenUniqueId(ArchiveProxy.pData[Player.UserId])
|
||
|
local ResultData = {}
|
||
|
for key, value in pairs(EquipmentData) do
|
||
|
if not table.find(EXCEPT_KEYS, key) then
|
||
|
ResultData[key] = value
|
||
|
end
|
||
|
end
|
||
|
|
||
|
ResultData.id = UniqueId
|
||
|
ResultData.orgId = EquipmentId
|
||
|
ResultData.wearing = false
|
||
|
|
||
|
ArchiveProxy.pData[Player.UserId][UniqueId] = ResultData
|
||
|
CreateEquipmentInstance(Player, UniqueId, ResultData)
|
||
|
end
|
||
|
|
||
|
|
||
|
function EquipmentProxy:OnPlayerRemoving(Player: Player)
|
||
|
|
||
|
end
|
||
|
|
||
|
|
||
|
ReplicatedStorage.Remotes.PlayerRemoving:Connect(EquipmentProxy.OnPlayerRemoving)
|
||
|
|
||
|
return EquipmentProxy
|