TH1/Unity/Assets/Scripts/TH1_UI/View/Interaction/UIInteractionDiplomacyOfferAllyView.cs
2026-06-20 16:53:20 +08:00

136 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using Logic;
using Logic.Action;
using Logic.Multilingual;
using RuntimeData;
using TH1_Core.Events;
using TH1_Logic.Action;
using TH1_Logic.Core;
using TH1_UI.View.Announce;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace TH1_UI.View.Interaction
{
public class UIInteractionDiplomacyOfferAllyView : Base.View
{
public TextMeshProUGUI Title;
public TextMeshProUGUI Content;
public Image Avatar1;
public TextMeshProUGUI AvatarTextLeft;
public Image Avatar2;
public TextMeshProUGUI AvatarTextRight;
public Button YesButton;
public Button NoButton;
public RectTransform BubbleChat;
public TextMeshProUGUI BubbleChatText;
public ViDelegateAssisstant.Dele OnYesChoiceMade;
public ViDelegateAssisstant.Dele OnNoChoiceMade;
protected override void OnInit()
{
base.OnInit();
YesButton.onClick.RemoveAllListeners();
YesButton.onClick.AddListener(()=> { OnYesChoiceMade?.Invoke(); });
NoButton.onClick.RemoveAllListeners();
NoButton.onClick.AddListener(()=> { OnNoChoiceMade?.Invoke(); });
}
public void SetContent(ShowUIInteractionDiplomacyOfferAlly evt)
{
//Step #1 判断鲁棒性
if (Avatar1 == null || Avatar2 == null || YesButton == null || NoButton == null || Title == null)
{
return;
}
var player1 = Main.MapData.PlayerMap.SelfPlayerData;
if(!Table.Instance.PlayerDataAssets.GetPlayerInfo(player1,out var info1)) return;
//step #2 设置title
if (!Main.MapData.PlayerMap.GetPlayerDataByPlayerID(evt.PlayerId, out var player2)) return;
if(!Table.Instance.PlayerDataAssets.GetPlayerInfo(player2,out var info2)) return;
var forceName = Logic.PlayerLogic.GetDisplayForceName(Main.MapData, player2);
if (evt.PlayerActionType == PlayerActionType.DanegeldDemand)
{
SetDanegeldText(forceName);
SetButtonText(YesButton, PlayerActionType.DanegeldPay);
SetButtonText(NoButton, PlayerActionType.DanegeldReject);
}
else
{
MultilingualManager.Instance.SetUIText(Title,Table.Instance.DiplomacyDataAssets.DiplomacyUIAnnounceOfferAllyTitle);
MultilingualManager.Instance.SetUIText(Content,Table.Instance.DiplomacyDataAssets.DiplomacyUIAnnounceOfferAlly,new List<string>(){forceName});
SetButtonText(YesButton, PlayerActionType.AcceptAlly);
SetButtonText(NoButton, PlayerActionType.RefuseAlly);
}
//Step #3 设置Avatar和bubble chat
Avatar1.sprite = info1.LeaderAvatar;
AvatarTextLeft.text =
Logic.PlayerLogic.GetDisplayForceName(Main.MapData, player1) + " (" + MultilingualManager.Instance.GetMultilingualTextSafe(Table.Instance.TextDataAssets.PresentationUIDiplomacyYouText) +")";
Avatar2.sprite = info2.LeaderAvatar;
AvatarTextRight.text = Logic.PlayerLogic.GetDisplayForceName(Main.MapData, player2);
var chatActionType = evt.PlayerActionType == PlayerActionType.DanegeldDemand
? PlayerActionType.DanegeldDemand
: PlayerActionType.OfferAlly;
var offerChatText = Table.Instance.DiplomacyDataAssets.GetDiplomacyTextByAction((Forces)(player2.PlayerForceId), chatActionType);
if (string.IsNullOrEmpty(offerChatText))
{
BubbleChat.gameObject.SetActive(false);
}
else
{
BubbleChat.gameObject.SetActive(true);
MultilingualManager.Instance.SetUIText(BubbleChatText, offerChatText);
LayoutRebuilder.ForceRebuildLayoutImmediate(BubbleChat);
}
}
private void SetDanegeldText(string forceName)
{
var actionId = new CommonActionId
{
ActionType = CommonActionType.PlayerAction,
PlayerActionType = PlayerActionType.DanegeldDemand
};
if (Table.Instance.ActionDataAssets.GetActionInfo(actionId, out var actionInfo) && actionInfo != null)
{
MultilingualManager.Instance.SetUIText(Title, actionInfo.ActionName);
MultilingualManager.Instance.SetUIText(Content, actionInfo.Desc, new List<string>() { forceName });
return;
}
MultilingualManager.Instance.SetUIText(Title, Table.Instance.TextDataAssets.PresentationUIHakureiDanegeldPaymentTitle);
MultilingualManager.Instance.SetUIText(Content, Table.Instance.TextDataAssets.PresentationUIHakureiDanegeldPaymentContent,
new List<string>() { forceName });
}
private void SetButtonText(Button button, PlayerActionType playerActionType)
{
if (button == null) return;
var label = button.GetComponentInChildren<TextMeshProUGUI>();
if (label == null) return;
var actionId = new CommonActionId
{
ActionType = CommonActionType.PlayerAction,
PlayerActionType = playerActionType
};
if (Table.Instance.ActionDataAssets.GetActionInfo(actionId, out var actionInfo) && actionInfo != null)
MultilingualManager.Instance.SetUIText(label, actionInfo.ActionName);
}
}
}