118 lines
3.2 KiB
C#
Raw Normal View History

2020-09-02 11:17:04 +01:00
using OWML.Common;
using OWML.Utils;
2021-05-15 14:10:51 +01:00
using QSB.SectorSync;
2021-08-14 15:03:01 +01:00
using QSB.Syncs.Sectored.Transforms;
2021-07-26 22:55:09 +01:00
using QSB.Tools.ProbeLauncherTool;
2020-08-23 10:07:43 +02:00
using QSB.Utility;
2021-07-11 16:18:47 +01:00
using QSB.WorldSync;
using UnityEngine;
namespace QSB.Tools.ProbeTool.TransformSync
{
2021-04-28 00:22:08 +01:00
public class PlayerProbeSync : SectoredTransformSync
2020-12-14 21:20:53 +00:00
{
2021-07-11 16:18:47 +01:00
protected override float DistanceLeeway => 10f;
public override bool UseInterpolation => true;
public override bool IgnoreDisabledAttachedObject => true;
2021-12-10 22:13:39 +00:00
public override bool IsPlayerObject => true;
2021-07-11 16:18:47 +01:00
2021-04-20 08:36:07 +01:00
public static PlayerProbeSync LocalInstance { get; private set; }
2021-04-21 16:49:30 +01:00
2021-07-12 22:02:50 +01:00
public override void OnStartAuthority() => LocalInstance = this;
2021-04-20 08:36:07 +01:00
2021-12-13 12:42:27 -08:00
protected override Transform InitLocalTransform()
2020-12-14 21:20:53 +00:00
{
QSBCore.UnityEvents.RunWhen(() => WorldObjectManager.AllObjectsReady, () => SectorSync.Init(Locator.GetProbe().GetSectorDetector(), TargetType.Probe));
2020-12-14 21:41:56 +01:00
2021-07-04 16:03:45 +01:00
var body = Locator.GetProbe().transform;
2020-12-14 21:20:53 +00:00
Player.ProbeBody = body.gameObject;
2020-12-14 21:41:56 +01:00
2021-07-04 16:03:45 +01:00
if (Player.Body == null)
{
DebugLog.ToConsole($"Warning - Player.Body is null!", MessageType.Warning);
return null;
}
var listener = Player.Body.AddComponent<ProbeListener>();
listener.Init(Locator.GetProbe());
2021-07-26 22:55:09 +01:00
var launcherListener = Player.Body.AddComponent<ProbeLauncherListener>();
launcherListener.Init(Player.LocalProbeLauncher);
return body;
2020-12-14 21:20:53 +00:00
}
2020-12-14 21:41:56 +01:00
2021-12-13 12:42:27 -08:00
protected override Transform InitRemoteTransform()
2020-12-14 21:20:53 +00:00
{
2021-07-03 21:51:13 +01:00
var probe = Locator.GetProbe().transform;
2020-12-14 21:41:56 +01:00
2020-12-14 21:20:53 +00:00
if (probe == null)
{
DebugLog.ToConsole("Error - Probe is null!", MessageType.Error);
return default;
}
2020-12-14 21:41:56 +01:00
2021-08-22 16:54:48 +01:00
var body = probe.gameObject.activeSelf
? probe.InstantiateInactive()
2021-07-13 22:20:15 +01:00
: Instantiate(probe);
2020-12-14 21:20:53 +00:00
body.name = "RemoteProbeTransform";
2020-12-14 21:41:56 +01:00
ProbeCreator.CreateProbe(body, Player);
2020-12-14 21:41:56 +01:00
2020-12-14 21:20:53 +00:00
Player.ProbeBody = body.gameObject;
2020-12-14 21:41:56 +01:00
return body;
2020-12-14 21:20:53 +00:00
}
2020-12-14 21:41:56 +01:00
2021-07-11 16:18:47 +01:00
protected override bool UpdateTransform()
{
2021-07-11 16:18:47 +01:00
if (!base.UpdateTransform())
{
2021-07-11 16:18:47 +01:00
return false;
}
if (HasAuthority)
{
if (!AttachedObject.gameObject.activeInHierarchy)
{
var probeOWRigidbody = Locator.GetProbe().GetComponent<SurveyorProbe>().GetOWRigidbody();
if (probeOWRigidbody == null)
{
DebugLog.ToConsole($"Warning - Could not find OWRigidbody of local probe.", MessageType.Warning);
}
var probeLauncher = Player.LocalProbeLauncher;
2021-07-25 00:27:52 +01:00
// TODO : make this sync to the *active* probe launcher's _launcherTransform
var launcherTransform = probeLauncher.GetValue<Transform>("_launcherTransform");
probeOWRigidbody.SetPosition(launcherTransform.position);
probeOWRigidbody.SetRotation(launcherTransform.rotation);
if (ReferenceTransform != null)
{
2021-12-13 16:02:31 -08:00
transform.position = ReferenceTransform.ToRelPos(AttachedObject.position);
transform.rotation = ReferenceTransform.ToRelRot(AttachedObject.rotation);
}
else
{
2021-11-19 00:28:36 -08:00
transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
}
var currentReferenceSector = ReferenceSector;
var playerReferenceSector = Player.TransformSync.ReferenceSector;
if (currentReferenceSector != playerReferenceSector)
{
SetReferenceSector(playerReferenceSector);
}
}
}
2021-07-11 16:18:47 +01:00
return true;
}
2021-07-17 09:51:47 +01:00
public override bool IsReady => Locator.GetProbe() != null;
2020-12-14 21:20:53 +00:00
}
2021-11-19 00:28:36 -08:00
}