130 lines
4.4 KiB
C#
Raw Normal View History

2022-03-04 03:00:44 -08:00
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;
using QSB.Syncs;
2021-08-14 15:03:01 +01:00
using QSB.Syncs.Sectored.Transforms;
2021-07-06 22:29:47 +01:00
using QSB.Utility;
using System.Linq;
2020-02-21 23:36:07 +01:00
using UnityEngine;
using Gizmos = Popcron.Gizmos;
2020-02-10 23:03:28 +01:00
2022-03-02 19:46:33 -08:00
namespace QSB.Player.TransformSync;
2022-01-14 21:34:09 -08:00
2022-08-27 11:44:52 -07:00
[UsedInUnityProject]
2022-03-02 19:46:33 -08:00
public class PlayerTransformSync : SectoredTransformSync
{
protected override bool IsPlayerObject => true;
protected override bool AllowInactiveAttachedObject => true;
2022-01-14 21:34:09 -08:00
2022-03-02 19:46:33 -08:00
private Transform _visibleCameraRoot;
private Transform _networkCameraRoot => gameObject.transform.GetChild(0);
2022-01-14 21:34:09 -08:00
2022-03-02 19:46:33 -08:00
private Transform _visibleRoastingSystem;
private Transform _networkRoastingSystem => gameObject.transform.GetChild(1);
2022-01-14 21:34:09 -08:00
2022-03-02 19:46:33 -08:00
private Transform _visibleStickPivot;
private Transform _networkStickPivot => _networkRoastingSystem.GetChild(0).GetChild(0);
2022-01-14 21:34:09 -08:00
2022-03-02 19:46:33 -08:00
private Transform _visibleStickTip;
private Transform _networkStickTip => _networkStickPivot.GetChild(0);
2022-01-14 21:34:09 -08:00
2022-03-02 19:46:33 -08:00
public override void OnStartClient()
{
var player = new PlayerInfo(this);
QSBPlayerManager.PlayerList.SafeAdd(player);
base.OnStartClient();
QSBPlayerManager.OnAddPlayer?.Invoke(Player);
2022-03-06 01:01:02 -08:00
DebugLog.DebugWrite($"Create Player : {Player}", MessageType.Info);
2022-01-14 21:34:09 -08:00
2022-03-02 19:46:33 -08:00
JoinLeaveSingularity.Create(Player, true);
}
2022-01-14 21:34:09 -08:00
public override void OnStartLocalPlayer() => LocalInstance = this;
2022-01-14 21:34:09 -08:00
2022-03-02 19:46:33 -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);
2022-03-06 01:01:02 -08:00
DebugLog.DebugWrite($"Remove Player : {Player}", MessageType.Info);
2022-03-02 19:46:33 -08:00
}
2022-01-16 06:53:45 -08:00
2022-03-02 19:46:33 -08:00
protected override void Uninit()
{
base.Uninit();
2022-01-14 21:34:09 -08:00
2022-03-02 19:46:33 -08:00
if (isLocalPlayer)
2022-02-27 06:28:59 -08:00
{
2022-03-02 19:46:33 -08:00
Player.IsReady = false;
new PlayerReadyMessage(false).Send();
2022-02-27 06:28:59 -08:00
}
Player.Reset();
2022-03-02 19:46:33 -08:00
}
protected override void Init()
{
base.Init();
var comps = GetComponents<QSBNetworkTransformChild>();
comps.First(x => x.Target == _networkCameraRoot).AttachedTransform = _visibleCameraRoot;
comps.First(x => x.Target == _networkRoastingSystem).AttachedTransform = _visibleRoastingSystem;
comps.First(x => x.Target == _networkStickPivot).AttachedTransform = _visibleStickPivot;
comps.First(x => x.Target == _networkStickTip).AttachedTransform = _visibleStickTip;
}
2022-02-27 06:28:59 -08:00
2022-03-02 19:46:33 -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 OnRenderObject()
{
if (!QSBCore.DebugSettings.DrawLines
|| !IsValid
|| !ReferenceTransform)
{
2022-03-02 19:46:33 -08:00
return;
}
2022-03-02 19:46:33 -08:00
base.OnRenderObject();
2022-03-02 19:46:33 -08:00
Gizmos.Cube(ReferenceTransform.TransformPoint(_networkRoastingSystem.position), ReferenceTransform.TransformRotation(_networkRoastingSystem.rotation), Vector3.one / 4, Color.red);
Gizmos.Cube(ReferenceTransform.TransformPoint(_networkStickPivot.position), ReferenceTransform.TransformRotation(_networkStickPivot.rotation), Vector3.one / 4, Color.red);
Gizmos.Cube(ReferenceTransform.TransformPoint(_networkStickTip.position), ReferenceTransform.TransformRotation(_networkStickTip.rotation), Vector3.one / 4, Color.red);
Gizmos.Cube(ReferenceTransform.TransformPoint(_networkCameraRoot.position), ReferenceTransform.TransformRotation(_networkCameraRoot.rotation), Vector3.one / 4, Color.red);
2022-03-02 19:46:33 -08:00
Gizmos.Cube(_visibleRoastingSystem.position, _visibleRoastingSystem.rotation, Vector3.one / 4, Color.magenta);
Gizmos.Cube(_visibleStickPivot.position, _visibleStickPivot.rotation, Vector3.one / 4, Color.blue);
Gizmos.Cube(_visibleStickTip.position, _visibleStickTip.rotation, Vector3.one / 4, Color.yellow);
Gizmos.Cube(_visibleCameraRoot.position, _visibleCameraRoot.rotation, Vector3.one / 4, Color.grey);
}
2022-03-02 19:46:33 -08:00
protected override bool CheckReady() =>
base.CheckReady() &&
(Locator.GetPlayerTransform() || AttachedTransform);
protected override bool CheckValid() => base.CheckValid() && !Player.IsDead;
public static PlayerTransformSync LocalInstance { get; private set; }
protected override bool UseInterpolation => true;
}