115 lines
3.5 KiB
C#
Raw Normal View History

2022-02-26 00:02:14 -08:00
using QSB.AuthoritySync;
2022-02-25 00:21:21 -08:00
using QSB.Syncs.Unsectored.Rigidbodies;
2022-02-25 16:01:36 -08:00
using QSB.Utility;
using QSB.Utility.LinkedWorldObject;
2022-02-25 00:21:21 -08:00
using QSB.WorldSync;
using System;
2022-05-07 18:42:45 +01:00
using UnityEngine;
2022-02-25 00:21:21 -08:00
2022-03-02 19:46:33 -08:00
namespace QSB.EchoesOfTheEye.RaftSync.TransformSync;
public class RaftTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBehaviour
2022-02-25 00:21:21 -08:00
{
private bool IsRidingRaft => Locator.GetPlayerController() && Locator.GetPlayerController().GetGroundBody() == AttachedRigidbody;
protected override bool UseInterpolation => !IsRidingRaft;
2022-02-25 00:21:21 -08:00
2022-05-07 18:42:45 +01:00
private float _lastSetPositionTime;
private const float ForcePositionAfterTime = 1;
private IWorldObject _worldObject;
public void SetWorldObject(IWorldObject worldObject) => _worldObject = worldObject;
2022-02-25 00:21:21 -08:00
protected override OWRigidbody InitAttachedRigidbody() =>
_worldObject.AttachedObject switch
{
RaftController x => x._raftBody,
DreamRaftController x => x._raftBody,
SealRaftController x => x._raftBody,
_ => throw new ArgumentOutOfRangeException(nameof(_worldObject.AttachedObject), _worldObject.AttachedObject, null)
};
2022-02-25 00:21:21 -08:00
2022-03-02 19:46:33 -08:00
public override void OnStartClient()
{
if (QSBCore.IsHost)
2022-02-25 00:21:21 -08:00
{
2022-03-02 19:46:33 -08:00
netIdentity.RegisterAuthQueue();
}
2022-02-25 00:21:21 -08:00
2022-03-02 19:46:33 -08:00
base.OnStartClient();
}
2022-02-25 00:21:21 -08:00
2022-03-02 19:46:33 -08:00
public override void OnStopClient()
{
if (QSBCore.IsHost)
{
netIdentity.UnregisterAuthQueue();
}
2022-02-25 00:21:21 -08:00
2022-03-02 19:46:33 -08:00
base.OnStopClient();
}
2022-02-25 00:21:21 -08:00
2022-03-02 19:46:33 -08:00
protected override void Init()
{
base.Init();
SetReferenceTransform(AttachedRigidbody.GetOrigParent());
2022-02-25 00:21:21 -08:00
2022-03-02 19:46:33 -08:00
AttachedRigidbody.OnUnsuspendOWRigidbody += OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody += OnSuspend;
netIdentity.UpdateAuthQueue(AttachedRigidbody.IsSuspended() ? AuthQueueAction.Remove : AuthQueueAction.Add);
}
2022-02-25 00:21:21 -08:00
2022-03-02 19:46:33 -08:00
protected override void Uninit()
{
base.Uninit();
2022-02-25 00:21:21 -08:00
2022-03-02 19:46:33 -08:00
AttachedRigidbody.OnUnsuspendOWRigidbody -= OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody -= OnSuspend;
}
2022-02-25 16:01:36 -08:00
2022-03-02 19:46:33 -08:00
private void OnUnsuspend(OWRigidbody suspendedBody) => netIdentity.UpdateAuthQueue(AuthQueueAction.Add);
private void OnSuspend(OWRigidbody suspendedBody) => netIdentity.UpdateAuthQueue(AuthQueueAction.Remove);
public override void OnStartAuthority() => DebugLog.DebugWrite($"{this} + AUTH");
public override void OnStopAuthority() => DebugLog.DebugWrite($"{this} - AUTH");
2022-02-25 16:01:36 -08:00
2022-03-02 19:46:33 -08:00
/// <summary>
/// replacement for base method
/// using SetPos/Rot instead of Move
/// </summary>
protected override void ApplyToAttached()
{
2022-08-23 12:26:02 -07:00
var targetPos = ReferenceTransform.FromRelPos(UseInterpolation ? SmoothPosition : transform.position);
var targetRot = ReferenceTransform.FromRelRot(UseInterpolation ? SmoothRotation : transform.rotation);
2022-02-25 16:01:36 -08:00
if (IsRidingRaft)
2022-05-07 18:42:45 +01:00
{
if (Time.unscaledTime >= _lastSetPositionTime + ForcePositionAfterTime)
{
_lastSetPositionTime = Time.unscaledTime;
2022-05-07 18:42:45 +01:00
2022-08-12 16:17:38 -07:00
var playerBody = Locator.GetPlayerBody();
var relPos = AttachedTransform.ToRelPos(playerBody.GetPosition());
var relRot = AttachedTransform.ToRelRot(playerBody.GetRotation());
2022-05-07 18:42:45 +01:00
AttachedRigidbody.SetPosition(targetPos);
AttachedRigidbody.SetRotation(targetRot);
2022-05-07 19:34:10 +01:00
2022-08-12 16:17:38 -07:00
playerBody.SetPosition(AttachedTransform.FromRelPos(relPos));
playerBody.SetRotation(AttachedTransform.FromRelRot(relRot));
}
}
else
{
2022-05-07 18:42:45 +01:00
AttachedRigidbody.SetPosition(targetPos);
AttachedRigidbody.SetRotation(targetRot);
}
2022-02-25 16:01:36 -08:00
2022-03-02 19:46:33 -08:00
var targetVelocity = ReferenceRigidbody.FromRelVel(Velocity, targetPos);
var targetAngularVelocity = ReferenceRigidbody.FromRelAngVel(AngularVelocity);
AttachedRigidbody.SetVelocity(targetVelocity);
AttachedRigidbody.SetAngularVelocity(targetAngularVelocity);
2022-02-25 16:01:36 -08:00
}
2022-03-11 09:43:45 -08:00
}