303 lines
7.9 KiB
C#
Raw Normal View History

using Mirror;
using OWML.Common;
2022-01-15 20:27:24 -08: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-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 NetworkAnimator 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
{
2022-01-15 03:54:37 -08:00
InvisibleAnimator = gameObject.GetRequiredComponent<Animator>();
NetworkAnimator = gameObject.GetRequiredComponent<NetworkAnimator>();
2021-04-24 00:10:29 +01:00
NetworkAnimator.enabled = false;
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
2022-01-07 15:21:58 -08:00
protected void OnDestroy()
2020-12-02 21:29:53 +00:00
{
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-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)
2020-12-02 21:29:53 +00:00
{
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
}
NetworkAnimator.enabled = true;
2022-01-15 20:27:24 -08:00
NetworkAnimator.Invoke("Awake");
2020-12-02 21:29:53 +00:00
var playerAnimController = body.GetComponent<PlayerAnimController>();
2021-12-26 21:41:37 -08:00
_suitedAnimController = playerAnimController._baseAnimController;
2021-12-26 20:35:40 -08:00
_unsuitedAnimController = playerAnimController._unsuitedAnimOverride;
_suitedGraphics = playerAnimController._suitedGroup;
_unsuitedGraphics = playerAnimController._unsuitedGroup;
2020-12-02 21:29:53 +00:00
}
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;
2021-12-26 20:57:34 -08:00
playerAnimController._suitedGroup = new GameObject();
playerAnimController._unsuitedGroup = new GameObject();
playerAnimController._baseAnimController = null;
playerAnimController._unsuitedAnimOverride = null;
playerAnimController._rightArmHidden = false;
2020-12-02 21:29:53 +00:00
2021-12-26 20:35:40 -08:00
var rightArmObjects = playerAnimController._rightArmObjects.ToList();
2020-12-02 21:29:53 +00:00
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>();
2022-01-29 01:29:02 -08:00
Delay.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;
2021-05-07 14:58:37 +01:00
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()
{
new ChangeAnimTypeMessage(AnimationType.PlayerSuited).Send();
2020-12-02 21:29:53 +00:00
SetAnimationType(AnimationType.PlayerSuited);
}
private void SuitDown()
{
new ChangeAnimTypeMessage(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);
}
Mirror.RebuildFloatParams();
2022-01-15 20:27:24 -08:00
NetworkAnimator.Invoke("Awake");
2020-12-02 21:29:53 +00:00
}
}
2020-11-08 16:13:10 +00:00
}