1
0
mirror of https://github.com/misternebula/quantum-space-buddies.git synced 2025-01-27 03:35:20 +00:00

85 lines
2.0 KiB
C#
Raw Normal View History

2022-08-27 11:44:52 -07:00
using QSB.Utility;
using UnityEngine;
2020-12-20 18:16:23 +00:00
2022-03-02 19:46:33 -08:00
namespace QSB.Player;
2022-08-27 11:44:52 -07:00
[UsedInUnityProject]
2022-03-02 19:46:33 -08:00
public class PlayerMapMarker : MonoBehaviour
2020-12-20 18:16:23 +00:00
{
private PlayerInfo _player;
2022-03-02 19:46:33 -08:00
private CanvasMapMarker _canvasMarker;
private bool _canvasMarkerInitialized;
private bool _hasBeenSetUpForInit;
2022-03-02 19:46:33 -08:00
public void Awake()
{
2022-03-02 19:46:33 -08:00
GlobalMessenger.AddListener("EnterMapView", new Callback(OnEnterMapView));
GlobalMessenger.AddListener("ExitMapView", new Callback(OnExitMapView));
}
2020-12-20 18:16:23 +00:00
2022-03-02 19:46:33 -08:00
public void Start()
{
enabled = false;
}
2022-03-02 19:46:33 -08: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;
_hasBeenSetUpForInit = true;
}
2022-03-02 19:46:33 -08: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 22:38:32 +01:00
_canvasMarker.SetLabel(_player.Name.ToUpper());
2022-03-02 19:46:33 -08:00
_canvasMarker.SetColor(Color.white);
_canvasMarker.SetVisibility(false);
_canvasMarkerInitialized = true;
}
2022-03-02 19:46:33 -08:00
private void OnEnterMapView() => enabled = true;
private void OnExitMapView() => enabled = false;
private bool ShouldBeVisible()
{
if (_player == null)
{
return false;
}
var playerScreenPos = Locator.GetActiveCamera().WorldToScreenPoint(transform.position);
var isInfrontOfCamera = playerScreenPos.z > 0f;
2022-10-16 18:42:52 -07:00
return isInfrontOfCamera &&
_player.IsReady &&
!_player.IsDead &&
_player.Visible &&
_player.InDreamWorld == QSBPlayerManager.LocalPlayer.InDreamWorld &&
_player.IsInMoon == QSBPlayerManager.LocalPlayer.IsInMoon;
}
2022-03-02 19:46:33 -08:00
public void LateUpdate()
{
if (!_canvasMarkerInitialized && _hasBeenSetUpForInit)
{
2022-03-02 19:46:33 -08:00
InitMarker();
}
var shouldBeVisible = ShouldBeVisible();
if (_canvasMarker.IsVisible() != shouldBeVisible)
2022-03-02 19:46:33 -08:00
{
_canvasMarker.SetVisibility(shouldBeVisible);
2020-12-20 18:16:23 +00:00
}
}
}