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

147 lines
3.8 KiB
C#
Raw Normal View History

2021-12-07 15:56:08 +00:00
using QSB.JellyfishSync.WorldObjects;
using QSB.Syncs;
2021-12-01 09:10:38 +00:00
using QSB.Syncs.Unsectored.Rigidbodies;
using QSB.Utility;
2021-12-01 09:10:38 +00:00
using QSB.WorldSync;
using QuantumUNET.Transport;
2021-12-07 15:56:08 +00:00
using System.Collections.Generic;
2021-12-01 09:44:12 +00:00
using UnityEngine;
2021-12-01 09:10:38 +00:00
namespace QSB.JellyfishSync.TransformSync
{
public class JellyfishTransformSync : UnsectoredRigidbodySync
{
public override bool IsReady => WorldObjectManager.AllObjectsAdded;
2021-12-01 09:10:38 +00:00
public override bool UseInterpolation => false;
2021-12-10 22:13:39 +00:00
public override bool IsPlayerObject => false;
2021-12-01 09:10:38 +00:00
private QSBJellyfish _qsbJellyfish;
public static readonly List<JellyfishController> Jellyfish = new();
2021-12-01 09:46:43 +00:00
private static readonly List<JellyfishTransformSync> _instances = new();
2021-12-01 09:10:38 +00:00
protected override OWRigidbody GetRigidbody()
=> _qsbJellyfish.AttachedObject._jellyfishBody;
public override void Start()
{
2021-12-01 09:46:43 +00:00
_instances.Add(this);
2021-12-01 09:10:38 +00:00
base.Start();
}
protected override void OnDestroy()
{
2021-12-01 09:46:43 +00:00
_instances.Remove(this);
2021-12-01 09:10:38 +00:00
base.OnDestroy();
}
2021-12-01 09:44:12 +00:00
public override float GetNetworkSendInterval() => 10;
2021-12-01 09:10:38 +00:00
protected override void Init()
{
_qsbJellyfish = QSBWorldSync.GetWorldFromUnity<QSBJellyfish>(Jellyfish[_instances.IndexOf(this)]);
2021-12-01 09:10:38 +00:00
_qsbJellyfish.TransformSync = this;
base.Init();
SetReferenceTransform(_qsbJellyfish.AttachedObject._planetBody.transform);
}
public override void SerializeTransform(QNetworkWriter writer, bool initialState)
{
base.SerializeTransform(writer, initialState);
if (!WorldObjectManager.AllObjectsReady)
{
writer.Write(false);
return;
}
_qsbJellyfish.Align = true;
writer.Write(_qsbJellyfish.IsRising);
}
2021-12-01 09:10:38 +00:00
private bool _shouldUpdate;
public override void DeserializeTransform(QNetworkReader reader, bool initialState)
{
base.DeserializeTransform(reader, initialState);
if (!WorldObjectManager.AllObjectsReady || HasAuthority)
{
reader.ReadBoolean();
return;
}
_qsbJellyfish.Align = false;
_qsbJellyfish.IsRising = reader.ReadBoolean();
2021-12-01 09:10:38 +00:00
_shouldUpdate = true;
}
/// replacement using SetPosition/Rotation instead of Move
2021-12-01 09:10:38 +00:00
protected override bool UpdateTransform()
{
if (HasAuthority)
{
SetValuesToSync();
return true;
2021-12-01 09:10:38 +00:00
}
if (!_shouldUpdate)
{
return false;
}
_shouldUpdate = false;
var hasMoved = CustomHasMoved(
transform.position,
_localPrevPosition,
transform.rotation,
_localPrevRotation,
_relativeVelocity,
_localPrevVelocity,
_relativeAngularVelocity,
_localPrevAngularVelocity);
_localPrevPosition = transform.position;
_localPrevRotation = transform.rotation;
_localPrevVelocity = _relativeVelocity;
_localPrevAngularVelocity = _relativeAngularVelocity;
if (!hasMoved)
{
return true;
}
2021-12-13 21:26:54 +00:00
var pos = ReferenceTransform.DecodePos(transform.position);
AttachedObject.SetPosition(pos);
AttachedObject.SetRotation(ReferenceTransform.DecodeRot(transform.rotation));
AttachedObject.SetVelocity(ReferenceTransform.GetAttachedOWRigidbody().DecodeVel(_relativeVelocity, pos));
AttachedObject.SetAngularVelocity(ReferenceTransform.GetAttachedOWRigidbody().DecodeAngVel(_relativeAngularVelocity));
return true;
2021-12-01 09:10:38 +00:00
}
2021-12-01 09:44:12 +00:00
protected override void OnRenderObject()
{
if (!WorldObjectManager.AllObjectsReady
2021-12-01 09:44:12 +00:00
|| !QSBCore.ShowLinesInDebug
|| !IsReady
|| ReferenceTransform == null
2021-12-13 21:26:54 +00:00
|| AttachedObject.IsSuspended())
2021-12-01 09:44:12 +00:00
{
return;
}
base.OnRenderObject();
2021-12-01 09:44:12 +00:00
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);
2021-12-01 09:44:12 +00:00
Popcron.Gizmos.Sphere(position + dir * jellyfish._lowerLimit, 10f, Color.magenta);
Popcron.Gizmos.Sphere(position + dir * jellyfish._upperLimit, 10f, Color.magenta);
}
2021-12-01 09:10:38 +00:00
}
}