161 lines
5.9 KiB
C#
Raw Normal View History

using OWML.Common;
2021-12-23 17:07:29 -08:00
using QSB.Messaging;
using QSB.Player.Messages;
using QSB.PlayerBodySetup.Local;
using QSB.PlayerBodySetup.Remote;
2021-08-14 15:03:01 +01:00
using QSB.Syncs.Sectored.Transforms;
2021-07-06 22:29:47 +01:00
using QSB.Utility;
2020-02-21 23:36:07 +01:00
using UnityEngine;
2020-02-10 23:03:28 +01:00
namespace QSB.Player.TransformSync;
2022-01-14 21:34:09 -08:00
public class PlayerTransformSync : SectoredTransformSync
{
protected override bool IsPlayerObject => true;
protected override bool AllowInactiveAttachedObject => true;
2022-01-14 21:34:09 -08:00
private Transform _visibleCameraRoot;
private Transform _networkCameraRoot => gameObject.transform.GetChild(0);
2022-01-14 21:34:09 -08:00
// todo? stick root might be the thing that moves instead of roasting system. one of them doesn't, i just don't know which
private Transform _visibleRoastingSystem;
private Transform _networkRoastingSystem => gameObject.transform.GetChild(1);
private Transform _networkStickRoot => _networkRoastingSystem.GetChild(0);
2022-01-14 21:34:09 -08:00
private Transform _visibleStickPivot;
private Transform _networkStickPivot => _networkStickRoot.GetChild(0);
2022-01-14 21:34:09 -08:00
private Transform _visibleStickTip;
private Transform _networkStickTip => _networkStickPivot.GetChild(0);
2022-01-14 21:34:09 -08:00
protected Vector3 _cameraPositionVelocity;
protected Quaternion _cameraRotationVelocity;
protected Vector3 _pivotPositionVelocity;
protected Quaternion _pivotRotationVelocity;
protected Vector3 _tipPositionVelocity;
protected Quaternion _tipRotationVelocity;
protected Vector3 _roastingPositionVelocity;
protected Quaternion _roastingRotationVelocity;
2022-02-16 23:45:54 -08:00
public override void OnStartClient()
{
var player = new PlayerInfo(this);
QSBPlayerManager.PlayerList.SafeAdd(player);
base.OnStartClient();
QSBPlayerManager.OnAddPlayer?.Invoke(Player);
DebugLog.DebugWrite($"Create Player : id<{Player.PlayerId}>", MessageType.Info);
2022-01-14 21:34:09 -08:00
JoinLeaveSingularity.Create(Player, true);
}
2022-01-18 12:19:58 -08:00
public override void OnStartLocalPlayer() => LocalInstance = this;
2022-01-14 21:34:09 -08:00
public override void OnStopClient()
{
JoinLeaveSingularity.Create(Player, false);
// TODO : Maybe move this to a leave event...? Would ensure everything could finish up before removing the player
QSBPlayerManager.OnRemovePlayer?.Invoke(Player);
base.OnStopClient();
Player.HudMarker?.Remove();
QSBPlayerManager.PlayerList.Remove(Player);
DebugLog.DebugWrite($"Remove Player : id<{Player.PlayerId}>", MessageType.Info);
}
2022-01-14 21:34:09 -08:00
protected override void Uninit()
{
base.Uninit();
2022-01-14 21:34:09 -08:00
if (isLocalPlayer)
2022-01-14 21:34:09 -08:00
{
Player.IsReady = false;
new PlayerReadyMessage(false).Send();
2022-01-14 21:34:09 -08:00
}
}
2022-01-14 21:34:09 -08:00
protected override Transform InitLocalTransform()
=> LocalPlayerCreation.CreatePlayer(
Player,
SectorDetector,
out _visibleCameraRoot,
out _visibleRoastingSystem,
out _visibleStickPivot,
out _visibleStickTip);
protected override Transform InitRemoteTransform()
=> RemotePlayerCreation.CreatePlayer(
Player,
out _visibleCameraRoot,
out _visibleRoastingSystem,
out _visibleStickPivot,
out _visibleStickTip);
protected override void GetFromAttached()
{
base.GetFromAttached();
if (!ReferenceTransform)
2022-01-14 21:34:09 -08:00
{
return;
2022-01-16 06:53:45 -08:00
}
GetFromChild(_visibleStickPivot, _networkStickPivot);
GetFromChild(_visibleStickTip, _networkStickTip);
GetFromChild(_visibleCameraRoot, _networkCameraRoot);
GetFromChild(_visibleRoastingSystem, _networkRoastingSystem);
}
2022-01-14 21:34:09 -08:00
protected override void ApplyToAttached()
{
base.ApplyToAttached();
if (!ReferenceTransform)
2022-01-16 06:53:45 -08:00
{
return;
2022-01-14 21:34:09 -08:00
}
ApplyToChild(_visibleStickPivot, _networkStickPivot, ref _pivotPositionVelocity, ref _pivotRotationVelocity);
ApplyToChild(_visibleStickTip, _networkStickTip, ref _tipPositionVelocity, ref _tipRotationVelocity);
ApplyToChild(_visibleCameraRoot, _networkCameraRoot, ref _cameraPositionVelocity, ref _cameraRotationVelocity);
ApplyToChild(_visibleRoastingSystem, _networkRoastingSystem, ref _roastingPositionVelocity, ref _roastingRotationVelocity);
}
private static void GetFromChild(Transform visible, Transform network)
{
network.localPosition = visible.localPosition;
network.localRotation = visible.localRotation;
}
private static void ApplyToChild(Transform visible, Transform network, ref Vector3 positionVelocity, ref Quaternion rotationVelocity)
{
visible.localPosition = Vector3.SmoothDamp(visible.localPosition, network.localPosition, ref positionVelocity, SmoothTime);
visible.localRotation = QuaternionHelper.SmoothDamp(visible.localRotation, network.localRotation, ref rotationVelocity, SmoothTime);
}
protected override void OnRenderObject()
{
if (!QSBCore.DebugSettings.DrawLines
|| !IsValid
|| !ReferenceTransform)
2022-01-14 21:34:09 -08:00
{
return;
2022-01-14 21:34:09 -08:00
}
base.OnRenderObject();
2022-01-14 21:34:09 -08:00
Popcron.Gizmos.Cube(ReferenceTransform.TransformPoint(_networkRoastingSystem.position), ReferenceTransform.TransformRotation(_networkRoastingSystem.rotation), Vector3.one / 4, Color.red);
Popcron.Gizmos.Cube(ReferenceTransform.TransformPoint(_networkStickPivot.position), ReferenceTransform.TransformRotation(_networkStickPivot.rotation), Vector3.one / 4, Color.red);
Popcron.Gizmos.Cube(ReferenceTransform.TransformPoint(_networkStickTip.position), ReferenceTransform.TransformRotation(_networkStickTip.rotation), Vector3.one / 4, Color.red);
Popcron.Gizmos.Cube(ReferenceTransform.TransformPoint(_networkCameraRoot.position), ReferenceTransform.TransformRotation(_networkCameraRoot.rotation), Vector3.one / 4, Color.red);
2021-05-15 14:10:51 +01:00
Popcron.Gizmos.Cube(_visibleRoastingSystem.position, _visibleRoastingSystem.rotation, Vector3.one / 4, Color.magenta);
Popcron.Gizmos.Cube(_visibleStickPivot.position, _visibleStickPivot.rotation, Vector3.one / 4, Color.blue);
Popcron.Gizmos.Cube(_visibleStickTip.position, _visibleStickTip.rotation, Vector3.one / 4, Color.yellow);
Popcron.Gizmos.Cube(_visibleCameraRoot.position, _visibleCameraRoot.rotation, Vector3.one / 4, Color.grey);
2020-12-14 21:20:53 +00:00
}
protected override bool CheckReady() => base.CheckReady()
&& (Locator.GetPlayerTransform() || AttachedTransform);
public static PlayerTransformSync LocalInstance { get; private set; }
protected override bool UseInterpolation => true;
}