quantum-space-buddies/QSB/Animation/Player/AnimationSync.cs

233 lines
7.0 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;
2020-12-14 16:24:52 +00:00
using QSB.Events;
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 13:04:13 +00:00
using System.Linq;
using UnityEngine;
2021-04-26 13:30:21 +00: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 20:41:56 +00:00
public AnimationType CurrentType { get; set; }
2021-04-20 07:36:07 +00:00
public Animator VisibleAnimator { get; private set; }
2021-04-23 23:10:29 +00: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-23 23:10:29 +00: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 20:41:56 +00: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-23 23:10:29 +00: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 20:41:56 +00:00
2020-12-14 21:20:53 +00:00
private void OnUniverseSceneLoaded(OWScene obj) => 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-04-23 23:10:29 +00:00
NetworkAnimator.enabled = true;
2021-04-20 07:36:07 +00:00
VisibleAnimator = body.GetComponent<Animator>();
2020-12-02 21:29:53 +00:00
Mirror = body.gameObject.AddComponent<AnimatorMirror>();
if (IsLocalPlayer)
{
2021-04-23 23:10:29 +00:00
Mirror.Init(VisibleAnimator, InvisibleAnimator);
2020-12-02 21:29:53 +00:00
}
else
{
2021-04-23 23:10:29 +00:00
Mirror.Init(InvisibleAnimator, VisibleAnimator);
2020-12-02 21:29:53 +00:00
}
2021-04-23 23:10:29 +00:00
for (var i = 0; i < InvisibleAnimator.parameterCount; i++)
2020-12-02 21:29:53 +00:00
{
2021-04-23 23:10:29 +00: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();
}
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();
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
}
private void InitCrouchSync()
{
_crouchSync = gameObject.AddComponent<CrouchSync>();
2021-04-20 07:36:07 +00:00
_crouchSync.Init(this, _playerController, VisibleAnimator);
2020-12-02 21:29:53 +00:00
}
2021-02-10 19:34:41 +00:00
public void SendCrouch(float value = 0) => QSBEventManager.FireEvent(EventNames.QSBCrouch, value);
2020-12-02 21:29:53 +00:00
public void HandleCrouch(float value) => _crouchSync.CrouchParam.Target = value;
2020-12-02 21:29:53 +00:00
private void SuitUp()
{
2021-02-10 19:34:41 +00:00
QSBEventManager.FireEvent(EventNames.QSBChangeAnimType, PlayerId, AnimationType.PlayerSuited);
2020-12-02 21:29:53 +00:00
SetAnimationType(AnimationType.PlayerSuited);
}
private void SuitDown()
{
2021-02-10 19:34:41 +00:00
QSBEventManager.FireEvent(EventNames.QSBChangeAnimType, PlayerId, AnimationType.PlayerUnsuited);
2020-12-02 21:29:53 +00:00
SetAnimationType(AnimationType.PlayerUnsuited);
}
public void SetSuitState(bool state)
{
2021-04-18 17:46:55 +00:00
if (!Player.PlayerStates.IsReady)
2020-12-19 13:31:05 +00:00
{
return;
}
2020-12-02 21:29:53 +00:00
if (state)
{
SuitUp();
return;
}
SuitDown();
}
public void SetAnimationType(AnimationType type)
{
if (CurrentType == type)
{
return;
}
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
}
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
}
RuntimeAnimatorController controller = default;
switch (type)
{
case AnimationType.PlayerSuited:
controller = _suitedAnimController;
_unsuitedGraphics?.SetActive(false);
_suitedGraphics?.SetActive(true);
break;
2020-12-03 08:28:05 +00:00
2020-12-02 21:29:53 +00:00
case AnimationType.PlayerUnsuited:
controller = _unsuitedAnimController;
_unsuitedGraphics?.SetActive(true);
_suitedGraphics?.SetActive(false);
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-04-23 23:10:29 +00:00
InvisibleAnimator.runtimeAnimatorController = controller;
2021-04-20 07:36:07 +00:00
VisibleAnimator.runtimeAnimatorController = controller;
2020-12-02 21:29:53 +00:00
if (type != AnimationType.PlayerSuited && type != AnimationType.PlayerUnsuited)
{
2021-04-20 07:36:07 +00:00
VisibleAnimator.SetTrigger("Playing");
2021-04-23 23:10:29 +00:00
InvisibleAnimator.SetTrigger("Playing");
2020-12-02 21:29:53 +00:00
}
else
{
// Avoids "jumping" when exiting instrument and putting on suit
2021-04-20 07:36:07 +00:00
VisibleAnimator.SetTrigger("Grounded");
2021-04-23 23:10:29 +00:00
InvisibleAnimator.SetTrigger("Grounded");
2020-12-02 21:29:53 +00:00
}
2021-04-23 23:10:29 +00:00
NetworkAnimator.animator = InvisibleAnimator; // Probably not needed.
2020-12-02 21:29:53 +00:00
Mirror.RebuildFloatParams();
2021-04-23 23:10:29 +00:00
for (var i = 0; i < InvisibleAnimator.parameterCount; i++)
2020-12-02 21:29:53 +00:00
{
2021-04-23 23:10:29 +00:00
NetworkAnimator.SetParameterAutoSend(i, true);
2020-12-02 21:29:53 +00:00
}
}
}
2020-11-08 16:13:10 +00:00
}