287 lines
8.2 KiB
C#
287 lines
8.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using FishNet.Connection;
|
|
using FishNet.Managing;
|
|
using FishNet.Object;
|
|
using Heathen.SteamworksIntegration;
|
|
using SoapCustomVariable;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class UI_MultiplayerNickDisplay : MonoBehaviour
|
|
{
|
|
private class RemotePlayerInfo
|
|
{
|
|
public int clientID;
|
|
|
|
public FishNetSpawnerProxy proxy;
|
|
|
|
public MultiplayerAvatarControler avatar;
|
|
|
|
public TMP_Text text;
|
|
|
|
public Image crown;
|
|
}
|
|
|
|
private class RemoteBoatInfo
|
|
{
|
|
public NetworkConnection owner;
|
|
|
|
public FishNetSpawnerProxy proxy;
|
|
|
|
public MultiplayerAvatarControler avatar;
|
|
|
|
public MultiplayerBoatController boat;
|
|
|
|
public TMP_Text text;
|
|
|
|
public Image crown;
|
|
}
|
|
|
|
[SerializeField]
|
|
private TMP_Text textTemplate;
|
|
|
|
[SerializeField]
|
|
private ScriptableLobbyDataVariable scriptable_variable_LobbyData;
|
|
|
|
private List<RemotePlayerInfo> remotePlayers = new List<RemotePlayerInfo>();
|
|
|
|
private List<RemoteBoatInfo> remoteBoats = new List<RemoteBoatInfo>();
|
|
|
|
private NetworkManager NetworkManager
|
|
{
|
|
get
|
|
{
|
|
if (NetworkManager.Instances.Count <= 0)
|
|
{
|
|
return null;
|
|
}
|
|
return NetworkManager.Instances[0];
|
|
}
|
|
}
|
|
|
|
private void MultiplayerBoatController_OnStartRemote(MultiplayerBoatController multiplayerBoatController)
|
|
{
|
|
remoteBoats.Add(new RemoteBoatInfo
|
|
{
|
|
owner = multiplayerBoatController.Owner,
|
|
proxy = null,
|
|
avatar = null,
|
|
boat = multiplayerBoatController,
|
|
text = null
|
|
});
|
|
}
|
|
|
|
private void MultiplayerBoatController_OnStopRemote(MultiplayerBoatController multiplayerBoatController)
|
|
{
|
|
RemoteBoatInfo remoteBoatInfo = remoteBoats.FirstOrDefault((RemoteBoatInfo element) => element.boat == multiplayerBoatController);
|
|
if (remoteBoatInfo != null)
|
|
{
|
|
if (remoteBoatInfo.text != null)
|
|
{
|
|
Object.Destroy(remoteBoatInfo.text.gameObject);
|
|
}
|
|
remoteBoats.Remove(remoteBoatInfo);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
textTemplate.gameObject.SetActive(value: false);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
MultiplayerBoatController.OnStartRemote += MultiplayerBoatController_OnStartRemote;
|
|
MultiplayerBoatController.OnStopRemote += MultiplayerBoatController_OnStopRemote;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
MultiplayerBoatController.OnStartRemote -= MultiplayerBoatController_OnStartRemote;
|
|
MultiplayerBoatController.OnStopRemote -= MultiplayerBoatController_OnStopRemote;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!NetworkManager || !NetworkManager.IsClientStarted)
|
|
{
|
|
if (remotePlayers != null)
|
|
{
|
|
foreach (RemotePlayerInfo remotePlayer in remotePlayers)
|
|
{
|
|
if (remotePlayer?.text != null)
|
|
{
|
|
Object.Destroy(remotePlayer.text.gameObject);
|
|
}
|
|
}
|
|
remotePlayers.Clear();
|
|
}
|
|
if (remoteBoats == null)
|
|
{
|
|
return;
|
|
}
|
|
foreach (RemoteBoatInfo remoteBoat in remoteBoats)
|
|
{
|
|
if (remoteBoat?.text != null)
|
|
{
|
|
Object.Destroy(remoteBoat.text.gameObject);
|
|
}
|
|
}
|
|
remoteBoats.Clear();
|
|
}
|
|
else
|
|
{
|
|
UpdateRemotePlayersList();
|
|
UpdateRemotePlayersNicks();
|
|
UpdateRemoteBoatsNicks();
|
|
}
|
|
}
|
|
|
|
private void UpdateRemotePlayersList()
|
|
{
|
|
List<RemotePlayerInfo> list = new List<RemotePlayerInfo>();
|
|
foreach (RemotePlayerInfo remotePlayer in remotePlayers)
|
|
{
|
|
if (!NetworkManager.ClientManager.Clients.ContainsKey(remotePlayer.clientID) || !NetworkManager.ClientManager.Clients[remotePlayer.clientID].IsValid || remotePlayer.proxy == null || remotePlayer.avatar == null)
|
|
{
|
|
list.Add(remotePlayer);
|
|
}
|
|
}
|
|
foreach (RemotePlayerInfo item in list)
|
|
{
|
|
if (item.text != null)
|
|
{
|
|
Object.Destroy(item.text.gameObject);
|
|
}
|
|
remotePlayers.Remove(item);
|
|
}
|
|
foreach (KeyValuePair<int, NetworkConnection> item2 in NetworkManager.IsServerStarted ? NetworkManager.ServerManager.Clients : NetworkManager.ClientManager.Clients)
|
|
{
|
|
if (!item2.Value.IsValid || item2.Value.ClientId == NetworkManager.ClientManager.Connection.ClientId)
|
|
{
|
|
continue;
|
|
}
|
|
int clientId = item2.Key;
|
|
if (!remotePlayers.Any((RemotePlayerInfo p) => p.clientID == clientId))
|
|
{
|
|
FishNetSpawnerProxy fishNetSpawnerProxy = item2.Value.Objects.FirstOrDefault((NetworkObject element) => element.GetComponent<FishNetSpawnerProxy>() != null)?.GetComponent<FishNetSpawnerProxy>();
|
|
MultiplayerAvatarControler multiplayerAvatarControler = item2.Value.Objects.FirstOrDefault((NetworkObject element) => element.GetComponent<MultiplayerAvatarControler>() != null)?.GetComponent<MultiplayerAvatarControler>();
|
|
if (fishNetSpawnerProxy != null && multiplayerAvatarControler != null)
|
|
{
|
|
TMP_Text tMP_Text = Object.Instantiate(textTemplate, textTemplate.transform.parent);
|
|
tMP_Text.gameObject.SetActive(value: true);
|
|
remotePlayers.Add(new RemotePlayerInfo
|
|
{
|
|
clientID = clientId,
|
|
proxy = fishNetSpawnerProxy,
|
|
avatar = multiplayerAvatarControler,
|
|
text = tMP_Text,
|
|
crown = tMP_Text.GetComponentInChildren<Image>()
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void UpdateRemotePlayersNicks()
|
|
{
|
|
foreach (RemotePlayerInfo remotePlayer in remotePlayers)
|
|
{
|
|
remotePlayer.text.text = remotePlayer.proxy.ProfileName;
|
|
remotePlayer.crown.gameObject.SetActive(IsLobbyOwner(remotePlayer.proxy.SteamID));
|
|
PlaceTextAboveObject(remotePlayer.text, remotePlayer.avatar.gameObject);
|
|
}
|
|
}
|
|
|
|
private void UpdateRemoteBoatsNicks()
|
|
{
|
|
foreach (RemoteBoatInfo remoteBoat in remoteBoats)
|
|
{
|
|
if (remoteBoat.owner == null || !remoteBoat.owner.IsValid || remoteBoat == null || remoteBoat.boat == null || !remoteBoat.boat.IsSpawned)
|
|
{
|
|
continue;
|
|
}
|
|
RemoteBoatInfo remoteBoatInfo = remoteBoat;
|
|
if ((object)remoteBoatInfo.proxy == null)
|
|
{
|
|
remoteBoatInfo.proxy = remoteBoat.owner.Objects.FirstOrDefault((NetworkObject element) => element.GetComponent<FishNetSpawnerProxy>() != null)?.GetComponent<FishNetSpawnerProxy>();
|
|
}
|
|
remoteBoatInfo = remoteBoat;
|
|
if ((object)remoteBoatInfo.avatar == null)
|
|
{
|
|
remoteBoatInfo.avatar = remoteBoat.owner.Objects.FirstOrDefault((NetworkObject element) => element.GetComponent<MultiplayerAvatarControler>() != null)?.GetComponent<MultiplayerAvatarControler>();
|
|
}
|
|
if (!(remoteBoat.proxy == null) && !(remoteBoat.avatar == null))
|
|
{
|
|
if (remoteBoat.text == null)
|
|
{
|
|
remoteBoat.text = Object.Instantiate(textTemplate, textTemplate.transform.parent);
|
|
remoteBoat.crown = remoteBoat.text.GetComponentInChildren<Image>();
|
|
remoteBoat.text.gameObject.SetActive(value: true);
|
|
}
|
|
remoteBoat.text.text = "Property of " + remoteBoat.proxy.ProfileName;
|
|
remoteBoat.crown.gameObject.SetActive(value: false);
|
|
PlaceTextAboveObject(remoteBoat.text, remoteBoat.boat.gameObject);
|
|
if (remoteBoat.avatar.State == State.vehicle || remoteBoat.avatar.State == State.vehicleFishing)
|
|
{
|
|
remoteBoat.text.gameObject.SetActive(value: false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private string GetPlayerName(ulong SteamID)
|
|
{
|
|
string result = "PlayerUnknown";
|
|
if (scriptable_variable_LobbyData.Value.IsValid)
|
|
{
|
|
LobbyMemberData[] members = scriptable_variable_LobbyData.Value.Members;
|
|
for (int i = 0; i < members.Length; i++)
|
|
{
|
|
LobbyMemberData lobbyMemberData = members[i];
|
|
if (lobbyMemberData.user.SteamId == SteamID)
|
|
{
|
|
result = lobbyMemberData.user.Nickname;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private bool IsLobbyOwner(ulong SteamID)
|
|
{
|
|
if (scriptable_variable_LobbyData.Value.IsValid)
|
|
{
|
|
return scriptable_variable_LobbyData.Value.Owner.user.SteamId == SteamID;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void PlaceTextAboveObject(TMP_Text text, GameObject gameObject, float verticalOffset = 2f, float viewRange = 50f)
|
|
{
|
|
if (Camera.main == null)
|
|
{
|
|
text.gameObject.SetActive(value: false);
|
|
return;
|
|
}
|
|
float num = Vector3.Distance(Camera.main.transform.position, gameObject.transform.position);
|
|
if (num > viewRange)
|
|
{
|
|
text.gameObject.SetActive(value: false);
|
|
return;
|
|
}
|
|
text.gameObject.SetActive(value: true);
|
|
Vector3 vector = gameObject.transform.position - Camera.main.transform.position;
|
|
if (Vector3.Dot(Camera.main.transform.forward, vector.normalized) < 0f)
|
|
{
|
|
text.gameObject.SetActive(value: false);
|
|
return;
|
|
}
|
|
text.transform.position = Camera.main.WorldToScreenPoint(gameObject.transform.position + Vector3.up * verticalOffset);
|
|
text.transform.localScale = Vector3.Lerp(Vector3.one, new Vector3(0.5f, 0.5f, 0.5f), num / viewRange);
|
|
}
|
|
}
|