quantum-space-buddies/QSB/JellyfishSync/TransformSync/JellyfishTransformSync.cs

109 lines
3.2 KiB
C#
Raw Normal View History

using Mirror;
using QSB.AuthoritySync;
2021-12-24 01:07:29 +00:00
using QSB.JellyfishSync.WorldObjects;
2021-12-01 09:10:38 +00:00
using QSB.Syncs.Unsectored.Rigidbodies;
using QSB.Utility;
using QSB.Utility.LinkedWorldObject;
2021-12-01 09:10:38 +00:00
using QSB.WorldSync;
2021-12-01 09:44:12 +00:00
using UnityEngine;
2021-12-01 09:10:38 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.JellyfishSync.TransformSync;
public class JellyfishTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBehaviour
2021-12-01 09:10:38 +00:00
{
2022-03-03 03:46:33 +00:00
protected override bool UseInterpolation => false;
protected override bool AllowInactiveAttachedObject => true; // since they deactivate when suspended
2022-01-15 05:25:13 +00:00
private QSBJellyfish _qsbJellyfish;
public void SetWorldObject(IWorldObject worldObject) => _qsbJellyfish = (QSBJellyfish)worldObject;
2022-01-15 05:25:13 +00:00
2022-03-03 03:46:33 +00:00
protected override OWRigidbody InitAttachedRigidbody()
=> _qsbJellyfish.AttachedObject._jellyfishBody;
2022-01-15 05:25:13 +00:00
2022-03-03 03:46:33 +00:00
public override void OnStartClient()
{
if (QSBCore.IsHost)
2022-01-15 05:25:13 +00:00
{
2022-03-03 03:46:33 +00:00
netIdentity.RegisterAuthQueue();
}
2022-03-03 03:46:33 +00:00
base.OnStartClient();
}
2022-01-15 05:25:13 +00:00
2022-03-03 03:46:33 +00:00
public override void OnStopClient()
{
if (QSBCore.IsHost)
{
netIdentity.UnregisterAuthQueue();
}
2022-01-15 05:25:13 +00:00
2022-03-03 03:46:33 +00:00
base.OnStopClient();
}
2022-01-15 05:25:13 +00:00
2022-03-03 03:46:33 +00:00
protected override float SendInterval => 10;
protected override bool UseReliableRpc => true;
2022-01-15 05:25:13 +00:00
2022-03-03 03:46:33 +00:00
protected override void Init()
{
base.Init();
SetReferenceTransform(_qsbJellyfish.AttachedObject._planetBody.transform);
2022-03-03 03:46:33 +00:00
AttachedRigidbody.OnUnsuspendOWRigidbody += OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody += OnSuspend;
netIdentity.UpdateAuthQueue(AttachedRigidbody.IsSuspended() ? AuthQueueAction.Remove : AuthQueueAction.Add);
}
2022-03-03 03:46:33 +00:00
protected override void Uninit()
{
base.Uninit();
2022-01-15 05:25:13 +00:00
2022-03-03 03:46:33 +00:00
AttachedRigidbody.OnUnsuspendOWRigidbody -= OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody -= OnSuspend;
}
2022-03-03 03:46:33 +00:00
private void OnUnsuspend(OWRigidbody suspendedBody) => netIdentity.UpdateAuthQueue(AuthQueueAction.Add);
private void OnSuspend(OWRigidbody suspendedBody) => netIdentity.UpdateAuthQueue(AuthQueueAction.Remove);
2022-03-03 03:46:33 +00:00
protected override void Serialize(NetworkWriter writer)
{
writer.Write(_qsbJellyfish.AttachedObject._isRising);
2022-03-03 03:46:33 +00:00
base.Serialize(writer);
}
2022-03-03 03:46:33 +00:00
protected override void Deserialize(NetworkReader reader)
{
_qsbJellyfish.SetIsRising(reader.Read<bool>());
2022-03-03 03:46:33 +00:00
base.Deserialize(reader);
}
2022-03-03 03:46:33 +00:00
/// replacement using SetPosition/Rotation instead of Move
protected override void ApplyToAttached()
{
var pos = ReferenceTransform.FromRelPos(transform.position);
AttachedRigidbody.SetPosition(pos);
AttachedRigidbody.SetRotation(ReferenceTransform.FromRelRot(transform.rotation));
AttachedRigidbody.SetVelocity(ReferenceRigidbody.FromRelVel(Velocity, pos));
AttachedRigidbody.SetAngularVelocity(ReferenceRigidbody.FromRelAngVel(AngularVelocity));
}
protected override void OnRenderObject()
{
if (!QSBCore.DebugSettings.DrawLines
|| !IsValid
|| !ReferenceTransform
|| !AttachedTransform.gameObject.activeInHierarchy)
2022-01-15 05:25:13 +00:00
{
2022-03-03 03:46:33 +00:00
return;
2022-01-15 05:25:13 +00:00
}
2022-03-03 03:46:33 +00:00
base.OnRenderObject();
var jellyfish = _qsbJellyfish.AttachedObject;
2022-03-03 03:46:33 +00:00
var position = ReferenceTransform.position;
var dir = Vector3.Normalize(jellyfish.transform.position - position);
// Popcron.Gizmos.Line(position + dir * jellyfish._lowerLimit, position + dir * jellyfish._upperLimit, Color.magenta);
Popcron.Gizmos.Sphere(position + dir * jellyfish._lowerLimit, 10f, Color.magenta);
Popcron.Gizmos.Sphere(position + dir * jellyfish._upperLimit, 10f, Color.magenta);
2022-01-15 05:25:13 +00:00
}
}