211 lines
7.6 KiB
Plaintext
211 lines
7.6 KiB
Plaintext
--> Services
|
||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||
|
||
--> Dependencies
|
||
local UIWindow = require(ReplicatedStorage.Base.UIWindow)
|
||
local UIEnums = require(ReplicatedStorage.Base.UIEnums)
|
||
local Signal = require(ReplicatedStorage.Tools.Signal)
|
||
|
||
--> Json
|
||
local JsonLevel = require(ReplicatedStorage.Json.Level)
|
||
local JsonForge = require(ReplicatedStorage.Json.Forge)
|
||
|
||
local Utils = require(ReplicatedStorage.Tools.Utils)
|
||
|
||
|
||
--> Events
|
||
local RE_ChallengeBoss = ReplicatedStorage.Events.RE_ChallengeBoss
|
||
local RE_ChallengeLevel = ReplicatedStorage.Events.RE_ChallengeLevel
|
||
|
||
--> Local
|
||
local LocalPlayer = game:GetService("Players").LocalPlayer
|
||
local forgeRedPointSignal = Signal.new(Signal.ENUM.FORGE_RED_POINT)
|
||
local challengeLevelEndSignal = Signal.new(Signal.ENUM.CHALLENGE_LEVEL_END)
|
||
|
||
--------------------------------------------------------------------------------
|
||
|
||
-- 简单的递增消耗计算函数
|
||
local function GetIncrementalCost(currentLevel: number, configData: table, costIndex: number?)
|
||
costIndex = costIndex or 2 -- 默认使用第二个消耗值
|
||
|
||
-- 获取配置表的最大ID
|
||
local maxId = Utils:GetMaxIdFromJson(configData)
|
||
|
||
-- 如果当前等级超过最大ID,使用最大ID的消耗配置
|
||
local effectiveLevel = math.min(currentLevel, maxId)
|
||
|
||
-- 根据有效等级获取对应的消耗配置
|
||
local levelData = Utils:GetIdDataFromJson(configData, effectiveLevel)
|
||
if not levelData or not levelData.cost then
|
||
warn("无法获取等级", effectiveLevel, "的消耗配置")
|
||
return 0
|
||
end
|
||
|
||
-- 确保costIndex在有效范围内
|
||
if costIndex > #levelData.cost then
|
||
warn("消耗索引超出范围:", costIndex, "最大索引:", #levelData.cost)
|
||
return 0
|
||
end
|
||
|
||
return levelData.cost[costIndex]
|
||
end
|
||
|
||
-- 计算可以进行的操作次数(累加消耗)
|
||
local function GetOperationCount(currentValue: number, currentLevel: number, configData: table, costIndex: number?)
|
||
local remainingMoney = currentValue
|
||
local forgeCount = 0
|
||
local currentForgeLevel = currentLevel
|
||
local maxId = Utils:GetMaxIdFromJson(configData)
|
||
|
||
-- 循环计算每次锻造的消耗,直到货币不足
|
||
while remainingMoney > 0 do
|
||
local cost = GetIncrementalCost(currentForgeLevel, configData, costIndex)
|
||
if cost <= 0 then break end -- 无效消耗
|
||
if remainingMoney >= cost then
|
||
remainingMoney = remainingMoney - cost
|
||
forgeCount = forgeCount + 1
|
||
currentForgeLevel = currentForgeLevel + 1
|
||
else
|
||
break -- 货币不足,停止计算
|
||
end
|
||
end
|
||
|
||
return forgeCount
|
||
end
|
||
|
||
--------------------------------------------------------------------------------
|
||
|
||
local MainWindow = {}
|
||
MainWindow.__index = MainWindow
|
||
setmetatable(MainWindow, {__index = UIWindow})
|
||
|
||
function MainWindow:Init(UIManager: table, Data: table?)
|
||
local self = UIWindow:Init(UIManager, Data)
|
||
setmetatable(self, MainWindow)
|
||
self.Variables = {
|
||
["_btnMainCreate"] = 0,
|
||
["_btnMainCha"] = 0,
|
||
["_btnMainAttributeUpgrade"] = 0,
|
||
["_btnStartChallenge"] = 0,
|
||
|
||
-- 锻造条
|
||
["_goForgeBar"] = 0,
|
||
["_goForgeFill"] = 0,
|
||
["_tmpForgeMoney"] = 0,
|
||
["_tmpForgeTime"] = 0,
|
||
|
||
-- 锻造临时红点
|
||
["_tmpRedCreate"] = 0,
|
||
}
|
||
self.UIRootName = "ui_w_main"
|
||
self.UIParentName = UIEnums.UIParent.UIRoot
|
||
|
||
return self
|
||
end
|
||
|
||
function MainWindow:OnClickChallengeButton()
|
||
self.Variables["_btnStartChallenge"].Visible = false
|
||
-- TODO: 之后这里做临时传送特效,可以打断,要不就独立个脚本,用signal触发对应特效
|
||
task.wait(2)
|
||
RE_ChallengeLevel:FireServer(true)
|
||
self.UIManager:OpenWindow("LevelStageWindow")
|
||
end
|
||
|
||
function MainWindow:SetShowForgeBar(nowForgeTime : number, moneyValue: number)
|
||
local maxForgeId = Utils:GetMaxIdFromJson(JsonForge)
|
||
local forgeTime = math.min(nowForgeTime, maxForgeId)
|
||
|
||
-- 获取当前锻造次数对应的消耗(用于UI显示)
|
||
local currentCost = GetIncrementalCost(forgeTime, JsonForge, 2)
|
||
|
||
-- 进度条 - 使用当前锻造次数对应的消耗显示
|
||
self.Variables["_goForgeFill"].Size = UDim2.new(math.min(moneyValue / currentCost, 1), 0, 1, 0)
|
||
self.Variables["_tmpForgeMoney"].Text = string.format("%d/%d", moneyValue, currentCost)
|
||
|
||
-- 右上角红点 - 使用累加消耗计算可以进行的操作次数
|
||
local timeRecorder = GetOperationCount(moneyValue, forgeTime, JsonForge, 2)
|
||
if timeRecorder > 0 then
|
||
self.Variables["_tmpForgeTime"].Visible = true
|
||
self.Variables["_tmpRedCreate"].Visible = true
|
||
self.Variables["_tmpForgeTime"].Text = timeRecorder
|
||
self.Variables["_goForgeFill"].BackgroundColor3 = Color3.fromRGB(255, 255, 0)
|
||
else
|
||
self.Variables["_tmpForgeTime"].Visible = false
|
||
self.Variables["_tmpRedCreate"].Visible = false
|
||
self.Variables["_goForgeFill"].BackgroundColor3 = Color3.fromRGB(12, 227, 209)
|
||
end
|
||
end
|
||
|
||
function MainWindow:OnOpenWindow()
|
||
UIWindow.OnOpenWindow(self)
|
||
|
||
local createCon = self.Variables["_btnMainCreate"].Activated:Connect(function()
|
||
self.UIManager:OpenWindow("CreateWindow")
|
||
end)
|
||
local chaCon = self.Variables["_btnMainCha"].Activated:Connect(function()
|
||
self.UIManager:OpenWindow("ChaWindow")
|
||
end)
|
||
local attributeUpgradeCon = self.Variables["_btnMainAttributeUpgrade"].Activated:Connect(function()
|
||
self.UIManager:OpenWindow("AttributeLvupWindow")
|
||
end)
|
||
local startChallengeCon = self.Variables["_btnStartChallenge"].Activated:Connect(function()
|
||
self:OnClickChallengeButton()
|
||
end)
|
||
|
||
table.insert(self.Connections, createCon)
|
||
table.insert(self.Connections, chaCon)
|
||
table.insert(self.Connections, attributeUpgradeCon)
|
||
table.insert(self.Connections, startChallengeCon)
|
||
|
||
local forgeRedPointCon = forgeRedPointSignal:Connect(function(show: boolean)
|
||
self.Variables["_tmpRedCreate"].Visible = show
|
||
end)
|
||
table.insert(self.Connections, forgeRedPointCon)
|
||
|
||
local challengeLevelEndCon = challengeLevelEndSignal:Connect(function(result: boolean)
|
||
self.Variables["_btnStartChallenge"].Visible = true
|
||
end)
|
||
table.insert(self.Connections, challengeLevelEndCon)
|
||
|
||
|
||
-- 货币进度条显示
|
||
local rePlayerDataFolder = Utils:WaitPlayerDataFolder(LocalPlayer)
|
||
local playerInfoFolder = rePlayerDataFolder:WaitForChild("PlayerInfo")
|
||
local forgeInstance = playerInfoFolder:WaitForChild("Stats"):WaitForChild("forge")
|
||
|
||
local itemFolder = playerInfoFolder:WaitForChild("Items")
|
||
local hasItem = itemFolder:FindFirstChild("2")
|
||
|
||
-- 设置锻造货币变动链接
|
||
local function SetForgeCostChange()
|
||
local costChangeCon = hasItem.Changed:Connect(function(newValue)
|
||
self:SetShowForgeBar(forgeInstance.Value, newValue)
|
||
end)
|
||
table.insert(self.Connections, costChangeCon)
|
||
end
|
||
|
||
if hasItem then
|
||
-- 后续变动设置
|
||
SetForgeCostChange()
|
||
-- 初始化设置
|
||
self:SetShowForgeBar(forgeInstance.Value, hasItem.Value)
|
||
else
|
||
-- 没有货币时监听设置
|
||
local addCon
|
||
addCon = itemFolder.ChildAdded:Connect(function(child)
|
||
if child.Name == "2" then
|
||
hasItem = child
|
||
self:SetShowForgeBar(forgeInstance.Value, hasItem.Value)
|
||
addCon:Disconnect()
|
||
addCon = nil
|
||
-- 设置显示
|
||
SetForgeCostChange()
|
||
end
|
||
end)
|
||
table.insert(self.Connections, addCon)
|
||
-- 初始化设置
|
||
self:SetShowForgeBar(forgeInstance.Value, 0)
|
||
end
|
||
end
|
||
|
||
return MainWindow |