2025-07-19 03:03:02 +08:00
|
|
|
|
--> Services
|
|
|
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
|
|
|
|
|
|
|
|
--> Dependencies
|
|
|
|
|
local UIWindow = require(ReplicatedStorage.Base.UIWindow)
|
|
|
|
|
local UIEnums = require(ReplicatedStorage.Base.UIEnums)
|
|
|
|
|
|
2025-07-30 00:37:09 +08:00
|
|
|
|
--> Json
|
|
|
|
|
local JsonLevel = require(ReplicatedStorage.Json.Level)
|
2025-07-31 19:36:32 +08:00
|
|
|
|
local JsonForge = require(ReplicatedStorage.Json.Forge)
|
2025-07-30 00:37:09 +08:00
|
|
|
|
|
|
|
|
|
local Utils = require(ReplicatedStorage.Tools.Utils)
|
|
|
|
|
|
2025-07-31 19:36:32 +08:00
|
|
|
|
--> Events
|
|
|
|
|
local RE_ChallengeBoss = ReplicatedStorage.Events.RE_ChallengeBoss
|
|
|
|
|
|
2025-07-30 00:37:09 +08:00
|
|
|
|
local LocalPlayer = game:GetService("Players").LocalPlayer
|
|
|
|
|
|
2025-07-19 03:03:02 +08:00
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
2025-08-01 19:23:12 +08:00
|
|
|
|
-- 简单的递增消耗计算函数
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
2025-07-19 03:03:02 +08:00
|
|
|
|
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,
|
2025-07-22 01:56:56 +08:00
|
|
|
|
["_btnMainCha"] = 0,
|
2025-07-23 01:04:27 +08:00
|
|
|
|
["_btnMainAttributeUpgrade"] = 0,
|
2025-07-30 00:37:09 +08:00
|
|
|
|
|
2025-07-31 19:36:32 +08:00
|
|
|
|
-- 关卡
|
2025-07-30 00:37:09 +08:00
|
|
|
|
["_tmpNowLevel"] = 0,
|
|
|
|
|
["_imgBoss"] = 0,
|
2025-07-31 19:36:32 +08:00
|
|
|
|
["_btnChallengeBoss"] = 0,
|
|
|
|
|
|
|
|
|
|
-- 锻造条
|
|
|
|
|
["_goForgeBar"] = 0,
|
|
|
|
|
["_goForgeFill"] = 0,
|
|
|
|
|
["_tmpForgeMoney"] = 0,
|
|
|
|
|
["_tmpForgeTime"] = 0,
|
2025-08-01 19:23:12 +08:00
|
|
|
|
|
|
|
|
|
-- 锻造临时红点
|
|
|
|
|
["_tmpRedCreate"] = 0,
|
2025-07-19 03:03:02 +08:00
|
|
|
|
}
|
|
|
|
|
self.UIRootName = "ui_w_main"
|
|
|
|
|
self.UIParentName = UIEnums.UIParent.UIRoot
|
|
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
end
|
|
|
|
|
|
2025-07-31 19:36:32 +08:00
|
|
|
|
-- 设置关卡显示
|
2025-07-30 00:37:09 +08:00
|
|
|
|
function MainWindow:SetShowLevel(level: number)
|
|
|
|
|
self.Variables["_tmpNowLevel"].Text = string.format("第%d关", level)
|
|
|
|
|
local levelData = Utils:GetIdDataFromJson(JsonLevel, level)
|
|
|
|
|
if levelData.type == 2 then
|
|
|
|
|
self.Variables["_imgBoss"].Visible = true
|
|
|
|
|
else
|
|
|
|
|
self.Variables["_imgBoss"].Visible = false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2025-07-31 19:36:32 +08:00
|
|
|
|
-- 设置boss挑战按钮显示
|
|
|
|
|
function MainWindow:SetShowBossChallenge(newValue: number)
|
|
|
|
|
local playerDataFolder = game.Workspace:WaitForChild("Level"):WaitForChild(LocalPlayer.UserId)
|
|
|
|
|
local ProgressFolder = playerDataFolder:WaitForChild("Progress")
|
|
|
|
|
local mainValue = ProgressFolder:FindFirstChild("Main").Value
|
|
|
|
|
local levelIdValue = playerDataFolder:WaitForChild("Challenge"):WaitForChild("LevelId").Value
|
|
|
|
|
|
|
|
|
|
if levelIdValue == mainValue then
|
|
|
|
|
self.Variables["_btnChallengeBoss"].Visible = false
|
|
|
|
|
else
|
|
|
|
|
self.Variables["_btnChallengeBoss"].Visible = (newValue == mainValue)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- 手动点击按钮,挑战boss
|
|
|
|
|
function MainWindow:OnClickChallengeBoss()
|
|
|
|
|
RE_ChallengeBoss:FireServer()
|
|
|
|
|
self.Variables["_btnChallengeBoss"].Visible = false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function MainWindow:SetShowForgeBar(nowForgeTime : number, moneyValue: number)
|
|
|
|
|
local maxForgeId = Utils:GetMaxIdFromJson(JsonForge)
|
2025-08-01 19:23:12 +08:00
|
|
|
|
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)
|
2025-07-31 19:36:32 +08:00
|
|
|
|
|
2025-08-01 19:23:12 +08:00
|
|
|
|
-- 右上角红点 - 使用累加消耗计算可以进行的操作次数
|
|
|
|
|
local timeRecorder = GetOperationCount(moneyValue, forgeTime, JsonForge, 2)
|
2025-07-31 19:36:32 +08:00
|
|
|
|
if timeRecorder > 0 then
|
|
|
|
|
self.Variables["_tmpForgeTime"].Visible = true
|
2025-08-01 19:23:12 +08:00
|
|
|
|
self.Variables["_tmpRedCreate"].Visible = true
|
2025-07-31 19:36:32 +08:00
|
|
|
|
self.Variables["_tmpForgeTime"].Text = timeRecorder
|
2025-08-01 19:23:12 +08:00
|
|
|
|
self.Variables["_goForgeFill"].BackgroundColor3 = Color3.fromRGB(255, 255, 0)
|
2025-07-31 19:36:32 +08:00
|
|
|
|
else
|
|
|
|
|
self.Variables["_tmpForgeTime"].Visible = false
|
2025-08-01 19:23:12 +08:00
|
|
|
|
self.Variables["_tmpRedCreate"].Visible = false
|
|
|
|
|
self.Variables["_goForgeFill"].BackgroundColor3 = Color3.fromRGB(12, 227, 209)
|
2025-07-31 19:36:32 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2025-07-19 03:03:02 +08:00
|
|
|
|
function MainWindow:OnOpenWindow()
|
|
|
|
|
UIWindow.OnOpenWindow(self)
|
|
|
|
|
|
|
|
|
|
local createCon = self.Variables["_btnMainCreate"].Activated:Connect(function()
|
|
|
|
|
self.UIManager:OpenWindow("CreateWindow")
|
|
|
|
|
end)
|
2025-07-22 01:56:56 +08:00
|
|
|
|
local chaCon = self.Variables["_btnMainCha"].Activated:Connect(function()
|
|
|
|
|
self.UIManager:OpenWindow("ChaWindow")
|
|
|
|
|
end)
|
2025-07-23 01:04:27 +08:00
|
|
|
|
local attributeUpgradeCon = self.Variables["_btnMainAttributeUpgrade"].Activated:Connect(function()
|
|
|
|
|
self.UIManager:OpenWindow("AttributeLvupWindow")
|
|
|
|
|
end)
|
2025-07-31 19:36:32 +08:00
|
|
|
|
local challengeBossCon = self.Variables["_btnChallengeBoss"].Activated:Connect(function()
|
|
|
|
|
self:OnClickChallengeBoss()
|
|
|
|
|
end)
|
|
|
|
|
|
2025-07-19 03:03:02 +08:00
|
|
|
|
table.insert(self.Connections, createCon)
|
2025-07-22 01:56:56 +08:00
|
|
|
|
table.insert(self.Connections, chaCon)
|
2025-07-23 01:04:27 +08:00
|
|
|
|
table.insert(self.Connections, attributeUpgradeCon)
|
2025-07-31 19:36:32 +08:00
|
|
|
|
table.insert(self.Connections, challengeBossCon)
|
2025-07-30 00:37:09 +08:00
|
|
|
|
|
2025-07-30 19:46:01 +08:00
|
|
|
|
-- TODO: 暂时用主关卡数显示,我记得之前这里主要是记录的,Challenge中才是正在挑战的内容
|
|
|
|
|
-- TODO: 之后LevelProxy也应该挪到ReplicatedStorage下,之前可能是因为觉得Challenge是临时的内容所以放在workspace下,但是逻辑做的不统一
|
|
|
|
|
local playerDataFolder = game.Workspace:WaitForChild("Level"):WaitForChild(LocalPlayer.UserId)
|
2025-07-31 19:36:32 +08:00
|
|
|
|
local StatsFolder = playerDataFolder:WaitForChild("Challenge")
|
|
|
|
|
local ProgressFolder = playerDataFolder:WaitForChild("Progress")
|
|
|
|
|
|
|
|
|
|
local levelCon = StatsFolder.LevelId.Changed:Connect(function(newValue)
|
2025-07-30 00:37:09 +08:00
|
|
|
|
self:SetShowLevel(newValue)
|
2025-07-31 19:36:32 +08:00
|
|
|
|
self:SetShowBossChallenge(ProgressFolder.BossFail.Value)
|
|
|
|
|
end)
|
|
|
|
|
local bossChallengeCon = ProgressFolder.BossFail.Changed:Connect(function(newValue)
|
|
|
|
|
self:SetShowBossChallenge(newValue)
|
2025-07-30 00:37:09 +08:00
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
-- 初始值设置
|
2025-07-31 19:36:32 +08:00
|
|
|
|
self:SetShowLevel(StatsFolder.LevelId.Value)
|
|
|
|
|
self:SetShowBossChallenge(ProgressFolder.BossFail.Value)
|
|
|
|
|
|
2025-07-30 00:37:09 +08:00
|
|
|
|
table.insert(self.Connections, levelCon)
|
2025-07-31 19:36:32 +08:00
|
|
|
|
table.insert(self.Connections, bossChallengeCon)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- 货币进度条显示
|
|
|
|
|
local rePlayerDataFolder = Utils:GetPlayerDataFolder(LocalPlayer)
|
|
|
|
|
local playerInfoFolder = rePlayerDataFolder:WaitForChild("PlayerInfo")
|
|
|
|
|
local forgeInstance = playerInfoFolder:WaitForChild("Stats"):WaitForChild("forge")
|
|
|
|
|
|
|
|
|
|
local itemFolder = playerInfoFolder:WaitForChild("Items")
|
2025-08-01 19:23:12 +08:00
|
|
|
|
local hasItem = itemFolder:FindFirstChild("2")
|
2025-07-31 19:36:32 +08:00
|
|
|
|
|
|
|
|
|
-- 设置锻造货币变动链接
|
|
|
|
|
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
|
2025-08-01 19:23:12 +08:00
|
|
|
|
hasItem = child
|
|
|
|
|
self:SetShowForgeBar(forgeInstance.Value, hasItem.Value)
|
2025-07-31 19:36:32 +08:00
|
|
|
|
addCon:Disconnect()
|
|
|
|
|
addCon = nil
|
2025-08-01 19:23:12 +08:00
|
|
|
|
-- 设置显示
|
2025-07-31 19:36:32 +08:00
|
|
|
|
SetForgeCostChange()
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
table.insert(self.Connections, addCon)
|
2025-08-01 19:23:12 +08:00
|
|
|
|
-- 初始化设置
|
|
|
|
|
self:SetShowForgeBar(forgeInstance.Value, 0)
|
2025-07-31 19:36:32 +08:00
|
|
|
|
end
|
2025-07-19 03:03:02 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return MainWindow
|