143 lines
3.6 KiB
C#
Raw Normal View History

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