2025-08-15 00:45:18 +08:00

248 lines
8.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

--> Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--> Dependencies
local UIWindow = require(ReplicatedStorage.Base.UIWindow)
local UIEnums = require(ReplicatedStorage.Base.UIEnums)
--> Components
local WearingShow = require(script.WearingShow)
local PackageShow = require(script.PackageShow)
--> Dependencies
local Utils = require(ReplicatedStorage.Tools.Utils)
local Localization = require(ReplicatedStorage.Tools.Localization)
--> Json
local JsonAttributesUpgrade = require(ReplicatedStorage.Json.AttributesUpgrade)
local JsonRune = require(ReplicatedStorage.Json.Rune)
--> Events
local RE_RuneInlay = ReplicatedStorage.Events.RE_RuneInlay
--> Variables
local LocalPlayer = game.Players.LocalPlayer
--------------------------------------------------------------------------------
local RuneWindow = {}
RuneWindow.__index = RuneWindow
setmetatable(RuneWindow, {__index = UIWindow})
function RuneWindow:Init(UIManager: table, Data: table?)
local self = UIWindow:Init(UIManager, Data)
setmetatable(self, RuneWindow)
self.Variables = {
["_goRunePanel"] = 0,
["__listRunePackage"] = 0,
["__listRuneWearing"] = 0,
["_tmpCombatValue"] = 0,
["_imgIcon"] = 0,
["_bgNowSelectFrame"] = 0,
["_imgNowRune"] = 0,
["_tmpNowRuneName"] = 0,
["_btnClose"] = 0,
["_btnBgClose"] = 0,
["_btnInlay"] = 0,
}
self.UIRootName = "ui_w_rune"
self.UIParentName = UIEnums.UIParent.UIRoot
self.ShowEquipmentId = nil
self.NowSelectRuneId = nil
return self
end
function RuneWindow:ShowDetailData(uniqueId: number)
if uniqueId then
self.NowSelectRuneId = uniqueId
local runeData = Utils:GetIdDataFromJson(JsonRune, uniqueId)
self.Variables["_tmpNowRuneName"].Text = Localization:GetLanguageData(runeData.nameId)
self.Variables["_imgNowRune"].Image = Localization:GetImageData(runeData.icon)
self.Variables["_imgNowRune"].BackgroundColor3 = Localization:GetRuneQualityBgColor(runeData.quality)
self.Variables["_imgNowRune"].Transparency = 0
else
self.NowSelectRuneId = nil
self.Variables["_tmpNowRuneName"].Text = ""
self.Variables["_imgNowRune"].Image = ""
self.Variables["_imgNowRune"].BackgroundColor3 = Color3.fromRGB(0, 0, 0)
self.Variables["_imgNowRune"].Transparency = 1
end
end
function RuneWindow:WearRefresh(data: table)
local newSlot = data.instance:GetAttribute("wearing")
self.Variables["__listRuneWearing"]:RemoveData("slot"..newSlot)
self.Variables["__listRunePackage"]:RemoveData(tostring(data.id))
-- 更新穿戴数据,要不再点击前端就不好使了
data.wearing = newSlot
self.Variables["__listRuneWearing"]:AddData("slot"..newSlot, data)
-- 穿戴后,如果当前穿戴的符文是当前选中的符文,则刷新当前选中的符文为空
if data.id == self.NowSelectRuneId then self:ShowDetailData() end
end
function RuneWindow:UnwearRefresh(data: table)
local oldSlot = data.wearing
self.Variables["__listRuneWearing"]:RemoveData("slot"..oldSlot)
data.wearing = 0
self.Variables["__listRunePackage"]:AddData(tostring(data.id), data)
self.Data.Wearing["slot"..oldSlot] = {
wearing = oldSlot,
}
self.Variables["__listRuneWearing"]:AddData("slot"..oldSlot, self.Data.Wearing["slot"..oldSlot])
end
function RuneWindow:InlayRune()
if self.NowSelectRuneId and self.ShowEquipmentId then
RE_RuneInlay:FireClient(self.NowSelectRuneId, self.ShowEquipmentId)
end
end
function RuneWindow:AddInstanceData(configInstance: Instance, Data: table?)
local data = self.Data
if Data then data = Data end
local attributes = configInstance:GetAttributes()
-- 归类是否是穿戴的装备
local parentName, secondName = "PackageRune", configInstance.Name
if attributes.wearing == self.ShowEquipmentId then
parentName = "WearingRune"
secondName = "slot"..attributes.wearingSlot
end
data[parentName][secondName] = {}
for attributeKey, attributeValue in attributes do
data[parentName][secondName][attributeKey] = attributeValue
end
data[parentName][secondName].instance = configInstance
return data[parentName][secondName], parentName
end
function RuneWindow:RemoveInstanceData(configInstance: Instance, Data: table?)
for key, data in pairs(self.Data.WearingRune) do
if data.instance == configInstance then
return tostring(key), "WearingRune"
end
end
for key, data in pairs(self.Data.PackageRune) do
if data.instance == configInstance then
return tostring(key), "PackageRune"
end
end
return nil
end
function RuneWindow:OnOpenWindow()
UIWindow.OnOpenWindow(self)
self.Variables["__listRunePackage"]:AddComponent(PackageShow)
self.Variables["__listRuneWearing"]:AddComponent(WearingShow)
local bgCloseCon = self.Variables["_btnBgClose"].Activated:Connect(function()
self.UIManager:CloseWindow(script.Name)
end)
local closeCon = self.Variables["_btnClose"].Activated:Connect(function()
self.UIManager:CloseWindow(script.Name)
end)
table.insert(self.Connections, bgCloseCon)
table.insert(self.Connections, closeCon)
-- 自己进行数据处理
local DataFolder = Utils:WaitPlayerDataFolder(LocalPlayer):FindFirstChild("Rune")
local data = {
WearingRune = {},
PackageRune = {},
}
-- 找到当前穿戴的装备
local EquipmentFolder = Utils:WaitPlayerDataFolder(LocalPlayer):FindFirstChild("Equipment")
for _, child in EquipmentFolder:GetChildren() do
if child:GetAttribute("wearing") ~= 0 then
self.ShowEquipmentId = child.Name
break
end
end
-- 如果当前没有穿戴的装备,并且没有装备,则报错提示, 并返回内容
if not self.ShowEquipmentId and #EquipmentFolder:GetChildren() == 0 then
warn("没有找到当前穿戴的装备")
return
end
-- 如果当前没有穿戴的装备,则默认使用第一个装备
if not self.ShowEquipmentId then
self.ShowEquipmentId = EquipmentFolder:GetChildren()[1].Name
end
local equipmentInstance = EquipmentFolder:FindFirstChild(self.ShowEquipmentId)
local maxRuneNumber = equipmentInstance:GetAttribute("maxRuneNumber")
-- 如果当前最大槽位数为0刷新至空显示
if maxRuneNumber <= 0 then
self.Variables["__listRuneWearing"]:SetData({})
self:ShowDetailData()
return
end
-- 添加符文数据
for _, child in DataFolder:GetChildren() do
self:AddInstanceData(child, data)
end
self:SetData(data)
-- 补充空槽位
for i = 1, maxRuneNumber do
local isExist = false
for k, data in pairs(self.Data.WearingRune) do
if data.wearingSlot == i then
isExist = true
break
end
end
if not isExist then
self.Data.WearingRune["slot" .. i] = {
wearingSlot = i,
}
end
end
local addCon = DataFolder.ChildAdded:Connect(function(child)
local data, parentName = self:AddInstanceData(child, data)
if parentName == "WearingRune" then
self.Variables["__listRuneWearing"]:AddData("slot"..data.wearingSlot, data)
else
self.Variables["__listRunePackage"]:AddData(child.Name, data)
end
end)
local removeCon = DataFolder.ChildRemoved:Connect(function(child)
-- TODO: 这里清除逻辑不清晰,之后优化
local key, parentName = self:RemoveInstanceData(child, self.Data)
if parentName == "WearingRune" then
local removeIndex = self.Variables["__listRuneWearing"]:RemoveData(key)
else
local removeIndex = self.Variables["__listRunePackage"]:RemoveData(key)
end
end)
table.insert(self.Connections, addCon)
table.insert(self.Connections, removeCon)
print(self.Variables["__listRunePackage"], self.Variables["__listRuneWearing"])
self.Variables["__listRunePackage"]:SetData(self.Data.PackageRune)
self.Variables["__listRuneWearing"]:SetData(self.Data.WearingRune)
self.Variables["__listRuneWearing"]:SetLayoutOrder("wearingSlot")
end
function RuneWindow:OnCloseWindow()
UIWindow.OnCloseWindow(self)
end
return RuneWindow