2022-01-27 03:32:07 -08:00
|
|
|
|
using Mirror;
|
|
|
|
|
using QSB.AuthoritySync;
|
2021-12-23 17:07:29 -08:00
|
|
|
|
using QSB.JellyfishSync.WorldObjects;
|
2021-12-01 01:10:38 -08:00
|
|
|
|
using QSB.Syncs.Unsectored.Rigidbodies;
|
2021-12-01 03:55:56 -08:00
|
|
|
|
using QSB.Utility;
|
2021-12-01 01:10:38 -08:00
|
|
|
|
using QSB.WorldSync;
|
2021-12-07 15:56:08 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-12-01 01:44:12 -08:00
|
|
|
|
using UnityEngine;
|
2021-12-01 01:10:38 -08:00
|
|
|
|
|
|
|
|
|
namespace QSB.JellyfishSync.TransformSync
|
|
|
|
|
{
|
2022-01-14 22:39:41 -08:00
|
|
|
|
public class JellyfishTransformSync : UnsectoredRigidbodySync
|
2022-01-14 21:25:13 -08:00
|
|
|
|
{
|
2022-01-16 06:11:44 -08:00
|
|
|
|
protected override bool UseInterpolation => false;
|
2022-01-16 06:53:45 -08:00
|
|
|
|
protected override bool OnlyApplyOnDeserialize => true;
|
2022-01-14 21:25:13 -08:00
|
|
|
|
|
|
|
|
|
private QSBJellyfish _qsbJellyfish;
|
2022-01-27 03:32:07 -08:00
|
|
|
|
private bool _isRising;
|
2022-01-14 22:28:44 -08:00
|
|
|
|
private static readonly List<JellyfishTransformSync> _instances = new();
|
2022-01-14 21:25:13 -08:00
|
|
|
|
|
2022-01-16 04:51:37 -08:00
|
|
|
|
protected override OWRigidbody InitAttachedRigidbody()
|
2022-01-14 21:25:13 -08:00
|
|
|
|
=> _qsbJellyfish.AttachedObject._jellyfishBody;
|
|
|
|
|
|
2022-01-18 12:19:58 -08:00
|
|
|
|
public override void OnStartClient()
|
2022-01-14 21:25:13 -08:00
|
|
|
|
{
|
|
|
|
|
_instances.Add(this);
|
2022-01-18 12:19:58 -08:00
|
|
|
|
base.OnStartClient();
|
2022-01-14 21:25:13 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 15:06:45 -08:00
|
|
|
|
public override void OnStopClient()
|
2022-01-14 21:25:13 -08:00
|
|
|
|
{
|
|
|
|
|
_instances.Remove(this);
|
2022-01-18 15:06:45 -08:00
|
|
|
|
base.OnStopClient();
|
2022-01-14 21:25:13 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override float SendInterval => 10;
|
|
|
|
|
protected override bool UseReliableRpc => true;
|
|
|
|
|
|
|
|
|
|
protected override void Init()
|
|
|
|
|
{
|
|
|
|
|
_qsbJellyfish = JellyfishManager.Jellyfish[_instances.IndexOf(this)].GetWorldObject<QSBJellyfish>();
|
2022-01-14 22:07:32 -08:00
|
|
|
|
_qsbJellyfish.TransformSync = this;
|
2022-01-14 21:25:13 -08:00
|
|
|
|
|
|
|
|
|
base.Init();
|
|
|
|
|
SetReferenceTransform(_qsbJellyfish.AttachedObject._planetBody.transform);
|
|
|
|
|
|
|
|
|
|
if (QSBCore.IsHost)
|
|
|
|
|
{
|
|
|
|
|
netIdentity.RegisterAuthQueue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AttachedRigidbody.OnUnsuspendOWRigidbody += OnUnsuspend;
|
|
|
|
|
AttachedRigidbody.OnSuspendOWRigidbody += OnSuspend;
|
|
|
|
|
netIdentity.SendAuthQueueMessage(AttachedRigidbody.IsSuspended() ? AuthQueueAction.Remove : AuthQueueAction.Add);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 17:51:26 -08:00
|
|
|
|
protected override void Uninit()
|
2022-01-14 21:25:13 -08:00
|
|
|
|
{
|
2022-02-02 22:41:59 -08:00
|
|
|
|
base.Uninit();
|
|
|
|
|
|
2022-01-21 17:51:26 -08:00
|
|
|
|
if (QSBCore.IsHost)
|
|
|
|
|
{
|
|
|
|
|
netIdentity.UnregisterAuthQueue();
|
|
|
|
|
}
|
2022-01-14 21:25:13 -08:00
|
|
|
|
|
2022-01-21 17:51:26 -08:00
|
|
|
|
AttachedRigidbody.OnUnsuspendOWRigidbody -= OnUnsuspend;
|
|
|
|
|
AttachedRigidbody.OnSuspendOWRigidbody -= OnSuspend;
|
2022-01-16 07:23:35 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-14 21:25:13 -08:00
|
|
|
|
private void OnUnsuspend(OWRigidbody suspendedBody) => netIdentity.SendAuthQueueMessage(AuthQueueAction.Add);
|
|
|
|
|
private void OnSuspend(OWRigidbody suspendedBody) => netIdentity.SendAuthQueueMessage(AuthQueueAction.Remove);
|
|
|
|
|
|
2022-01-27 03:32:07 -08:00
|
|
|
|
protected override void Serialize(NetworkWriter writer)
|
|
|
|
|
{
|
|
|
|
|
base.Serialize(writer);
|
|
|
|
|
writer.Write(_isRising);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Deserialize(NetworkReader reader)
|
|
|
|
|
{
|
|
|
|
|
base.Deserialize(reader);
|
|
|
|
|
_isRising = reader.Read<bool>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void GetFromAttached()
|
|
|
|
|
{
|
|
|
|
|
base.GetFromAttached();
|
|
|
|
|
|
|
|
|
|
_isRising = _qsbJellyfish.AttachedObject._isRising;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-14 21:25:13 -08:00
|
|
|
|
/// replacement using SetPosition/Rotation instead of Move
|
2022-01-16 06:53:45 -08:00
|
|
|
|
protected override void ApplyToAttached()
|
2022-01-14 21:25:13 -08:00
|
|
|
|
{
|
|
|
|
|
var pos = ReferenceTransform.FromRelPos(transform.position);
|
|
|
|
|
AttachedRigidbody.SetPosition(pos);
|
|
|
|
|
AttachedRigidbody.SetRotation(ReferenceTransform.FromRelRot(transform.rotation));
|
|
|
|
|
AttachedRigidbody.SetVelocity(ReferenceTransform.GetAttachedOWRigidbody().FromRelVel(_relativeVelocity, pos));
|
|
|
|
|
AttachedRigidbody.SetAngularVelocity(ReferenceTransform.GetAttachedOWRigidbody().FromRelAngVel(_relativeAngularVelocity));
|
2022-01-27 03:32:07 -08:00
|
|
|
|
|
|
|
|
|
_qsbJellyfish.SetIsRising(_isRising);
|
2022-01-14 21:25:13 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnRenderObject()
|
|
|
|
|
{
|
2022-01-20 22:33:03 +00:00
|
|
|
|
if (!QSBCore.DebugSettings.DrawLines
|
2022-01-21 17:24:22 -08:00
|
|
|
|
|| !IsValid
|
2022-01-27 02:15:42 -08:00
|
|
|
|
|| !ReferenceTransform)
|
2022-01-14 21:25:13 -08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.OnRenderObject();
|
|
|
|
|
|
|
|
|
|
var jellyfish = _qsbJellyfish.AttachedObject;
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-01 01:10:38 -08:00
|
|
|
|
}
|