64 lines
1.6 KiB
C#
Raw Normal View History

2021-11-10 09:18:57 +00:00
using QSB.Anglerfish.TransformSync;
using QSB.Events;
2021-11-09 17:56:45 -08:00
using QSB.Utility;
using QSB.WorldSync;
using QuantumUNET;
using UnityEngine;
namespace QSB.Anglerfish.WorldObjects
{
public class QSBAngler : WorldObject<AnglerfishController>
{
2021-11-10 09:28:05 +00:00
public AnglerTransformSync TransformSync;
public Transform TargetTransform;
public Vector3 TargetVelocity { get; private set; }
private Vector3 _lastTargetPosition;
2021-11-09 17:56:45 -08:00
public override void Init(AnglerfishController attachedObject, int id)
{
ObjectId = id;
AttachedObject = attachedObject;
QSBCore.UnityEvents.RunWhen(() => TransformSync, () =>
{
AttachedObject.OnAnglerSuspended += OnSuspend;
AttachedObject.OnAnglerUnsuspended += OnUnsuspend;
});
2021-11-09 17:56:45 -08:00
if (QSBCore.IsHost)
{
Object.Instantiate(QSBNetworkManager.Instance.AnglerPrefab).SpawnWithServerAuthority();
}
}
public override void OnRemoval()
{
AttachedObject.OnAnglerSuspended -= OnSuspend;
AttachedObject.OnAnglerUnsuspended -= OnUnsuspend;
2021-11-09 17:56:45 -08:00
if (QSBCore.IsHost)
{
2021-11-10 09:28:05 +00:00
QNetworkServer.Destroy(TransformSync.gameObject);
2021-11-09 17:56:45 -08:00
}
2021-11-09 18:38:19 -08:00
}
private void OnSuspend(AnglerfishController.AnglerState _) =>
QSBEventManager.FireEvent(EventNames.QSBSuspensionChange, TransformSync.NetIdentity, true);
2021-11-09 18:38:19 -08:00
private void OnUnsuspend(AnglerfishController.AnglerState _) =>
QSBEventManager.FireEvent(EventNames.QSBSuspensionChange, TransformSync.NetIdentity, false);
2021-11-09 20:35:55 -08:00
public void UpdateTargetVelocity()
2021-11-09 20:35:55 -08:00
{
2021-11-10 09:28:05 +00:00
if (TargetTransform == null)
{
return;
}
2021-11-25 15:38:05 +00:00
2021-11-10 09:28:05 +00:00
TargetVelocity = TargetTransform.position - _lastTargetPosition;
_lastTargetPosition = TargetTransform.position;
2021-11-09 20:35:55 -08:00
}
2021-11-09 17:56:45 -08:00
}
}