131 lines
4.1 KiB
Plaintext
131 lines
4.1 KiB
Plaintext
-- 装备代理
|
|
local EquipmentProxy = {}
|
|
|
|
--> Services
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
local ServerStorage = game:GetService("ServerStorage")
|
|
|
|
--> Variables
|
|
local Utils = require(ReplicatedStorage.Tools.Utils)
|
|
local ArchiveProxy = require(ServerStorage.Proxy.ArchiveProxy)
|
|
local PlayerInfoProxy = require(ServerStorage.Proxy.PlayerInfoProxy)
|
|
|
|
--> Json
|
|
local JsonEquipment = require(ReplicatedStorage.Json.Equipment)
|
|
|
|
--> 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
|
|
Utils:CreateFolder("Equipment", pData)
|
|
|
|
-- 新玩家数据初始化
|
|
if not ArchiveProxy.pData[Player.UserId][STORE_NAME] then
|
|
ArchiveProxy.pData[Player.UserId][STORE_NAME] = {}
|
|
end
|
|
|
|
-- 初始化装备
|
|
for uniqueId, EquipmentData in ArchiveProxy.pData[Player.UserId][STORE_NAME] do
|
|
CreateEquipmentInstance(Player, uniqueId, EquipmentData)
|
|
end
|
|
end
|
|
|
|
local EXCEPT_KEYS = { "id", "orgId", "name", "attributes"}
|
|
-- 添加装备到背包
|
|
function EquipmentProxy:AddEquipment(Player: Player, EquipmentId: number)
|
|
local pData = Utils:GetPlayerDataFolder(Player)
|
|
if not pData then return end
|
|
|
|
local EquipmentData = Utils:GetIdDataFromJson(JsonEquipment, 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:RecycleEquipment(Player: Player, EquipmentId: number)
|
|
local pData = Utils:GetPlayerDataFolder(Player)
|
|
if not pData then return end
|
|
|
|
local EquipmentData = ArchiveProxy.pData[Player.UserId][STORE_NAME][EquipmentId]
|
|
if not EquipmentData then return end
|
|
|
|
-- 回收装备返回金币
|
|
PlayerInfoProxy:ChangeItem(Player, 1, EquipmentData.recycle)
|
|
|
|
ArchiveProxy.pData[Player.UserId][EquipmentId] = nil
|
|
local EquipmentInstance = GetPlayerEquipmentFolder(Player):FindFirstChild(EquipmentId)
|
|
if EquipmentInstance then
|
|
EquipmentInstance:Destroy()
|
|
end
|
|
end
|
|
|
|
-- 穿戴装备
|
|
function EquipmentProxy:WearEquipment(Player: Player, EquipmentId: number)
|
|
local pData = Utils:GetPlayerDataFolder(Player)
|
|
if not pData then return end
|
|
|
|
-- 穿戴装备时再生成模型
|
|
|
|
end
|
|
|
|
function EquipmentProxy:OnPlayerRemoving(Player: Player)
|
|
|
|
end
|
|
|
|
|
|
ReplicatedStorage.Remotes.PlayerRemoving.Event:Connect(function(PlayerUserId: string)
|
|
EquipmentProxy:OnPlayerRemoving(PlayerUserId)
|
|
end)
|
|
|
|
return EquipmentProxy |