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

89 lines
1.7 KiB
C#
Raw Normal View History

using QSB.Utility;
using UnityEngine;
2020-11-03 22:29:23 +00:00
namespace QSB.Player
{
2020-12-02 21:23:01 +00:00
public class PlayerHUDMarker : HUDDistanceMarker
{
private PlayerInfo _player;
private bool _needsInitializing;
2021-06-23 21:05:41 +00:00
private bool _isReady;
2020-12-02 21:23:01 +00:00
protected override void InitCanvasMarker()
{
_markerRadius = 2f;
_markerTarget = new GameObject().transform;
_markerTarget.parent = transform;
_markerTarget.localPosition = Vector3.up * 2;
}
public void Init(PlayerInfo player)
{
DebugLog.DebugWrite($"Init {player.PlayerId} name:{player.Name}");
2020-12-02 21:23:01 +00:00
_player = player;
_player.HudMarker = this;
_needsInitializing = true;
2020-12-02 21:23:01 +00:00
}
2021-06-23 21:05:41 +00:00
private void Update()
2020-12-02 21:23:01 +00:00
{
2021-06-23 21:05:41 +00:00
if (_needsInitializing)
{
Initialize();
}
if (!_isReady || !_player.PlayerStates.IsReady)
{
return;
}
2020-12-02 21:23:01 +00:00
if (_canvasMarker != null)
{
var isVisible = _canvasMarker.IsVisible();
if (_player.Visible != isVisible)
{
_canvasMarker.SetVisibility(_player.Visible);
}
2020-12-02 21:23:01 +00:00
}
2021-06-23 21:05:41 +00:00
else
2020-12-02 21:23:01 +00:00
{
2021-06-23 21:05:41 +00:00
DebugLog.DebugWrite($"Warning - _canvasMarker for {_player.PlayerId} is null!", OWML.Common.MessageType.Warning);
2020-12-02 21:23:01 +00:00
}
}
private void Initialize()
{
2021-07-06 21:29:47 +00:00
if (_player.Name == null)
{
DebugLog.ToConsole($"Error - {_player.PlayerId} has a null name!", OWML.Common.MessageType.Error);
_player.Name = "NULL";
}
2020-12-02 21:23:01 +00:00
_markerLabel = _player.Name.ToUpper();
_needsInitializing = false;
2021-06-23 21:05:41 +00:00
_isReady = true;
2020-12-02 21:23:01 +00:00
base.InitCanvasMarker();
}
public void Remove()
{
2021-06-23 21:05:41 +00:00
_isReady = false;
2020-12-02 21:23:01 +00:00
// do N O T destroy the parent - it completely breaks the ENTIRE GAME
2021-03-25 20:56:26 +00:00
if (_canvasMarker != null)
2020-12-02 21:23:01 +00:00
{
_canvasMarker.DestroyMarker();
}
2021-06-18 21:38:32 +00:00
2021-03-25 20:56:26 +00:00
if (_markerTarget != null)
{
Destroy(_markerTarget.gameObject);
}
2021-06-18 21:38:32 +00:00
2020-12-02 21:23:01 +00:00
Destroy(this);
}
}
2020-12-03 08:28:05 +00:00
}