316 lines
8.5 KiB
C#
Raw Normal View History

2020-11-08 19:32:04 +00:00
using OWML.Common;
2020-12-20 10:56:15 +00:00
using OWML.Utils;
2021-12-25 18:23:20 -08:00
using QSB.Animation.Player.Messages;
2021-05-07 14:58:37 +01:00
using QSB.Animation.Player.Thrusters;
2021-12-25 18:23:20 -08:00
using QSB.Messaging;
2020-11-03 21:33:48 +00:00
using QSB.Player;
2020-11-08 14:41:16 +00:00
using QSB.Utility;
2020-12-07 21:19:16 +00:00
using QuantumUNET.Components;
2020-08-21 14:04:13 +01:00
using System.Linq;
using UnityEngine;
2021-04-26 14:30:21 +01:00
namespace QSB.Animation.Player
{
2020-12-02 21:29:53 +00:00
public class AnimationSync : PlayerSyncObject
{
private RuntimeAnimatorController _suitedAnimController;
private AnimatorOverrideController _unsuitedAnimController;
private GameObject _suitedGraphics;
private GameObject _unsuitedGraphics;
private PlayerCharacterController _playerController;
private CrouchSync _crouchSync;
private RuntimeAnimatorController _chertController;
2021-03-06 13:00:28 +00:00
//private readonly RuntimeAnimatorController _eskerController;
//private readonly RuntimeAnimatorController _feldsparController;
//private readonly RuntimeAnimatorController _gabbroController;
2020-12-02 21:29:53 +00:00
private RuntimeAnimatorController _riebeckController;
public AnimatorMirror Mirror { get; private set; }
2020-12-14 21:41:56 +01:00
public AnimationType CurrentType { get; set; }
2021-04-20 08:36:07 +01:00
public Animator VisibleAnimator { get; private set; }
2021-04-24 00:10:29 +01:00
public Animator InvisibleAnimator { get; private set; }
public QNetworkAnimator NetworkAnimator { get; private set; }
2020-12-03 08:28:05 +00:00
2020-12-14 19:23:24 +00:00
protected void Awake()
2020-12-02 21:29:53 +00:00
{
2021-04-24 00:10:29 +01:00
InvisibleAnimator = gameObject.AddComponent<Animator>();
NetworkAnimator = gameObject.AddComponent<QNetworkAnimator>();
NetworkAnimator.enabled = false;
NetworkAnimator.animator = InvisibleAnimator;
2020-12-02 21:29:53 +00:00
2020-12-14 21:41:56 +01:00
QSBSceneManager.OnUniverseSceneLoaded += OnUniverseSceneLoaded;
2020-12-02 21:29:53 +00:00
}
2020-12-14 21:20:53 +00:00
protected override void OnDestroy()
2020-12-02 21:29:53 +00:00
{
2020-12-14 16:04:16 +00:00
base.OnDestroy();
2021-04-24 00:10:29 +01:00
Destroy(InvisibleAnimator);
Destroy(NetworkAnimator);
2021-03-26 20:56:57 +00:00
QSBSceneManager.OnUniverseSceneLoaded -= OnUniverseSceneLoaded;
2020-12-14 21:20:53 +00:00
}
2020-12-14 21:41:56 +01:00
private void OnUniverseSceneLoaded(OWScene oldScene, OWScene newScene) => LoadControllers();
2020-12-02 21:29:53 +00:00
2020-12-14 21:20:53 +00:00
private void LoadControllers()
2020-12-02 21:29:53 +00:00
{
2020-12-14 16:24:52 +00:00
var bundle = QSBCore.InstrumentAssetBundle;
2020-12-02 21:29:53 +00:00
_chertController = bundle.LoadAsset("assets/Chert/Traveller_Chert.controller") as RuntimeAnimatorController;
_riebeckController = bundle.LoadAsset("assets/Riebeck/Traveller_Riebeck.controller") as RuntimeAnimatorController;
}
private void InitCommon(Transform body)
{
if (QSBSceneManager.IsInUniverse)
{
LoadControllers();
}
2021-06-18 22:38:32 +01:00
2021-04-24 00:10:29 +01:00
NetworkAnimator.enabled = true;
2021-04-20 08:36:07 +01:00
VisibleAnimator = body.GetComponent<Animator>();
2020-12-02 21:29:53 +00:00
Mirror = body.gameObject.AddComponent<AnimatorMirror>();
if (IsLocalPlayer)
{
2021-04-24 00:10:29 +01:00
Mirror.Init(VisibleAnimator, InvisibleAnimator);
2020-12-02 21:29:53 +00:00
}
else
{
2021-04-24 00:10:29 +01:00
Mirror.Init(InvisibleAnimator, VisibleAnimator);
2020-12-02 21:29:53 +00:00
}
2021-04-24 00:10:29 +01:00
for (var i = 0; i < InvisibleAnimator.parameterCount; i++)
2020-12-02 21:29:53 +00:00
{
2021-04-24 00:10:29 +01:00
NetworkAnimator.SetParameterAutoSend(i, true);
2020-12-02 21:29:53 +00:00
}
var playerAnimController = body.GetComponent<PlayerAnimController>();
_suitedAnimController = AnimControllerPatch.SuitedAnimController;
_unsuitedAnimController = playerAnimController.GetValue<AnimatorOverrideController>("_unsuitedAnimOverride");
_suitedGraphics = playerAnimController.GetValue<GameObject>("_suitedGroup");
_unsuitedGraphics = playerAnimController.GetValue<GameObject>("_unsuitedGroup");
}
public void InitLocal(Transform body)
{
InitCommon(body);
_playerController = body.parent.GetComponent<PlayerCharacterController>();
InitCrouchSync();
2021-05-07 19:17:09 +01:00
InitAccelerationSync();
2020-12-02 21:29:53 +00:00
}
public void InitRemote(Transform body)
{
InitCommon(body);
var playerAnimController = body.GetComponent<PlayerAnimController>();
playerAnimController.enabled = false;
playerAnimController.SetValue("_suitedGroup", new GameObject());
playerAnimController.SetValue("_unsuitedGroup", new GameObject());
playerAnimController.SetValue("_baseAnimController", null);
playerAnimController.SetValue("_unsuitedAnimOverride", null);
playerAnimController.SetValue("_rightArmHidden", false);
var rightArmObjects = playerAnimController.GetValue<GameObject[]>("_rightArmObjects").ToList();
rightArmObjects.ForEach(rightArmObject => rightArmObject.layer = LayerMask.NameToLayer("Default"));
body.Find("player_mesh_noSuit:Traveller_HEA_Player/player_mesh_noSuit:Player_Head").gameObject.layer = 0;
body.Find("Traveller_Mesh_v01:Traveller_Geo/Traveller_Mesh_v01:PlayerSuit_Helmet").gameObject.layer = 0;
2020-12-05 18:41:38 +00:00
SetAnimationType(AnimationType.PlayerUnsuited);
2020-12-02 21:29:53 +00:00
InitCrouchSync();
2021-05-07 19:17:09 +01:00
InitAccelerationSync();
2021-05-07 14:58:37 +01:00
ThrusterManager.CreateRemotePlayerVFX(Player);
2020-12-02 21:29:53 +00:00
var ikSync = body.gameObject.AddComponent<PlayerHeadRotationSync>();
2021-03-13 10:17:52 +00:00
QSBCore.UnityEvents.RunWhen(() => Player.CameraBody != null, () => ikSync.Init(Player.CameraBody.transform));
2020-12-02 21:29:53 +00:00
}
2021-05-07 19:17:09 +01:00
private void InitAccelerationSync()
2021-05-07 14:58:37 +01:00
{
Player.JetpackAcceleration = GetComponent<JetpackAccelerationSync>();
var thrusterModel = HasAuthority ? Locator.GetPlayerBody().GetComponent<ThrusterModel>() : null;
Player.JetpackAcceleration.Init(thrusterModel);
}
2020-12-02 21:29:53 +00:00
private void InitCrouchSync()
{
2021-05-03 20:13:03 +01:00
_crouchSync = GetComponent<CrouchSync>();
_crouchSync.Init(_playerController, VisibleAnimator);
2020-12-02 21:29:53 +00:00
}
private void SuitUp()
{
2021-12-25 18:23:20 -08:00
new ChangeAnimTypeMessage(PlayerId, AnimationType.PlayerSuited).Send();
2020-12-02 21:29:53 +00:00
SetAnimationType(AnimationType.PlayerSuited);
}
private void SuitDown()
{
2021-12-25 18:23:20 -08:00
new ChangeAnimTypeMessage(PlayerId, AnimationType.PlayerUnsuited).Send();
2020-12-02 21:29:53 +00:00
SetAnimationType(AnimationType.PlayerUnsuited);
}
public void SetSuitState(bool state)
{
if (!Player.IsReady)
2020-12-19 13:31:05 +00:00
{
return;
}
2021-06-18 22:38:32 +01:00
2020-12-02 21:29:53 +00:00
if (state)
{
SuitUp();
return;
}
2021-06-18 22:38:32 +01:00
2020-12-02 21:29:53 +00:00
SuitDown();
}
public void SetAnimationType(AnimationType type)
{
if (CurrentType == type)
{
return;
}
2021-06-18 22:38:32 +01:00
2020-12-02 21:29:53 +00:00
CurrentType = type;
if (_unsuitedAnimController == null)
{
2020-12-19 18:44:27 +00:00
DebugLog.ToConsole($"Error - Unsuited controller is null. ({PlayerId})", MessageType.Error);
2020-12-02 21:29:53 +00:00
}
2021-06-18 22:38:32 +01:00
2020-12-02 21:29:53 +00:00
if (_suitedAnimController == null)
{
2020-12-19 18:44:27 +00:00
DebugLog.ToConsole($"Error - Suited controller is null. ({PlayerId})", MessageType.Error);
2020-12-02 21:29:53 +00:00
}
2021-06-18 22:38:32 +01:00
2021-12-10 22:13:39 +00:00
if (_unsuitedGraphics == null)
{
DebugLog.ToConsole($"Warning - _unsuitedGraphics is null! ({PlayerId})", MessageType.Warning);
}
if (_suitedGraphics == null)
{
DebugLog.ToConsole($"Warning - _suitedGraphics is null! ({PlayerId})", MessageType.Warning);
}
2020-12-02 21:29:53 +00:00
RuntimeAnimatorController controller = default;
switch (type)
{
case AnimationType.PlayerSuited:
controller = _suitedAnimController;
2021-12-10 22:13:39 +00:00
if (_unsuitedGraphics != null)
{
_unsuitedGraphics?.SetActive(false);
}
2021-12-25 18:23:20 -08:00
2021-12-10 22:13:39 +00:00
if (_suitedGraphics != null)
{
_suitedGraphics?.SetActive(true);
}
2021-12-25 18:23:20 -08:00
2020-12-02 21:29:53 +00:00
break;
2020-12-03 08:28:05 +00:00
2020-12-02 21:29:53 +00:00
case AnimationType.PlayerUnsuited:
controller = _unsuitedAnimController;
2021-12-10 22:13:39 +00:00
if (_unsuitedGraphics != null)
{
_unsuitedGraphics?.SetActive(true);
}
if (_suitedGraphics != null)
{
_suitedGraphics?.SetActive(false);
}
2020-12-02 21:29:53 +00:00
break;
2020-12-03 08:28:05 +00:00
2020-12-02 21:29:53 +00:00
case AnimationType.Chert:
controller = _chertController;
break;
2020-12-03 08:28:05 +00:00
2020-12-02 21:29:53 +00:00
case AnimationType.Esker:
2021-03-06 13:00:28 +00:00
//controller = _eskerController;
2020-12-02 21:29:53 +00:00
break;
2020-12-03 08:28:05 +00:00
2020-12-02 21:29:53 +00:00
case AnimationType.Feldspar:
2021-03-06 13:00:28 +00:00
//controller = _feldsparController;
2020-12-02 21:29:53 +00:00
break;
2020-12-03 08:28:05 +00:00
2020-12-02 21:29:53 +00:00
case AnimationType.Gabbro:
2021-03-06 13:00:28 +00:00
//controller = _gabbroController;
2020-12-02 21:29:53 +00:00
break;
2020-12-03 08:28:05 +00:00
2020-12-02 21:29:53 +00:00
case AnimationType.Riebeck:
controller = _riebeckController;
break;
}
2021-06-18 22:38:32 +01:00
2021-12-10 14:54:51 +00:00
if (InvisibleAnimator == null)
{
DebugLog.ToConsole($"Error - InvisibleAnimator is null. ({PlayerId})", MessageType.Error);
}
else
{
InvisibleAnimator.runtimeAnimatorController = controller;
}
if (VisibleAnimator == null)
{
DebugLog.ToConsole($"Error - VisibleAnimator is null. ({PlayerId})", MessageType.Error);
}
else
{
VisibleAnimator.runtimeAnimatorController = controller;
}
2021-11-20 19:49:50 +00:00
if (type is not AnimationType.PlayerSuited and not AnimationType.PlayerUnsuited)
2020-12-02 21:29:53 +00:00
{
2021-12-11 20:06:02 +00:00
if (VisibleAnimator != null)
{
VisibleAnimator.SetTrigger("Playing");
}
2021-12-25 18:23:20 -08:00
2021-12-11 20:06:02 +00:00
if (InvisibleAnimator != null)
{
InvisibleAnimator.SetTrigger("Playing");
}
2020-12-02 21:29:53 +00:00
}
else
{
// Avoids "jumping" when exiting instrument and putting on suit
2021-12-11 20:06:02 +00:00
if (VisibleAnimator != null)
{
VisibleAnimator.SetTrigger("Grounded");
}
if (InvisibleAnimator != null)
{
InvisibleAnimator.SetTrigger("Grounded");
}
2020-12-02 21:29:53 +00:00
}
2021-06-18 22:38:32 +01:00
2021-12-10 14:54:51 +00:00
if (NetworkAnimator == null)
2020-12-02 21:29:53 +00:00
{
2021-12-10 14:54:51 +00:00
DebugLog.ToConsole($"Error - NetworkAnimator is null. ({PlayerId})", MessageType.Error);
}
2021-12-10 22:13:39 +00:00
else if (Mirror == null)
{
DebugLog.ToConsole($"Error - Mirror is null. ({PlayerId})", MessageType.Error);
}
else if (InvisibleAnimator != null)
2021-12-10 14:54:51 +00:00
{
NetworkAnimator.animator = InvisibleAnimator; // Probably not needed.
Mirror.RebuildFloatParams();
2021-12-10 22:13:39 +00:00
for (var i = 0; i < InvisibleAnimator.parameterCount; i++)
2021-12-10 14:54:51 +00:00
{
NetworkAnimator.SetParameterAutoSend(i, true);
}
2020-12-02 21:29:53 +00:00
}
}
}
2020-11-08 16:13:10 +00:00
}