quantum-space-buddies/QSB/Player/PlayerMapMarker.cs

97 lines
2.2 KiB
C#
Raw Normal View History

2022-11-07 20:14:03 +00:00
using QSB.ServerSettings;
using QSB.Utility;
2022-08-27 18:44:52 +00:00
using UnityEngine;
2020-12-20 18:16:23 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.Player;
2022-08-27 18:44:52 +00:00
[UsedInUnityProject]
2022-03-03 03:46:33 +00:00
public class PlayerMapMarker : MonoBehaviour
2020-12-20 18:16:23 +00:00
{
private PlayerInfo _player;
2022-03-03 03:46:33 +00:00
private CanvasMapMarker _canvasMarker;
private bool _canvasMarkerInitialized;
private bool _hasBeenSetUpForInit;
2022-03-03 03:46:33 +00:00
public void Awake()
{
2022-03-03 03:46:33 +00:00
GlobalMessenger.AddListener("EnterMapView", new Callback(OnEnterMapView));
GlobalMessenger.AddListener("ExitMapView", new Callback(OnExitMapView));
}
2020-12-20 18:16:23 +00:00
2022-03-03 03:46:33 +00:00
public void Start()
{
enabled = false;
}
2022-03-03 03:46:33 +00:00
public void OnDestroy()
{
GlobalMessenger.RemoveListener("EnterMapView", new Callback(OnEnterMapView));
GlobalMessenger.RemoveListener("ExitMapView", new Callback(OnExitMapView));
}
2020-12-20 18:16:23 +00:00
public void Init(PlayerInfo player)
{
_player = player;
_player.MapMarker = this;
_hasBeenSetUpForInit = true;
}
2022-03-03 03:46:33 +00:00
public void InitMarker()
{
var obj = GameObject.FindWithTag("MapCamera");
var markerManager = obj.GetRequiredComponent<MapController>().GetMarkerManager();
_canvasMarker = markerManager.InstantiateNewMarker(true);
markerManager.RegisterMarker(_canvasMarker, transform);
2021-06-18 21:38:32 +00:00
_canvasMarker.SetLabel(_player.Name.ToUpper());
2022-03-03 03:46:33 +00:00
_canvasMarker.SetColor(Color.white);
_canvasMarker.SetVisibility(false);
_canvasMarkerInitialized = true;
}
2022-03-03 03:46:33 +00:00
private void OnEnterMapView() => enabled = true;
private void OnExitMapView() => enabled = false;
private bool ShouldBeVisible()
{
if (_player == null)
{
return false;
}
2022-11-07 20:14:03 +00:00
if (!ServerSettingsManager.ShowPlayerNames)
{
return false;
}
var playerScreenPos = Locator.GetActiveCamera().WorldToScreenPoint(transform.position);
var isInfrontOfCamera = playerScreenPos.z > 0f;
2022-10-17 01:42:52 +00:00
return isInfrontOfCamera &&
_player.IsReady &&
!_player.IsDead &&
_player.Visible &&
_player.InDreamWorld == QSBPlayerManager.LocalPlayer.InDreamWorld &&
_player.IsInMoon == QSBPlayerManager.LocalPlayer.IsInMoon;
}
2022-03-03 03:46:33 +00:00
public void LateUpdate()
{
if (!_canvasMarkerInitialized && _hasBeenSetUpForInit)
{
2022-03-03 03:46:33 +00:00
InitMarker();
}
var shouldBeVisible = ShouldBeVisible();
if (_canvasMarker.IsVisible() != shouldBeVisible)
2022-03-03 03:46:33 +00:00
{
_canvasMarker.SetVisibility(shouldBeVisible);
2020-12-20 18:16:23 +00:00
}
}
public void Remove()
{
// TODO
}
}