211 lines
7.6 KiB
Plaintext
Raw Normal View History

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-08-05 15:19:19 +08:00
local Signal = require(ReplicatedStorage.Tools.Signal)
2025-07-19 03:03:02 +08:00
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-08-05 15:19:19 +08:00
2025-07-31 19:36:32 +08:00
--> Events
local RE_ChallengeBoss = ReplicatedStorage.Events.RE_ChallengeBoss
2025-08-05 00:55:02 +08:00
local RE_ChallengeLevel = ReplicatedStorage.Events.RE_ChallengeLevel
2025-07-31 19:36:32 +08:00
2025-08-05 15:19:19 +08:00
--> Local
2025-07-30 00:37:09 +08:00
local LocalPlayer = game:GetService("Players").LocalPlayer
2025-08-05 15:19:19 +08:00
local forgeRedPointSignal = Signal.new(Signal.ENUM.FORGE_RED_POINT)
local challengeLevelEndSignal = Signal.new(Signal.ENUM.CHALLENGE_LEVEL_END)
2025-07-30 00:37:09 +08:00
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-08-05 00:55:02 +08:00
["_btnStartChallenge"] = 0,
2025-07-30 00:37:09 +08:00
2025-07-31 19:36:32 +08:00
-- 锻造条
["_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-08-05 15:19:19 +08:00
function MainWindow:OnClickChallengeButton()
self.Variables["_btnStartChallenge"].Visible = false
-- TODO: 之后这里做临时传送特效可以打断要不就独立个脚本用signal触发对应特效
task.wait(2)
RE_ChallengeLevel:FireServer(true)
self.UIManager:OpenWindow("LevelStageWindow")
2025-07-31 19:36:32 +08:00
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-08-05 00:55:02 +08:00
local startChallengeCon = self.Variables["_btnStartChallenge"].Activated:Connect(function()
2025-08-05 15:19:19 +08:00
self:OnClickChallengeButton()
2025-08-05 00:55:02 +08:00
end)
2025-07-31 19:36:32 +08:00
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-08-05 00:55:02 +08:00
table.insert(self.Connections, startChallengeCon)
2025-07-31 19:36:32 +08:00
2025-08-05 15:19:19 +08:00
local forgeRedPointCon = forgeRedPointSignal:Connect(function(show: boolean)
self.Variables["_tmpRedCreate"].Visible = show
end)
table.insert(self.Connections, forgeRedPointCon)
2025-07-31 19:36:32 +08:00
2025-08-05 15:19:19 +08:00
local challengeLevelEndCon = challengeLevelEndSignal:Connect(function(result: boolean)
self.Variables["_btnStartChallenge"].Visible = true
end)
table.insert(self.Connections, challengeLevelEndCon)
2025-07-31 19:36:32 +08:00
-- 货币进度条显示
2025-08-05 15:19:19 +08:00
local rePlayerDataFolder = Utils:WaitPlayerDataFolder(LocalPlayer)
2025-07-31 19:36:32 +08:00
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