---@class WB_SelectSoldier_C:UUserWidget ---@field NewButton_Settings UNewButton ---@field ScrollBox_TeamPlayers UScrollBox ---@field ScrollBox_Type UScrollBox ---@field TextBlock_CountDown UTextBlock ---@field TextBlock_Load UTextBlock ---@field TextBlock_Tip UTextBlock ---@field VerticalBox_Diff UVerticalBox --Edit Below-- ---@type WB_SelectSoldier_C local WB_SelectSoldier = { bInitDoOnce = false; }; function WB_SelectSoldier:Construct() print(string.format('[WB_SelectSoldier:Construct] 执行')) UITool.BindButtonClicked(self.NewButton_Settings, self.OpenSettings, self); UGCEventSystem.AddListener(EventTypes.CountDownTimeChange, self.CountDownTimeChange, self); UGCEventSystem.AddListener(EventTypes.UpdatePlayerDatas, self.OnUpdatePlayerDatas, self); UIManager:Register("WB_SelectSoldier", self); self.TextBlock_Load:SetVisibility(ESlateVisibility.Collapsed); -- 设置值 UITool.ForeachAllChildren(self.VerticalBox_Diff, function(index, Widget) Widget:LuaInit(); Widget:Init(index); end); end function WB_SelectSoldier:OnShowPanel(...) self:FillSoldiers(); end function WB_SelectSoldier:FillSoldiers() UGCLogSystem.Log("[WB_SelectSoldier:FillSoldiers] 执行") -- 创建一下这么多个 ObjectPath.AddFunc("WB_SelectSolider_Item", function(TargetClass) local MaxSoldierNum = 0; for i, v in pairs(ESoldierType) do if v > MaxSoldierNum then MaxSoldierNum = v; end end UITool.AdaptChildren(self.ScrollBox_Type, table.getCount(SoldierConfig), ObjectPath.WB_SelectSolider_Item, function(Widget, Index) Widget:SetParent(self, self.OnClickItem); end); -- 在此处尝试拿到玩家选择的兵种次数 local Soldiers = {}; local Times = {}; if LocalPlayerKey then Times = GameState.PlayerDatas.ArchiveData[LocalPlayerKey].Soldiers; Times = Times or {}; UGCLogSystem.LogTree(string.format("[WB_SelectSoldier:FillSoldiers] Times ="), Times) end for i = 1, MaxSoldierNum do local ConfigItem = SoldierConfig[i] if ConfigItem then table.insert(Soldiers, { Type = i, Times = Times[i] or 0, }); end end table.sort(Soldiers, function(a, b) if a.Times == b.Times then return a.Type < b.Type; end return a.Times > b.Times; end); for i = 1, #Soldiers do local Item = self.ScrollBox_Type:GetChildAt(i - 1); Item:SetSoldierType(Soldiers[i].Type); end end) end WB_SelectSoldier.CurrItem = nil; function WB_SelectSoldier:OnClickItem(InItem) UGCLogSystem.Log("[WB_SelectSoldier:OnClickItem] 点击成功") if self.CurrItem == InItem then return ; end if self.CurrItem ~= nil then self.CurrItem:SetSelect(false); end self.CurrItem = InItem; self.CurrItem:SetSelect(true); -- 发送 RPC GameState:SendMiniGameRPC("OnPlayerSelectSoldier", LocalPlayerKey, InItem:GetSoldierType(), true); -- 设置属性的量 self:SetInfoValue(InItem:GetSoldierType()) end function WB_SelectSoldier:OpenSettings() UITool.OpenSettings(); end function WB_SelectSoldier:Tick(MyGeometry, InDeltaTime) end -- function WB_SelectSoldier:Destruct() -- end function WB_SelectSoldier:OnUpdatePlayerDatas(PlayerDatas) -- 更新玩家队伍,判断当前是什么队伍 if LocalTeamId == nil then return ; end local SelfTeamPlayerKeys = {}; for i, v in pairs(PlayerDatas.AccountInfo) do if LocalTeamId == v.TeamID then table.insert(SelfTeamPlayerKeys, i); end end UITool.AdaptChildren(self.ScrollBox_TeamPlayers, table.getCount(SelfTeamPlayerKeys), ObjectPath.WB_ShowPlayerSelectSoldier, function(Widget, index) if SelfTeamPlayerKeys[index] then Widget:SetVisibility(ESlateVisibility.SelfHitTestInvisible); Widget:SetPlayerKey(SelfTeamPlayerKeys[index]); else Widget:SetVisibility(ESlateVisibility.Collapsed); end end) end function WB_SelectSoldier:UpdatePlayerSoldiers(InList) local Table = {}; for PlayerKey, SoldierType in pairs(InList) do local AccountInfo = GameState.PlayerDatas.AccountInfo[PlayerKey]; if AccountInfo.TeamID == LocalTeamId then table.insert(Table, { PlayerKey = PlayerKey, SoldierType = SoldierType, }); end end UITool.ForeachAllChildren(self.ScrollBox_TeamPlayers, function(index, Widget) if Table[index] then if Widget:GetPlayerKey() == Table[index].PlayerKey then Widget:SetSoldierType(Table[index].SoldierType); end end end); local SoldierCounts = {}; for PlayerKey, SoldierType in pairs(InList) do local TeamID = GameState.PlayerDatas.AccountInfo[PlayerKey].TeamID if TeamID == LocalTeamId then SoldierCounts[SoldierType] = (SoldierCounts[SoldierType] or 0) + 1; end end UITool.ForeachAllChildren(self.ScrollBox_Type, function(index, Widget) local TargetType = Widget:GetSoldierType(); if TargetType == nil then return; end local LimitCount = SoldierConfig[TargetType].LimitCount; local TargetCount = SoldierCounts[TargetType]; if TargetCount ~= nil and LimitCount ~= nil and LimitCount > 0 then Widget:SetSelectedCount(TargetCount, LimitCount); else Widget:SetSelectedCount(0); end end); end function WB_SelectSoldier:CountDownTimeChange(NewTime) -- 显示出来 local Time = math.floor(math.clamp(NewTime - 1, 0, NewTime)); UGCLogSystem.Log("[WB_SelectSoldier:CountDownTimeChange] 当前时间:%0.f", Time); self.TextBlock_CountDown:SetText(Time); if Time <= 0 then -- 启动 self.bShowLoading = true; self.TextBlock_Load:SetVisibility(ESlateVisibility.SelfHitTestInvisible); -- 所有子项全部设置为不可点击 UITool.ForeachAllChildren(self.ScrollBox_Type, function(index, Widget) Widget:SetButtonEnable(false); end); -- 发送 RPC 表示当前可以让没选的选了 end end WB_SelectSoldier.bIsOnce = false; WB_SelectSoldier.bShowLoading = false; WB_SelectSoldier.AddPoint = 0.85; WB_SelectSoldier.PointCount = 0.; WB_SelectSoldier.TotalPointCount = 3; WB_SelectSoldier.CurrCount = 0; function WB_SelectSoldier:Loading(InDeltaTime) if self.bIsOnce then return ; end if not self.bShowLoading then return end self.PointCount = self.PointCount + InDeltaTime; if self.PointCount > self.AddPoint then self.PointCount = self.PointCount - self.AddPoint; local s = '加载中' for i = 1, self.CurrCount + 1 do s = s .. '. ' end self.CurrCount = (self.CurrCount + 1) % self.TotalPointCount; self.TextBlock_Load:SetText(s); end end function WB_SelectSoldier:SetInfoValue(SoldierType) local Benefit = SoldierConfig[SoldierType].Benefit if Benefit then for TypeName, Type in pairs(ESoldierAddType) do local Item = self.VerticalBox_Diff:GetChildAt(Type - 1); if Item then Item:SetTarget(Benefit[TypeName]); end end else UITool.ForeachAllChildren(self.VerticalBox_Diff, function(index, Widget) Widget:SetTarget(nil); end); end end return WB_SelectSoldier;