227 lines
8.0 KiB
C#
227 lines
8.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Logic;
|
|
using Logic.Multilingual;
|
|
using RuntimeData;
|
|
using TH1_Core.Events;
|
|
using TH1_Core.Managers;
|
|
using TH1_Logic.Core;
|
|
using TH1_Logic.Net;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems; // 需要引入此命名空间以处理指针事件
|
|
|
|
namespace TH1_UI.View.Info
|
|
{
|
|
public class UIBottomRankingRowMono : MonoBehaviour
|
|
{
|
|
public Image Medal;
|
|
public List<Sprite> MedalSprite;
|
|
public Color NormalRankTextColor;
|
|
public List<Color> MedalRankTextColor;
|
|
|
|
public Image BG;
|
|
public Sprite NormalBG;
|
|
public Sprite SelfBG;
|
|
public Button Button;
|
|
public Image AvatarMask;
|
|
public Image Avatar;
|
|
public GameObject Union;
|
|
public TextMeshProUGUI Rank;
|
|
public TextMeshProUGUI ForceName;
|
|
public TextMeshProUGUI PlayerName;
|
|
public TextMeshProUGUI City;
|
|
public TextMeshProUGUI Score;
|
|
|
|
public Material GrayScaleMat;
|
|
private PlayerData _player;
|
|
private MultilingualType _lastLanguage;
|
|
private bool _lastUseOnlinePlayerName;
|
|
private string _lastOnlinePlayerName;
|
|
|
|
private void Awake()
|
|
{
|
|
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_player == null) return;
|
|
if (Main.MapData?.PlayerMap == null) return;
|
|
var currentLanguage = MultilingualManager.Instance.CurrentType;
|
|
var useOnlinePlayerName = TryGetOnlinePlayerName(_player, out var onlinePlayerName);
|
|
if (_lastLanguage == currentLanguage
|
|
&& _lastUseOnlinePlayerName == useOnlinePlayerName
|
|
&& string.Equals(_lastOnlinePlayerName, onlinePlayerName, StringComparison.Ordinal)) return;
|
|
_lastLanguage = currentLanguage;
|
|
RefreshForceName(useOnlinePlayerName, onlinePlayerName);
|
|
}
|
|
|
|
public void SetContent(int rk, PlayerData player)
|
|
{
|
|
if (!Table.Instance.PlayerDataAssets.GetPlayerInfo(player, out var playerInfo)) return;
|
|
var selfPlayer = Main.MapData.PlayerMap.SelfPlayerData;
|
|
_player = player;
|
|
_lastLanguage = MultilingualManager.Instance.CurrentType;
|
|
|
|
Rank.text = (rk + 1).ToString();
|
|
BG.sprite = player == Main.MapData.PlayerMap.SelfPlayerData ? SelfBG : NormalBG;
|
|
Medal.gameObject.SetActive(rk < 3);
|
|
Rank.color = NormalRankTextColor;
|
|
if (rk is >= 0 and < 3)
|
|
{
|
|
Medal.sprite = MedalSprite[rk];
|
|
Rank.color = MedalRankTextColor[rk];
|
|
}
|
|
|
|
Button.onClick.RemoveAllListeners();
|
|
|
|
var col = AvatarMask.color;
|
|
col.a = 0.01f;
|
|
AvatarMask.color = col;
|
|
|
|
//死亡的对手,未相遇也会呈现真名和avatar
|
|
if (!player.IsSurvival)
|
|
{
|
|
RefreshForceName();
|
|
Avatar.sprite = playerInfo.LeaderAvatar;
|
|
Avatar.material = GrayScaleMat;
|
|
col = AvatarMask.color;
|
|
col.a = 0;
|
|
AvatarMask.color = col;
|
|
Union.SetActive(false);
|
|
}
|
|
//未相遇的对手
|
|
else if (!HasMetPlayer(selfPlayer, player))
|
|
{
|
|
RefreshForceName();
|
|
Avatar.sprite = Table.Instance.PlayerDataAssets.CommonPlayerAvatar;
|
|
Avatar.material = null;
|
|
Union.SetActive(false);
|
|
}
|
|
//常规对手
|
|
else
|
|
{
|
|
RefreshForceName();
|
|
Avatar.sprite = playerInfo.LeaderAvatar;
|
|
Avatar.material = null;
|
|
|
|
var tt = player.Id;
|
|
Button.onClick.AddListener(() =>
|
|
{
|
|
|
|
var infoUI = new ShowUIInfoDiplomacy() { Pid = tt };
|
|
EventManager.Publish(infoUI);
|
|
});
|
|
Union.SetActive(player != selfPlayer && Main.MapData.SameUnion(selfPlayer.Id, player.Id));
|
|
|
|
|
|
}
|
|
|
|
City.text = Main.MapData.GetCityCount(player.Id).ToString();
|
|
Score.text = player.PlayerScore.ToString();
|
|
}
|
|
|
|
private void RefreshForceName()
|
|
{
|
|
if (_player == null || Main.MapData == null) return;
|
|
RefreshForceName(TryGetOnlinePlayerName(_player, out var onlinePlayerName), onlinePlayerName);
|
|
}
|
|
|
|
private void RefreshForceName(bool useOnlinePlayerName, string onlinePlayerName)
|
|
{
|
|
if (_player == null || Main.MapData?.PlayerMap == null) return;
|
|
_lastUseOnlinePlayerName = useOnlinePlayerName;
|
|
_lastOnlinePlayerName = onlinePlayerName;
|
|
|
|
if (useOnlinePlayerName && PlayerName != null)
|
|
{
|
|
SetForceNameActive(false);
|
|
SetPlayerNameActive(true);
|
|
ClearMultilingualTextId(PlayerName);
|
|
PlayerName.text = onlinePlayerName;
|
|
PlayerName.color = NormalRankTextColor;
|
|
|
|
return;
|
|
}
|
|
|
|
SetPlayerNameActive(false);
|
|
SetForceNameActive(true);
|
|
if (ForceName == null) return;
|
|
ClearMultilingualTextId(ForceName);
|
|
|
|
var selfPlayer = Main.MapData.PlayerMap.SelfPlayerData;
|
|
if (!_player.IsSurvival)
|
|
{
|
|
ForceName.text = Logic.PlayerLogic.GetDisplayForceName(Main.MapData, _player);
|
|
ForceName.color = Color.gray;
|
|
}
|
|
else if (!HasMetPlayer(selfPlayer, _player))
|
|
{
|
|
ForceName.text = "???";
|
|
ForceName.color = NormalRankTextColor;
|
|
}
|
|
else
|
|
{
|
|
ForceName.text = Logic.PlayerLogic.GetDisplayForceName(Main.MapData, _player);
|
|
ForceName.color = NormalRankTextColor;
|
|
}
|
|
}
|
|
|
|
private bool TryGetOnlinePlayerName(PlayerData player, out string playerName)
|
|
{
|
|
playerName = null;
|
|
var mapData = Main.MapData;
|
|
if (player == null || mapData?.Net?.Mode != NetMode.Multi) return false;
|
|
|
|
var lobby = LobbyManager.Instance?.Lobby;
|
|
if (lobby == null || !lobby.IsInLobby()) return false;
|
|
|
|
var slot = mapData.MapConfig?.MultiCivs?.Find(civ => civ != null && civ.PlayerId == player.Id);
|
|
var memberId = slot?.MemberId ?? 0;
|
|
if (memberId == 0 && slot != null && slot.IsHostControlled) memberId = lobby.GetLobbyOwnerId();
|
|
if (memberId == 0) memberId = mapData.Net.GetmemberIdId(player.Id);
|
|
if (memberId == 0 || IsAIControlled(memberId)) return false;
|
|
|
|
var memberInfo = lobby.GetMemberInfo(memberId);
|
|
if (string.IsNullOrWhiteSpace(memberInfo?.Name)) return false;
|
|
|
|
playerName = memberInfo.Name;
|
|
return true;
|
|
}
|
|
|
|
private static bool IsAIControlled(ulong memberId)
|
|
{
|
|
var confirm = Main.Instance?.ConfirmMap?.GetPlayerConfirm(memberId);
|
|
return confirm != null && confirm.IsNeedAI();
|
|
}
|
|
|
|
private static bool HasMetPlayer(PlayerData selfPlayer, PlayerData player)
|
|
{
|
|
if (selfPlayer == null || player == null) return false;
|
|
if (selfPlayer.Id == player.Id) return true;
|
|
return selfPlayer.MeetPlayers?.Contains(player.Id) == true;
|
|
}
|
|
|
|
private void SetForceNameActive(bool active)
|
|
{
|
|
if (ForceName != null) ForceName.gameObject.SetActive(active);
|
|
}
|
|
|
|
private void SetPlayerNameActive(bool active)
|
|
{
|
|
if (PlayerName != null) PlayerName.gameObject.SetActive(active);
|
|
}
|
|
|
|
private static void ClearMultilingualTextId(TextMeshProUGUI text)
|
|
{
|
|
var multilingual = text.GetComponent<MultilingualTextMono>();
|
|
if (multilingual == null) return;
|
|
multilingual.ID = 0;
|
|
multilingual.ParamList = null;
|
|
}
|
|
|
|
}
|
|
}
|