TH1/Unity/Assets/Scripts/TH1_UI/View/Info/UIInfoDiplomacyView.cs
2026-02-14 22:57:12 +08:00

465 lines
15 KiB
C#

using System.Collections.Generic;
using Logic;
using Logic.Action;
using Logic.Audio;
using Logic.CrashSight;
using Logic.Multilingual;
using RuntimeData;
using TH1_Core.Events;
using TH1_Core.Managers;
using TH1_Logic.Action;
using TH1_Logic.Core;
using TH1_UI.Components;
using TH1_UI.HintUI;
using TH1Resource;
using TMPro;
using UI.HintUI;
using UnityEngine;
using UnityEngine.UI;
namespace TH1_UI.View.Info
{
public class UIInfoDiplomacyView : Base.View
{
public Button closeButton;
public Button BlockButton;
public RectTransform VerticalGroup;
// Avatar部分
public TextMeshProUGUI ForceTitle;
public RectTransform ForceTitleRect;
public Image LeaderAvatar;
public RectTransform BubbleChat;
public TextMeshProUGUI BubbleChatText;
public Image TopStripeBG2;
public Image TopStripeBG;
public Image ForceTitleBG;
// Status部分
public Transform StatusArea;
public GameObject Status;
public Image StateIcon;
public TextMeshProUGUI StateText;
//态度条
public GameObject AttitudeArea;
public TextMeshProUGUI AttitudeNoMeetText;
public RectTransform RelationHandle;
public GameObject AttitudeBar;
public HintTrigger AttitudeBarHint;
//ActionArea
public Transform ActionArea;
public Image AllyActionCircleBGOutside;
public Image AllyActionCircleBG;
public GameObject AllyActionSend;
public Button AllyActionButton;
public Image EmbassyActionCircleBGOutside;
public Image EmbassyActionCircleBG;
public Image EmbassyActionCost;
public TextMeshProUGUI EmbassyActionCostText;
public Button EmbassyActionButton;
public GameObject BreakAllyAction;
public Button BreakAllyActionButton;
//RelationArea
public List<UIInfoDiplomacyAvatarMono> OtherRelationAvatarList;
public ViDelegateAssisstant.Dele OnBtnCloseClick;
public GameObject RelationNoMeetHint;
public Sprite BlueBG;
public Sprite GrayBG;
public Sprite GreenBG;
public Color GrayColor;
public Color GreenColor;
public Color BlueColor;
public Color RedColor;
public Color BlueTextColor;
public Color RedTextColor;
protected override void OnInit()
{
base.OnInit();
closeButton.onClick.RemoveAllListeners();
BlockButton.onClick.RemoveAllListeners();
closeButton.onClick.AddListener(() => { OnBtnCloseClick.Invoke(); });
BlockButton.onClick.AddListener(() => { OnBtnCloseClick.Invoke(); });
}
public void SetContent(uint pid)
{
if (!Main.MapData.PlayerMap.GetPlayerDataByPlayerID(pid, out var player)) return;
if (!Table.Instance.PlayerDataAssets.GetPlayerInfo(player, out var info)) return;
var selfp = Main.MapData.PlayerMap.SelfPlayerData;
player.GetCountryDiplomacyInfo(selfp.Id, out var dipInfo);
//step #1 设置AvatarArea和chatBubble
SetContentAvatarArea(player);
//Step #2 设置StatusAcrea
SetContentStatusArea(player);
//Step #3 设置AttitudeArea
SetContentAttitudeArea(player);
//Step #4
SetContentActionArea(player);
//Step #5 设置该玩家和其他玩家的关系
SetContentRelationArea(player);
//Step #6 设置BGM
AudioManager.Instance.PlayMusic(info.MusicName,1f,2f,true);
//Step #7 重新排布
LayoutRebuilder.ForceRebuildLayoutImmediate(VerticalGroup);
}
private void SetContentAvatarArea(PlayerData player)
{
bool isSelf = (player.Id == Main.MapData.PlayerMap.SelfPlayerData.Id);
var titleText = isSelf ? Table.Instance.DiplomacyDataAssets.DiplomacyUITitleSelf:Table.Instance.DiplomacyDataAssets.DiplomacyUITitle;
if (!Table.Instance.PlayerDataAssets.GetPlayerInfo(player, out var info)) return;
var forceName = MultilingualManager.Instance.GetMultilingualTextSafe(info.ForceName);
MultilingualManager.Instance.SetUIText(ForceTitle,titleText,new List<string>{forceName});
LayoutRebuilder.ForceRebuildLayoutImmediate(ForceTitleRect);
LeaderAvatar.sprite = info.LeaderAvatar;
//设置color
var color2 = info.Color;
color2.a = 0.4f;
TopStripeBG2.color = color2;
TopStripeBG.color = info.Color;
ForceTitleBG.color = info.Color;
if (isSelf)
{
BubbleChat.gameObject.SetActive(false);
return;
}
player.GetCountryDiplomacyInfo(Main.MapData.PlayerMap.SelfPlayerId, out var dipInfo);
bool meetus = dipInfo.DiplomacyState != DiplomacyState.NoDiplomacy;
BubbleChat.gameObject.SetActive(meetus);
if (!meetus) return;
MultilingualManager.Instance.SetUIText(BubbleChatText,Table.Instance.DiplomacyDataAssets.GetDiplomacyTextByFeeling((Forces)(player.PlayerForceId),dipInfo.FeelingState));
LayoutRebuilder.ForceRebuildLayoutImmediate(BubbleChat);
}
private void SetContentStatusArea(PlayerData player)
{
bool isSelf = (player.Id == Main.MapData.PlayerMap.SelfPlayerData.Id);
StatusArea.gameObject.SetActive(!isSelf);
if (isSelf) return;
if (!player.GetCountryDiplomacyInfo(Main.MapData.PlayerMap.SelfPlayerId, out var dipInfo)) return;
if (!Table.Instance.DiplomacyDataAssets.GetStateInfo(dipInfo.DiplomacyState, out var stateInfo)) return;
bool meetus = dipInfo.DiplomacyState != DiplomacyState.NoDiplomacy;
Status.gameObject.SetActive(meetus);
StateIcon.color = stateInfo.StateColor;
StateIcon.sprite = stateInfo.StateIcon;
MultilingualManager.Instance.SetUIText(StateText,stateInfo.StateText);
}
private void SetContentAttitudeArea(PlayerData player)
{
bool isSelf = (player.Id == Main.MapData.PlayerMap.SelfPlayerData.Id);
AttitudeArea.gameObject.SetActive(!isSelf);
if (isSelf) return;
if (!player.GetCountryDiplomacyInfo(Main.MapData.PlayerMap.SelfPlayerId, out var dipInfo)) return;
bool meetus = dipInfo.DiplomacyState != DiplomacyState.NoDiplomacy;
AttitudeNoMeetText.gameObject.SetActive(!meetus);
AttitudeBar.SetActive(meetus);
if(!meetus) return;
RelationHandle.anchoredPosition = new Vector2(Mathf.Lerp(13f, 585f, dipInfo.FeelingValue/100f),RelationHandle.anchoredPosition.y);
var text = $"<color=yellow>" + MultilingualManager.Instance.GetMultilingualTextSafe(Table.Instance.TextDataAssets.PresentationUIDiplomacyThinkYouText) +"</color><br>";
foreach (var op in dipInfo.FeelingStrategyList)
{
if (!Table.Instance.DiplomacyDataAssets.GetFeelingStragegyInfo(op, out var stgInfo)) continue;
text += $"<color=#{ColorUtility.ToHtmlStringRGB(stgInfo.FeelingStrategyColor)}>";
text += MultilingualManager.Instance.GetMultilingualTextSafe(stgInfo.FeelingStrategyTitle);
text += "</color>:";
text += MultilingualManager.Instance.GetMultilingualTextSafe(stgInfo.FeelingStrategyDesc);
text += "<br>";
}
AttitudeBarHint.DataProvider.HintDataType = HintDataType.TextData;
AttitudeBarHint.DataProvider.Text = text;
}
private void SetContentActionArea(PlayerData player)
{
bool isSelf = (player.Id == Main.MapData.PlayerMap.SelfPlayerData.Id);
ActionArea.gameObject.SetActive(!isSelf);
if (isSelf) return;
if (!player.GetCountryDiplomacyInfo(Main.MapData.PlayerMap.SelfPlayerId, out var dipInfo)) return;
SetAllyAction(player);
SetEmbassyAction(player);
SetBreakAllyAction(player);
}
private void SetAllyAction(PlayerData player)
{
if (player == null)
{
LogSystem.LogError("SetAllyAction Error: player is null");
return;
}
var actionId = new CommonActionId()
{ ActionType = CommonActionType.PlayerAction, PlayerActionType = PlayerActionType.OfferAlly};
var actionParams = new CommonActionParams(
mapData: Main.MapData,
playerData: Main.MapData.PlayerMap.SelfPlayerData,
targetPlayer:player,
mainObjectType: MainObjectType.Player);
var action = ActionLogicFactory.GetActionLogic(actionId);
// 检查 action 是否为 null
if (action == null)
{
LogSystem.LogError("SetAllyAction Error: action is null");
return;
}
// 检查 UI 组件
if (AllyActionButton == null || AllyActionCircleBGOutside == null || AllyActionCircleBG == null || AllyActionSend == null)
{
LogSystem.LogError($"SetAllyAction Error: UI Component is null. AllyActionButton={AllyActionButton}, AllyActionCircleBGOutside={AllyActionCircleBGOutside}, AllyActionCircleBG={AllyActionCircleBG}, AllyActionSend={AllyActionSend}");
return;
}
if (!action.CheckShow(actionParams, out var actionCantType)) return;
AllyActionButton.onClick.RemoveAllListeners();
switch (actionCantType)
{
case ShowType.Locked:
AllyActionCircleBGOutside.color = GrayColor;
AllyActionCircleBG.sprite = GrayBG;
AllyActionSend.gameObject.SetActive(false);
break;
case ShowType.None:
AllyActionCircleBGOutside.color = BlueColor;
AllyActionCircleBG.sprite = BlueBG;
AllyActionSend.gameObject.SetActive(false);
AllyActionButton.onClick.AddListener(() =>
{
//只有轮到我行动的时候,点击才生效
if (actionParams.MapData?.CurPlayer != null &&
actionParams.MapData.CurPlayer == Main.MapData?.PlayerMap?.SelfPlayerData)
{
action.CompleteExecute(actionParams);
//EventManager.Publish(new HideUIInfoDiplomacy(){non = 0});
//TODO 莫名其妙的bug publish Hide 不生效, 强制Close才生效
OnBtnCloseClick?.Invoke();
}
});
break;
case ShowType.Send:
AllyActionCircleBGOutside.color = BlueColor;
AllyActionCircleBG.sprite = BlueBG;
AllyActionSend.gameObject.SetActive(true);
break;
case ShowType.Done:
AllyActionCircleBGOutside.color = GreenColor;
AllyActionCircleBG.sprite = GreenBG;
AllyActionSend.gameObject.SetActive(false);
break;
}
}
private void SetEmbassyAction(PlayerData player)
{
if (player == null)
{
LogSystem.LogError($"SetEmbassyAction Error player is null");
return;
}
var actionId = new CommonActionId()
{ ActionType = CommonActionType.PlayerAction, PlayerActionType = PlayerActionType.Embassy};
var actionParams = new CommonActionParams(
mapData: Main.MapData,
playerData: Main.MapData.PlayerMap.SelfPlayerData,
targetPlayer:player,
mainObjectType: MainObjectType.Player);
var action = ActionLogicFactory.GetActionLogic(actionId);
if (action == null)
{
LogSystem.LogError($"SetEmbassyAction Error Action is null");
return;
}
if (!action.CheckShow(actionParams, out var actionCantType)) return;
if (!EmbassyActionButton || !EmbassyActionCircleBGOutside || !EmbassyActionCircleBG || !EmbassyActionCost ||
!EmbassyActionCostText || OnBtnCloseClick == null)
{
LogSystem.LogError($"SetEmbassyAction Error UI Component is null " +
$"{EmbassyActionButton} {EmbassyActionCircleBGOutside} {EmbassyActionCircleBG} " +
$"{EmbassyActionCost} {EmbassyActionCostText} {OnBtnCloseClick}");
return;
}
EmbassyActionButton.onClick.RemoveAllListeners();
switch (actionCantType)
{
case ShowType.Locked:
EmbassyActionCircleBGOutside.color = GrayColor;
EmbassyActionCircleBG.sprite = GrayBG;
EmbassyActionCost.gameObject.SetActive(false);
break;
case ShowType.None:
EmbassyActionCircleBGOutside.color = BlueColor;
EmbassyActionCircleBG.sprite = BlueBG;
EmbassyActionCost.gameObject.SetActive(true);
EmbassyActionCostText.color = BlueTextColor;
EmbassyActionButton.onClick.AddListener(() =>
{
//只有轮到我行动的时候,点击才生效
if (actionParams.MapData.CurPlayer != null &&
actionParams.MapData.CurPlayer == Main.MapData.PlayerMap.SelfPlayerData)
{
action.CompleteExecute(actionParams);
//EventManager.Publish(new HideUIInfoDiplomacy(){non =0});
//TODO 莫名其妙的bug publish Hide 不生效, 强制Close才生效
OnBtnCloseClick?.Invoke();
}
});
break;
case ShowType.Cost:
EmbassyActionCircleBGOutside.color = RedColor;
EmbassyActionCircleBG.sprite = BlueBG;
EmbassyActionCost.gameObject.SetActive(true);
EmbassyActionCostText.color = RedTextColor;
break;
case ShowType.Done:
EmbassyActionCircleBGOutside.color = GreenColor;
EmbassyActionCircleBG.sprite = GreenBG;
EmbassyActionCost.gameObject.SetActive(false);
break;
}
}
private void SetBreakAllyAction(PlayerData player)
{
if (player == null)
{
LogSystem.LogError("SetBreakAllyAction Error: player is null");
return;
}
var actionId = new CommonActionId()
{ ActionType = CommonActionType.PlayerAction, PlayerActionType = PlayerActionType.BreakAlly};
var actionParams = new CommonActionParams(
mapData: Main.MapData,
playerData: Main.MapData.PlayerMap.SelfPlayerData,
targetPlayer:player,
mainObjectType: MainObjectType.Player);
var action = ActionLogicFactory.GetActionLogic(actionId);
// 检查 action 是否为 null
if (action == null)
{
LogSystem.LogError("SetBreakAllyAction Error: action is null");
return;
}
// 检查 UI 组件
if (BreakAllyAction == null || BreakAllyActionButton == null)
{
LogSystem.LogError($"SetBreakAllyAction Error: UI Component is null. BreakAllyAction={BreakAllyAction}, BreakAllyActionButton={BreakAllyActionButton}");
return;
}
bool canShow = action.CheckShow(actionParams, out var actionCantType);
BreakAllyAction.gameObject.SetActive(canShow);
BreakAllyActionButton.onClick.RemoveAllListeners();
if (!canShow) return;
BreakAllyActionButton.onClick.AddListener(() =>
{
//只有轮到我行动的时候,点击才生效
if (actionParams.MapData.CurPlayer != null && actionParams.MapData.CurPlayer == Main.MapData.PlayerMap.SelfPlayerData)
{
action.CompleteExecute(actionParams);
//TODO 莫名其妙的bug publish Hide 不生效, 强制Close才生效 _task居然=空 导致无法关闭
//EventManager.Publish(new HideUIInfoDiplomacy());
OnBtnCloseClick?.Invoke();
}
});
}
private void SetContentRelationArea(PlayerData player)
{
//判断鲁棒性
if (player.MeetPlayers.Count - 1 > OtherRelationAvatarList.Count) return;
int rk = 0;
//依次设置pid对她的每一个pid2的外交关系
foreach (var pid2 in player.MeetPlayers)
{
if (pid2 == player.Id) continue;
if(!Main.MapData.PlayerMap.GetPlayerDataByPlayerID(pid2,out var player2))continue;
if (!player2.Alive) continue;
player.GetCountryDiplomacyInfo(pid2, out var dipInfo2);
if(!Table.Instance.DiplomacyDataAssets.GetStateInfo(dipInfo2.DiplomacyState, out var stateInfo2))continue;
OtherRelationAvatarList[rk].gameObject.SetActive(true);
//设置名字、头像、外交状态
OtherRelationAvatarList[rk]?.SetContent(player2,stateInfo2.DiplomacyState,
!Main.MapData.PlayerMap.SelfPlayerData.MeetPlayers.Contains(pid2));
rk++;
}
RelationNoMeetHint.SetActive(rk == 0);
for(;rk < OtherRelationAvatarList.Count;rk++)
OtherRelationAvatarList[rk].gameObject.SetActive(false);
}
public void CloseView()
{
AudioManager.Instance.StopMusic();
}
}
}