155 lines
3.7 KiB
C#
Raw Normal View History

2021-07-05 16:13:53 +01:00
using QSB.Player;
using QSB.Utility;
using UnityEngine;
2022-03-02 19:46:33 -08:00
namespace QSB.Tools.ProbeTool;
public class QSBProbe : MonoBehaviour
{
2022-03-02 19:46:33 -08:00
public delegate void SurveyorProbeEvent();
public delegate void RetrieveEvent(float retrieveLength);
public event SurveyorProbeEvent OnLaunchProbe;
public event SurveyorProbeEvent OnAnchorProbe;
public event SurveyorProbeEvent OnUnanchorProbe;
public event SurveyorProbeEvent OnRetrieveProbe;
public event SurveyorProbeEvent OnProbeDestroyed;
public event RetrieveEvent OnStartRetrieveProbe;
private GameObject _detectorObj;
private RulesetDetector _rulesetDetector;
private SingularityWarpEffect _warpEffect;
private bool _isRetrieving;
private PlayerInfo _owner;
private bool _anchored;
public RulesetDetector GetRulesetDetector()
=> _rulesetDetector;
private void Awake()
2020-12-02 21:23:01 +00:00
{
2022-03-02 19:46:33 -08:00
_detectorObj = GetComponentInChildren<RulesetDetector>().gameObject;
_rulesetDetector = _detectorObj.GetComponent<RulesetDetector>();
_warpEffect = GetComponentInChildren<SingularityWarpEffect>();
_warpEffect.OnWarpComplete += OnWarpComplete;
_isRetrieving = false;
}
2021-07-04 22:34:31 +01:00
2022-03-02 19:46:33 -08:00
private void Start() => gameObject.SetActive(false);
2021-07-05 16:13:53 +01:00
2022-03-02 19:46:33 -08:00
protected void OnDestroy() => _warpEffect.OnWarpComplete -= OnWarpComplete;
2021-07-04 22:34:31 +01:00
2022-03-02 19:46:33 -08:00
public void SetOwner(PlayerInfo player)
{
if (_owner != null)
2021-07-05 16:13:53 +01:00
{
2022-03-02 19:46:33 -08:00
DebugLog.ToConsole($"Warning - Trying to set owner of probe that already has an owner!", OWML.Common.MessageType.Warning);
}
2021-07-04 22:34:31 +01:00
2022-03-02 19:46:33 -08:00
_owner = player;
}
2021-07-04 22:34:31 +01:00
2022-03-02 19:46:33 -08:00
private void OnWarpComplete() => Deactivate();
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
public bool IsRetrieving()
=> IsLaunched() && _isRetrieving;
2021-12-29 18:12:07 +00:00
2022-03-02 19:46:33 -08:00
public bool IsLaunched()
=> gameObject.activeSelf;
2021-07-05 16:13:53 +01:00
2022-03-02 19:46:33 -08:00
public bool IsAnchored()
=> IsLaunched() && _anchored;
2021-12-29 18:12:07 +00:00
2022-03-02 19:46:33 -08:00
public void HandleEvent(ProbeEvent probeEvent)
{
if (_owner == null)
{
DebugLog.ToConsole($"Error - Trying to handle event on probe with no owner.", OWML.Common.MessageType.Error);
return;
}
2021-12-29 18:12:07 +00:00
2022-03-02 19:46:33 -08:00
switch (probeEvent)
{
case ProbeEvent.Launch:
_anchored = false;
2021-07-04 16:03:45 +01:00
2022-03-02 19:46:33 -08:00
gameObject.SetActive(true);
transform.position = _owner.ProbeLauncher.transform.position;
transform.rotation = _owner.ProbeLauncher.transform.rotation;
2022-03-02 19:46:33 -08:00
if (OnLaunchProbe == null)
{
DebugLog.ToConsole($"Warning - OnLaunchProbe is null!", OWML.Common.MessageType.Warning);
2021-07-04 16:03:45 +01:00
break;
2022-03-02 19:46:33 -08:00
}
2021-12-29 18:12:07 +00:00
2022-03-02 19:46:33 -08:00
OnLaunchProbe();
break;
case ProbeEvent.Anchor:
_anchored = true;
2021-07-04 16:03:45 +01:00
2022-03-02 19:46:33 -08:00
if (OnAnchorProbe == null)
{
DebugLog.ToConsole($"Warning - OnAnchorProbe is null!", OWML.Common.MessageType.Warning);
2021-07-04 16:03:45 +01:00
break;
2022-03-02 19:46:33 -08:00
}
OnAnchorProbe();
break;
case ProbeEvent.Unanchor:
_anchored = false;
OnUnanchorProbe();
break;
case ProbeEvent.Retrieve:
_anchored = false;
if (OnRetrieveProbe == null)
{
DebugLog.ToConsole($"Warning - OnRetrieveProbe is null!", OWML.Common.MessageType.Warning);
break;
2022-03-02 19:46:33 -08:00
}
2021-12-25 14:29:27 +00:00
2022-03-02 19:46:33 -08:00
OnRetrieveProbe();
break;
case ProbeEvent.Destroy:
_anchored = false;
Destroy(gameObject);
2021-07-04 19:10:50 +01:00
2022-03-02 19:46:33 -08:00
if (OnProbeDestroyed == null)
{
DebugLog.ToConsole($"Warning - OnProbeDestroyed is null!", OWML.Common.MessageType.Warning);
break;
2022-03-02 19:46:33 -08:00
}
2021-07-04 22:34:31 +01:00
2022-03-02 19:46:33 -08:00
OnProbeDestroyed();
break;
case ProbeEvent.Invalid:
default:
DebugLog.ToConsole($"Warning - Unknown/Invalid probe event.", OWML.Common.MessageType.Warning);
break;
}
2022-03-02 19:46:33 -08:00
}
2021-07-04 22:34:31 +01:00
2022-03-02 19:46:33 -08:00
private void Deactivate()
{
transform.localScale = Vector3.one;
gameObject.SetActive(false);
_isRetrieving = false;
}
2022-03-02 19:46:33 -08:00
public void OnStartRetrieve(float duration)
{
if (!_isRetrieving)
{
_isRetrieving = true;
_warpEffect.WarpObjectOut(duration);
2022-03-02 19:46:33 -08:00
if (OnStartRetrieveProbe == null)
{
DebugLog.ToConsole($"Warning - OnStartRetrieveProbe is null!", OWML.Common.MessageType.Warning);
return;
}
2022-03-02 19:46:33 -08:00
OnStartRetrieveProbe(duration);
2020-12-02 21:23:01 +00:00
}
}
2020-12-03 08:28:05 +00:00
}