quantum-space-buddies/QSB/OrbSync/TransformSync/NomaiOrbTransformSync.cs

98 lines
2.4 KiB
C#
Raw Normal View History

2021-10-20 16:29:49 +00:00
using OWML.Common;
using QSB.Syncs.Unsectored.Transforms;
2021-04-28 09:02:16 +00:00
using QSB.Utility;
using QSB.WorldSync;
using System.Collections.Generic;
using UnityEngine;
namespace QSB.OrbSync.TransformSync
{
2021-08-14 14:03:01 +00:00
internal class NomaiOrbTransformSync : UnsectoredTransformSync
2021-04-28 09:02:16 +00:00
{
2021-12-14 05:18:16 +00:00
public static readonly List<NomaiOrbTransformSync> Instances = new();
2021-04-28 09:02:16 +00:00
2021-12-10 22:13:39 +00:00
public override bool IsPlayerObject => false;
2021-12-14 05:42:20 +00:00
public NomaiInterfaceOrb Orb;
2021-12-14 05:18:16 +00:00
private int _index => Instances.IndexOf(this);
2021-04-28 09:02:16 +00:00
2021-12-14 05:18:16 +00:00
public override void OnStartClient() => Instances.Add(this);
2021-04-28 09:02:16 +00:00
protected override void OnDestroy()
{
2021-12-14 05:18:16 +00:00
Instances.Remove(this);
2021-08-19 16:34:21 +00:00
base.OnDestroy();
2021-04-28 09:02:16 +00:00
}
protected override void Init()
{
2021-12-14 05:18:16 +00:00
if (!Instances.Contains(this))
2021-10-25 15:31:15 +00:00
{
2021-12-14 05:18:16 +00:00
Instances.Add(this);
2021-10-25 15:31:15 +00:00
}
2021-04-28 09:02:16 +00:00
base.Init();
2021-10-24 09:47:25 +00:00
if (AttachedObject == null)
{
DebugLog.ToConsole($"Error - Trying to init orb with null AttachedObject.", MessageType.Error);
2021-10-25 15:31:15 +00:00
return;
2021-10-24 09:47:25 +00:00
}
2021-07-09 14:54:16 +00:00
var originalParent = AttachedObject.GetAttachedOWRigidbody().GetOrigParent();
if (originalParent == Locator.GetRootTransform())
{
2021-11-11 05:51:14 +00:00
DebugLog.DebugWrite($"{LogName} with AttachedObject {AttachedObject.name} had it's original parent as SolarSystemRoot - Disabling...");
2021-10-20 16:29:49 +00:00
enabled = false;
2021-12-14 05:18:16 +00:00
Instances[_index] = null;
2021-07-09 14:54:16 +00:00
}
SetReferenceTransform(originalParent);
2021-12-14 05:42:20 +00:00
Orb = AttachedObject.GetRequiredComponent<NomaiInterfaceOrb>();
2021-04-28 09:02:16 +00:00
}
private Transform GetTransform()
2021-05-08 11:53:09 +00:00
{
2021-05-18 14:22:21 +00:00
if (_index == -1)
{
2021-12-14 05:18:16 +00:00
DebugLog.ToConsole($"Error - Index cannot be found. OrbTransformSyncs count : {Instances.Count}", MessageType.Error);
2021-05-18 14:22:21 +00:00
return null;
}
2021-06-18 21:38:32 +00:00
2021-12-14 05:18:16 +00:00
if (OrbManager.Orbs.Count <= _index)
2021-05-08 11:53:09 +00:00
{
2021-12-14 05:18:16 +00:00
DebugLog.ToConsole($"Error - OldOrbList does not contain index {_index}.", MessageType.Error);
2021-05-08 11:53:09 +00:00
return null;
}
2021-06-18 21:38:32 +00:00
2021-12-14 05:18:16 +00:00
if (OrbManager.Orbs[_index] == null)
2021-05-08 15:57:01 +00:00
{
2021-10-20 16:29:49 +00:00
DebugLog.ToConsole($"Error - OldOrbList index {_index} is null.", MessageType.Error);
2021-05-08 15:57:01 +00:00
return null;
}
2021-06-18 21:38:32 +00:00
2021-12-14 05:18:16 +00:00
return OrbManager.Orbs[_index].transform;
2021-05-08 11:53:09 +00:00
}
2021-12-14 05:42:20 +00:00
protected override bool UpdateTransform()
{
2021-12-14 05:52:59 +00:00
if (HasAuthority)
2021-12-14 05:42:20 +00:00
{
2021-12-14 05:52:59 +00:00
transform.position = ReferenceTransform.ToRelPos(AttachedObject.position);
2021-12-14 05:42:20 +00:00
}
2021-12-14 05:52:59 +00:00
else
2021-12-14 05:42:20 +00:00
{
2021-12-14 05:52:59 +00:00
Orb.SetTargetPosition(ReferenceTransform.FromRelPos(transform.position));
2021-12-14 05:42:20 +00:00
}
return true;
}
2021-12-13 20:42:27 +00:00
protected override Transform InitLocalTransform() => GetTransform();
protected override Transform InitRemoteTransform() => GetTransform();
2021-04-28 09:02:16 +00:00
public override bool IsReady => WorldObjectManager.AllObjectsReady;
2021-12-14 05:52:59 +00:00
public override bool UseInterpolation => false;
2021-04-28 09:02:16 +00:00
}
}