255 lines
8.8 KiB
C#
Raw Normal View History

2021-07-06 22:29:47 +01:00
using OWML.Utils;
using QSB.Animation.Player;
using QSB.Events;
2020-11-20 19:50:31 +00:00
using QSB.Instruments;
2021-07-06 22:29:47 +01:00
using QSB.RoastingSync;
2021-05-15 14:10:51 +01:00
using QSB.SectorSync;
2021-05-02 13:59:39 +01:00
using QSB.Syncs.TransformSync;
2021-07-06 22:29:47 +01:00
using QSB.Tools;
using QSB.Utility;
2021-07-11 16:18:47 +01:00
using QSB.WorldSync;
2021-07-06 22:29:47 +01:00
using System.Linq;
2020-02-21 23:36:07 +01:00
using UnityEngine;
2020-02-10 23:03:28 +01:00
2021-04-11 17:05:02 +01:00
namespace QSB.Player.TransformSync
2020-02-15 20:48:02 +01:00
{
2021-04-28 00:22:08 +01:00
public class PlayerTransformSync : SectoredTransformSync
2020-12-14 21:20:53 +00:00
{
2020-12-24 16:34:24 +00:00
static PlayerTransformSync() => AnimControllerPatch.Init();
2021-07-06 22:29:47 +01:00
private Transform _visibleCameraRoot;
private Transform _networkCameraRoot => gameObject.transform.GetChild(0);
2021-07-07 19:45:14 +01:00
private Transform _networkRoastingSystem => gameObject.transform.GetChild(1);
private Transform _networkStickRoot => _networkRoastingSystem.GetChild(0);
2021-07-06 22:29:47 +01:00
private Transform _visibleStickPivot;
2021-07-07 19:45:14 +01:00
private Transform _networkStickPivot => _networkStickRoot.GetChild(0);
2021-07-06 22:29:47 +01:00
private Transform _visibleStickTip;
private Transform _networkStickTip => _networkStickPivot.GetChild(0);
2021-07-07 21:14:57 +01:00
protected Vector3 _cameraPositionVelocity;
protected Quaternion _cameraRotationVelocity;
protected Vector3 _pivotPositionVelocity;
protected Quaternion _pivotRotationVelocity;
protected Vector3 _tipPositionVelocity;
protected Quaternion _tipRotationVelocity;
2021-07-06 22:29:47 +01:00
private Transform GetStickPivot()
=> Resources.FindObjectsOfTypeAll<RoastingStickController>().First().transform.Find("Stick_Root/Stick_Pivot");
2021-02-16 12:15:43 +00:00
public override void OnStartLocalPlayer()
2021-02-15 10:58:21 +00:00
=> LocalInstance = this;
2020-08-19 22:29:53 +02:00
2021-04-21 11:02:17 +01:00
public override void Start()
{
base.Start();
Player.TransformSync = this;
}
protected override void OnSceneLoaded(OWScene scene, bool isInUniverse)
{
2021-07-12 16:04:02 +01:00
if (!HasAuthority)
{
base.OnSceneLoaded(scene, isInUniverse);
}
if (isInUniverse)
{
Player.PlayerStates.IsReady = true;
QSBEventManager.FireEvent(EventNames.QSBPlayerReady, true);
}
else
{
Player.PlayerStates.IsReady = false;
QSBEventManager.FireEvent(EventNames.QSBPlayerReady, false);
}
base.OnSceneLoaded(scene, isInUniverse);
}
2021-04-22 20:42:38 +01:00
protected override void OnDestroy()
2020-12-14 21:20:53 +00:00
{
2021-07-11 16:18:47 +01:00
// TODO : Maybe move this to a leave event...? Would ensure everything could finish up before removing the player
2021-03-23 13:18:29 +00:00
QSBPlayerManager.OnRemovePlayer?.Invoke(PlayerId);
2021-04-22 20:42:38 +01:00
base.OnDestroy();
2021-03-09 16:43:41 +00:00
if (QSBPlayerManager.PlayerExists(PlayerId))
{
Player.HudMarker?.Remove();
QSBPlayerManager.RemovePlayer(PlayerId);
}
2020-12-14 21:20:53 +00:00
}
2020-12-13 22:25:23 +00:00
2021-07-07 23:54:09 +01:00
protected override Component InitLocalTransform()
2020-12-14 21:20:53 +00:00
{
2021-05-15 14:10:51 +01:00
SectorSync.Init(Locator.GetPlayerSectorDetector(), this);
2021-07-06 22:29:47 +01:00
// player body
2021-07-07 19:45:14 +01:00
var player = Locator.GetPlayerTransform();
var playerModel = player.Find("Traveller_HEA_Player_v2");
GetComponent<AnimationSync>().InitLocal(playerModel);
GetComponent<InstrumentsManager>().InitLocal(player);
Player.Body = player.gameObject;
2021-07-06 22:29:47 +01:00
// camera
var cameraBody = Locator.GetPlayerCamera().gameObject.transform;
Player.Camera = Locator.GetPlayerCamera();
Player.CameraBody = cameraBody.gameObject;
_visibleCameraRoot = cameraBody;
// stick
var pivot = GetStickPivot();
Player.RoastingStick = pivot.gameObject;
_visibleStickPivot = pivot;
_visibleStickTip = pivot.Find("Stick_Tip");
DebugLog.DebugWrite("PlayerTransformSync init done - Request state!");
QSBEventManager.FireEvent(EventNames.QSBPlayerStatesRequest);
2021-07-07 19:45:14 +01:00
return player;
2020-12-14 21:20:53 +00:00
}
2020-02-12 20:05:08 +01:00
2021-07-07 23:54:09 +01:00
protected override Component InitRemoteTransform()
2020-12-14 21:20:53 +00:00
{
2021-07-07 19:45:14 +01:00
/*
* CREATE PLAYER STRUCTURE
*/
2021-07-07 19:46:00 +01:00
2021-07-07 19:45:14 +01:00
// Variable naming convention is broken here to reflect OW unity project (with REMOTE_ prefixed) for readability
var REMOTE_Player_Body = new GameObject("REMOTE_Player_Body");
var REMOTE_PlayerCamera = new GameObject("REMOTE_PlayerCamera");
REMOTE_PlayerCamera.transform.parent = REMOTE_Player_Body.transform;
REMOTE_PlayerCamera.transform.localPosition = new Vector3(0f, 0.8496093f, 0.1500003f);
var REMOTE_RoastingSystem = new GameObject("REMOTE_RoastingSystem");
REMOTE_RoastingSystem.transform.parent = REMOTE_Player_Body.transform;
REMOTE_RoastingSystem.transform.localPosition = new Vector3(0f, 0.4f, 0f);
var REMOTE_Stick_Root = new GameObject("REMOTE_Stick_Root");
REMOTE_Stick_Root.transform.parent = REMOTE_RoastingSystem.transform;
REMOTE_Stick_Root.transform.localPosition = new Vector3(0.25f, 0f, 0.08f);
REMOTE_Stick_Root.transform.localRotation = Quaternion.Euler(0f, -10f, 0f);
/*
* SET UP PLAYER BODY
*/
2021-07-07 19:46:00 +01:00
2021-07-07 19:45:14 +01:00
var player = Locator.GetPlayerTransform();
var playerModel = player.Find("Traveller_HEA_Player_v2");
var REMOTE_Traveller_HEA_Player_v2 = Instantiate(playerModel);
REMOTE_Traveller_HEA_Player_v2.transform.parent = REMOTE_Player_Body.transform;
REMOTE_Traveller_HEA_Player_v2.transform.localPosition = new Vector3(0f, -1.03f, -0.2f);
REMOTE_Traveller_HEA_Player_v2.transform.localRotation = Quaternion.Euler(-1.500009f, 0f, 0f);
REMOTE_Traveller_HEA_Player_v2.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
Player.Body = REMOTE_Player_Body;
GetComponent<AnimationSync>().InitRemote(REMOTE_Traveller_HEA_Player_v2);
GetComponent<InstrumentsManager>().InitRemote(REMOTE_Player_Body.transform);
var marker = REMOTE_Player_Body.AddComponent<PlayerHUDMarker>();
2020-12-14 21:20:53 +00:00
marker.Init(Player);
2021-07-07 19:45:14 +01:00
REMOTE_Player_Body.AddComponent<PlayerMapMarker>().PlayerName = Player.Name;
2021-07-06 22:29:47 +01:00
2021-07-07 19:45:14 +01:00
/*
* SET UP PLAYER CAMERA
*/
2021-07-07 19:46:00 +01:00
2021-07-07 19:45:14 +01:00
PlayerToolsManager.Init(REMOTE_PlayerCamera.transform);
2021-07-06 22:29:47 +01:00
2021-07-07 19:45:14 +01:00
var camera = REMOTE_PlayerCamera.AddComponent<Camera>();
2021-07-06 22:29:47 +01:00
camera.enabled = false;
2021-07-07 19:45:14 +01:00
var owcamera = REMOTE_PlayerCamera.AddComponent<OWCamera>();
2021-07-06 22:29:47 +01:00
owcamera.fieldOfView = 70;
owcamera.nearClipPlane = 0.1f;
owcamera.farClipPlane = 50000f;
Player.Camera = owcamera;
2021-07-07 19:45:14 +01:00
Player.CameraBody = REMOTE_PlayerCamera;
_visibleCameraRoot = REMOTE_PlayerCamera.transform;
2021-07-06 22:29:47 +01:00
2021-07-07 19:45:14 +01:00
/*
* SET UP ROASTING STICK
*/
var REMOTE_Stick_Pivot = Instantiate(GetStickPivot());
REMOTE_Stick_Pivot.parent = REMOTE_Stick_Root.transform;
REMOTE_Stick_Pivot.gameObject.SetActive(false);
Destroy(REMOTE_Stick_Pivot.Find("Stick_Tip/Props_HEA_RoastingStick/RoastingStick_Arm").gameObject);
Destroy(REMOTE_Stick_Pivot.Find("Stick_Tip/Props_HEA_RoastingStick/RoastingStick_Arm_NoSuit").gameObject);
2021-07-06 22:29:47 +01:00
2021-07-07 19:45:14 +01:00
var mallowRoot = REMOTE_Stick_Pivot.Find("Stick_Tip/Mallow_Root");
2021-07-06 22:29:47 +01:00
mallowRoot.gameObject.SetActive(false);
var oldMarshmallow = mallowRoot.GetComponent<Marshmallow>();
// Recreate particle system
Destroy(mallowRoot.Find("MallowSmoke").GetComponent<RelativisticParticleSystem>());
var newSystem = mallowRoot.Find("MallowSmoke").gameObject.AddComponent<CustomRelativisticParticleSystem>();
newSystem.Init(Player);
// Create new marshmallow
var newMarshmallow = mallowRoot.gameObject.AddComponent<QSBMarshmallow>();
newMarshmallow._fireRenderer = oldMarshmallow.GetValue<MeshRenderer>("_fireRenderer");
newMarshmallow._smokeParticles = oldMarshmallow.GetValue<ParticleSystem>("_smokeParticles");
newMarshmallow._mallowRenderer = oldMarshmallow.GetValue<MeshRenderer>("_mallowRenderer");
newMarshmallow._rawColor = oldMarshmallow.GetValue<Color>("_rawColor");
newMarshmallow._toastedColor = oldMarshmallow.GetValue<Color>("_toastedColor");
newMarshmallow._burntColor = oldMarshmallow.GetValue<Color>("_burntColor");
Destroy(oldMarshmallow);
2021-07-07 19:45:14 +01:00
Player.RoastingStick = REMOTE_Stick_Pivot.gameObject;
2021-07-06 22:29:47 +01:00
Player.Marshmallow = newMarshmallow;
mallowRoot.gameObject.SetActive(true);
2021-07-07 19:45:14 +01:00
_visibleStickPivot = REMOTE_Stick_Pivot;
_visibleStickTip = REMOTE_Stick_Pivot.Find("Stick_Tip");
2021-07-06 22:29:47 +01:00
2021-07-07 19:45:14 +01:00
return REMOTE_Player_Body.transform;
2021-07-06 22:29:47 +01:00
}
2021-07-11 16:18:47 +01:00
protected override bool UpdateTransform()
2021-07-06 22:29:47 +01:00
{
2021-07-11 16:18:47 +01:00
if (!base.UpdateTransform())
{
return false;
}
2021-07-06 22:29:47 +01:00
2021-07-07 21:14:57 +01:00
UpdateSpecificTransform(_visibleStickPivot, _networkStickPivot, ref _pivotPositionVelocity, ref _pivotRotationVelocity);
UpdateSpecificTransform(_visibleStickTip, _networkStickTip, ref _tipPositionVelocity, ref _tipRotationVelocity);
UpdateSpecificTransform(_visibleCameraRoot, _networkCameraRoot, ref _cameraPositionVelocity, ref _cameraRotationVelocity);
2021-07-11 16:18:47 +01:00
return true;
2021-07-07 20:35:37 +01:00
}
2021-07-07 21:14:57 +01:00
private void UpdateSpecificTransform(Transform visible, Transform network, ref Vector3 positionVelocity, ref Quaternion rotationVelocity)
2021-07-07 20:35:37 +01:00
{
2021-07-06 22:29:47 +01:00
if (HasAuthority)
{
2021-07-07 20:35:37 +01:00
network.localPosition = visible.localPosition;
network.localRotation = visible.localRotation;
2021-07-06 22:29:47 +01:00
return;
}
2021-07-07 21:14:57 +01:00
visible.localPosition = Vector3.SmoothDamp(visible.localPosition, network.localPosition, ref positionVelocity, SmoothTime);
visible.localRotation = QuaternionHelper.SmoothDamp(visible.localRotation, network.localRotation, ref rotationVelocity, SmoothTime);
2020-12-14 21:20:53 +00:00
}
2021-07-11 16:18:47 +01:00
public override bool IsReady
=> Locator.GetPlayerTransform() != null
2020-12-14 21:20:53 +00:00
&& Player != null
&& QSBPlayerManager.PlayerExists(Player.PlayerId)
&& Player.PlayerStates.IsReady
2020-12-14 21:20:53 +00:00
&& NetId.Value != uint.MaxValue
2021-07-11 16:18:47 +01:00
&& NetId.Value != 0U
&& WorldObjectManager.AllReady;
2021-05-15 14:10:51 +01:00
public static PlayerTransformSync LocalInstance { get; private set; }
public override bool UseInterpolation => true;
public override TargetType Type => TargetType.Player;
2020-12-14 21:20:53 +00:00
}
2020-12-03 08:28:05 +00:00
}