2025-08-06 00:39:20 +08:00

127 lines
4.3 KiB
Plaintext

local AttributeLvupShow = {}
AttributeLvupShow.__index = AttributeLvupShow
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Utils = require(ReplicatedStorage.Tools.Utils)
local Localization = require(ReplicatedStorage.Tools.Localization)
local JsonAttributesUpgrade = require(ReplicatedStorage.Json.AttributesUpgrade)
local JsonAttributes = require(ReplicatedStorage.Json.Attributes)
local RE_UpgradeAttributes = ReplicatedStorage.Events.RE_UpgradeAttributes
local LocalPlayer = game:GetService("Players").LocalPlayer
function AttributeLvupShow:Init(data: table)
local self = {}
self.Data = data
self.Variables = {
["_imgIcon"] = 0,
["_tmpAttributeName"] = 0,
["_tmpValue"] = 0,
["_tmpLv"] = 0,
["_btnUpgrade"] = 0,
}
self.Connections = {}
setmetatable(self, AttributeLvupShow)
return self
end
function AttributeLvupShow:GetNowLv()
local attributeFolder = Utils:GetPlayerDataFolder(LocalPlayer):FindFirstChild("PlayerInfo"):FindFirstChild("AttributesUpgrade")
local instance = attributeFolder:FindFirstChild(tostring(self.Data.id))
local nowLv = 0
if instance then nowLv = instance.Value end
return nowLv
end
function AttributeLvupShow:Refresh()
-- 如果有实例,,并且之前有监听,则断开添加监听
if self.instance then
if self.folderCon then
self.folderCon:Disconnect()
self.folderCon = nil
end
end
if self.instance and not self.instanceCon then
local instanceCon = self.instance.Changed:Connect(function()
self:Refresh()
end)
self.instanceCon = instanceCon
end
local attributeData = Utils:GetIdDataFromJson(JsonAttributes, self.Data.id)
self.Variables._imgIcon.Image = Localization:GetImageData(attributeData.iconId)
self.Variables._tmpAttributeName.Text = self.Data.id
local nowLv = self:GetNowLv()
self.Variables._tmpLv.Text = "Lv." .. nowLv
-- 花费按钮显示
if self.Data.maxLv then
self.Variables._btnUpgrade.Text = nowLv >= self.Data.maxLv and "满级" or self.Data.cost[2] + self.Data.cost[3] * (self.Data.maxLv - 1)
-- 属性值显示
local caculateValue = self.Data.lvAdd[1] + self.Data.lvAdd[2] * (self.Data.maxLv - 1)
self.Variables._tmpValue.Text = string.format("%.2f%%", caculateValue / 100)
else
self.Variables._btnUpgrade.Text = self.Data.cost[2] + self.Data.cost[3] * nowLv
-- 属性值显示(下一级)
if self.Data.id == 1 or self.Data.id == 2 then
self.Variables._tmpValue.Text = math.floor(self.Data.lvAdd[1] * ((self.Data.lvAdd[2] / 10000) ^ nowLv))
else
self.Variables._tmpValue.Text = self.Data.lvAdd[1] + self.Data.lvAdd[2] * nowLv
end
end
end
function AttributeLvupShow:OnInitFinish()
local con = self.Variables._btnUpgrade.MouseButton1Click:Connect(function()
if self.Data.maxLv then
if self:GetNowLv() >= self.Data.maxLv then
-- TODO: 之后做提示弹窗
else
RE_UpgradeAttributes:FireServer(self.Data.id)
end
else
-- TODO: 检查货币是否充足
RE_UpgradeAttributes:FireServer(self.Data.id)
end
end)
table.insert(self.Connections, con)
-- 没有实例时,监听文件夹,有实例时,监听实例
if not self.instance then
local attributeFolder = Utils:GetPlayerDataFolder(LocalPlayer):FindFirstChild("PlayerInfo"):FindFirstChild("AttributesUpgrade")
local attributeInst = attributeFolder:FindFirstChild(tostring(self.Data.id))
if attributeInst then
self.instance = attributeInst
else
local folderCon = attributeFolder.ChildAdded:Connect(function(child)
if child.Name == tostring(self.Data.id) then
self.instance = child
self:Refresh()
end
end)
self.folderCon = folderCon
end
end
end
function AttributeLvupShow:Destroy()
if self.instanceCon then
self.instanceCon:Disconnect()
self.instanceCon = nil
end
if self.folderCon then
self.folderCon:Disconnect()
self.folderCon = nil
end
for k, v in pairs(self) do
self[k] = nil
end
self = nil
end
return AttributeLvupShow