UGCProjects/GZJ/Script/UI/ChildWidgets/WBP_WeaponInfo.lua
2025-01-08 22:46:12 +08:00

125 lines
4.8 KiB
Lua

---@class WBP_WeaponInfo_C:UUserWidget
---@field Button_ChooseWeapon_Left UButton
---@field Button_ChooseWeapon_Right UButton
---@field Image_Weapon UImage
---@field Panel_WeaponInfo UCanvasPanel
---@field TextBlock_WeaponName UTextBlock
--Edit Below--
local WBP_WeaponInfo = {
bInitDoOnce = false;
};
function WBP_WeaponInfo:Construct()
self.SuperClass:Construct()
self:SetWeaponComponent()
if self.WeaponComponent then
self:OnUpdateWeapon(self.WeaponComponent.CurrentWeaponId, GameDataManager.GetLocalPlayerController().PlayerKey)
end
EventSystem:AddListener(EventType.ClientUpdateWeapon, WBP_WeaponInfo.OnUpdateWeapon, self)
self.Button_ChooseWeapon_Left.OnClicked:Add(WBP_WeaponInfo.OnMinusWeapon, self)
self.Button_ChooseWeapon_Right.OnClicked:Add(WBP_WeaponInfo.OnAddWeapon, self)
end
-- function WBP_WeaponInfo:Tick(MyGeometry, InDeltaTime)
-- end
function WBP_WeaponInfo:Destruct()
EventSystem:RemoveListener(EventType.ClientUpdateWeapon, WBP_WeaponInfo.OnUpdateWeapon, self)
self.Button_ChooseWeapon_Left.OnClicked:Add(WBP_WeaponInfo.OnMinusWeapon, self)
self.Button_ChooseWeapon_Right.OnClicked:Add(WBP_WeaponInfo.OnAddWeapon, self)
self.SuperClass:Destruct()
end
function WBP_WeaponInfo:OnUpdateWeapon(InNewId, InPlayerKey)
if InPlayerKey ~= GameDataManager.GetLocalPlayerController().PlayerKey then
return
end
-- 找到对应 图标
local WeaponType = GameDataManager.GetWeaponType(InNewId)
if WeaponType == nil then
UE.Log("[WBP_WeaponInfo:OnUpdateWeapon] Data is nil")
return
end
local Val = DropItemMap.WeaponTable[WeaponType]
if Val ~= nil then
if Val.Icon == nil then
self.Image_Weapon:SetVisibility(ESlateVisibility.Collapsed)
UE.Log("[WBP_WeaponInfo:OnUpdateWeapon] ID[%d] can't find Icon", InNewId)
else
local Icon = UE.LoadObject(Val.Icon)
if Icon == nil then
self.Image_Weapon:SetVisibility(ESlateVisibility.Collapsed)
else
self:SetWeaponIcon(Icon)
self.TextBlock_WeaponName:SetText(Val.Text)
self.TextBlock_WeaponName:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
end
end
end
end
function WBP_WeaponInfo:SetWeaponIcon(InIcon)
self.Image_Weapon:SetBrushFromTexture(InIcon)
self.Image_Weapon:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
end
function WBP_WeaponInfo:SetWeaponComponent()
local PlayerController = GameDataManager.GetLocalPlayerController()
if PlayerController then
local OwningPawn = PlayerController:GetPlayerCharacterSafety()
if OwningPawn then
local WC = OwningPawn:GetWeaponComponent()
if WC ~= nil and UE.IsValid(WC) then
self.WeaponComponent = WC
return
end
end
end
self.WeaponComponent = nil
end
function WBP_WeaponInfo:OnMinusWeapon()
-- 找到当前的 Id
if self.WeaponComponent == nil then
self:SetWeaponComponent()
end
local CurrentIndex = self.WeaponComponent:GetCurrentWeaponIndex()
local TotalWeaponCount = self.WeaponComponent:GetWeaponCount()
print(string.format("[WBP_InteractPanel:OnMinusWeapon] Total Weapon Count = %d", TotalWeaponCount))
local NextIndex = (CurrentIndex + TotalWeaponCount - 1) % TotalWeaponCount
if NextIndex == 0 then
NextIndex = TotalWeaponCount
end
print(string.format("[WBP_InteractPanel:OnMinusWeapon] Next Index = %d", NextIndex))
local NextWeaponId = self.WeaponComponent:FindWeaponIdByIndex(NextIndex)
local PlayerController = GameDataManager.GetLocalPlayerController()
print(string.format("[WBP_InteractPanel:OnMinusWeapon] NextWeapon Id = %d" , NextWeaponId))
UnrealNetwork.CallUnrealRPC(PlayerController, PlayerController, "ServerRPC_ChangeCurrentWeaponId", NextWeaponId)
end
function WBP_WeaponInfo:OnAddWeapon()
if self.WeaponComponent == nil then
self:SetWeaponComponent()
end
local CurrentIndex = self.WeaponComponent:GetCurrentWeaponIndex()
local TotalWeaponCount = self.WeaponComponent:GetWeaponCount()
print(string.format("[WBP_InteractPanel:OnAddWeapon] Total Weapon Count = %d", TotalWeaponCount))
local NextIndex = (CurrentIndex + TotalWeaponCount + 1) % TotalWeaponCount
if NextIndex == 0 then
NextIndex = TotalWeaponCount
end
print(string.format("[WBP_InteractPanel:OnAddWeapon] Next Index = %d", NextIndex))
local NextWeaponId = self.WeaponComponent:FindWeaponIdByIndex(NextIndex)
local PlayerController = GameDataManager.GetLocalPlayerController()
print(string.format("[WBP_InteractPanel:OnAddWeapon] NextWeapon Id = %d" , NextWeaponId))
UnrealNetwork.CallUnrealRPC(PlayerController, PlayerController, "ServerRPC_ChangeCurrentWeaponId", NextWeaponId)
end
return WBP_WeaponInfo;