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
|
|
|
|
{
|
2022-08-14 12:41:50 +01:00
|
|
|
|
private PlayerInfo _player;
|
2022-03-02 19:46:33 -08:00
|
|
|
|
private CanvasMapMarker _canvasMarker;
|
|
|
|
|
private bool _canvasMarkerInitialized;
|
2022-08-14 12:41:50 +01:00
|
|
|
|
private bool _hasBeenSetUpForInit;
|
2022-03-02 19:46:33 -08:00
|
|
|
|
|
|
|
|
|
public void Awake()
|
2022-02-24 22:04:54 -08:00
|
|
|
|
{
|
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-02-27 04:40:44 -08:00
|
|
|
|
|
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
|
|
|
|
|
2022-08-14 12:41:50 +01: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);
|
2022-08-14 12:41:50 +01:00
|
|
|
|
markerManager.RegisterMarker(_canvasMarker, transform);
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2022-08-14 12:41:50 +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-02-27 04:40:44 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
private void OnEnterMapView() => enabled = true;
|
|
|
|
|
private void OnExitMapView() => enabled = false;
|
|
|
|
|
|
2022-08-14 12:41:50 +01:00
|
|
|
|
private bool ShouldBeVisible()
|
|
|
|
|
{
|
|
|
|
|
if (_player == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var playerScreenPos = Locator.GetActiveCamera().WorldToScreenPoint(transform.position);
|
|
|
|
|
var isInfrontOfCamera = playerScreenPos.z > 0f;
|
|
|
|
|
|
2022-08-22 21:26:38 -07:00
|
|
|
|
return _player.IsReady && !_player.IsDead && (!_player.InDreamWorld || QSBPlayerManager.LocalPlayer.InDreamWorld) && _player.Visible && isInfrontOfCamera;
|
2022-08-14 12:41:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public void LateUpdate()
|
|
|
|
|
{
|
2022-08-14 12:41:50 +01:00
|
|
|
|
if (!_canvasMarkerInitialized && _hasBeenSetUpForInit)
|
2022-02-24 22:04:54 -08:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
InitMarker();
|
|
|
|
|
}
|
2022-02-27 04:40:44 -08:00
|
|
|
|
|
2022-08-14 12:41:50 +01:00
|
|
|
|
var shouldBeVisible = ShouldBeVisible();
|
|
|
|
|
|
|
|
|
|
if (_canvasMarker.IsVisible() != shouldBeVisible)
|
2022-03-02 19:46:33 -08:00
|
|
|
|
{
|
2022-08-14 12:41:50 +01:00
|
|
|
|
_canvasMarker.SetVisibility(shouldBeVisible);
|
2020-12-20 18:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-24 22:04:54 -08:00
|
|
|
|
}
|