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

108 lines
1.9 KiB
C#
Raw Normal View History

2022-11-07 20:14:03 +00:00
using QSB.ServerSettings;
using QSB.Utility;
using UnityEngine;
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 PlayerHUDMarker : HUDDistanceMarker
{
2022-03-03 03:46:33 +00:00
private PlayerInfo _player;
private bool _needsInitializing;
private bool _isReady;
public override void InitCanvasMarker()
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
_markerRadius = 2f;
2020-12-02 21:23:01 +00:00
2022-03-03 03:46:33 +00:00
_markerTarget = new GameObject().transform;
_markerTarget.parent = transform;
2020-12-02 21:23:01 +00:00
2022-03-03 03:46:33 +00:00
_markerTarget.localPosition = Vector3.up * 0.25f;
}
2020-12-02 21:23:01 +00:00
2022-03-03 03:46:33 +00:00
public void Init(PlayerInfo player)
{
_player = player;
_player.HudMarker = this;
_needsInitializing = true;
}
2020-12-02 21:23:01 +00:00
2022-08-13 10:52:11 +00:00
private bool ShouldBeVisible()
{
if (_player == null)
{
return false;
}
2022-11-07 20:14:03 +00:00
if (!ServerSettingsManager.ShowPlayerNames)
{
return false;
}
2022-10-17 01:42:52 +00:00
return _player.IsReady &&
!_player.IsDead &&
_player.Visible &&
_player.InDreamWorld == QSBPlayerManager.LocalPlayer.InDreamWorld &&
_player.IsInMoon == QSBPlayerManager.LocalPlayer.IsInMoon;
2022-08-13 10:52:11 +00:00
}
2022-03-03 03:46:33 +00:00
private void Update()
{
if (_needsInitializing)
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
Initialize();
}
2021-06-23 21:05:41 +00:00
2022-03-03 03:46:33 +00:00
if (!_isReady || !_player.IsReady)
{
2022-03-03 03:46:33 +00:00
return;
}
2022-03-03 03:46:33 +00:00
if (_canvasMarker != null)
{
var isVisible = _canvasMarker.IsVisible();
2022-08-13 10:52:11 +00:00
if (ShouldBeVisible() != isVisible)
{
2022-08-13 10:52:11 +00:00
_canvasMarker.SetVisibility(ShouldBeVisible());
}
}
2022-03-03 03:46:33 +00:00
else
{
2022-03-06 09:01:02 +00:00
DebugLog.ToConsole($"Warning - _canvasMarker for {_player} is null!", OWML.Common.MessageType.Warning);
2022-03-03 03:46:33 +00:00
}
}
2021-07-06 21:29:47 +00:00
2022-03-03 03:46:33 +00:00
private void Initialize()
{
if (_player.Name == null)
{
2022-03-06 09:01:02 +00:00
DebugLog.ToConsole($"Error - {_player} has a null name!", OWML.Common.MessageType.Error);
2022-03-03 03:46:33 +00:00
_player.Name = "NULL";
}
2020-12-02 21:23:01 +00:00
2022-03-03 03:46:33 +00:00
_markerLabel = _player.Name.ToUpper();
_needsInitializing = false;
_isReady = true;
2020-12-02 21:23:01 +00:00
2022-03-03 03:46:33 +00:00
base.InitCanvasMarker();
}
2021-06-18 21:38:32 +00:00
2022-03-03 03:46:33 +00:00
public void Remove()
{
_isReady = false;
// do N O T destroy the parent - it completely breaks the ENTIRE GAME
if (_canvasMarker != null)
{
2022-03-03 03:46:33 +00:00
_canvasMarker.DestroyMarker();
}
2022-03-03 03:46:33 +00:00
if (_markerTarget != null)
{
Destroy(_markerTarget.gameObject);
}
2022-03-03 03:46:33 +00:00
Destroy(this);
2020-12-02 21:23:01 +00:00
}
2020-12-03 08:28:05 +00:00
}