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

201 lines
5.5 KiB
C#
Raw Normal View History

using Mirror;
using OWML.Common;
2022-01-16 04:27:24 +00:00
using OWML.Utils;
2021-12-26 02:23:20 +00:00
using QSB.Animation.Player.Messages;
2021-05-07 13:58:37 +00:00
using QSB.Animation.Player.Thrusters;
2021-12-26 02:23:20 +00: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;
2022-08-15 10:14:23 +00:00
using QSB.WorldSync;
2022-01-23 16:14:42 +00:00
using System;
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.Animation.Player;
2022-08-27 18:44:52 +00:00
[UsedInUnityProject]
2022-03-03 03:46:33 +00:00
public class AnimationSync : PlayerSyncObject
{
2022-03-03 03:46:33 +00:00
private RuntimeAnimatorController _suitedAnimController;
private AnimatorOverrideController _unsuitedAnimController;
private GameObject _suitedGraphics;
private GameObject _unsuitedGraphics;
public AnimatorMirror Mirror { get; private set; }
public bool InSuitedUpState { get; set; }
public Animator VisibleAnimator { get; private set; }
public Animator InvisibleAnimator { get; private set; }
public NetworkAnimator NetworkAnimator { get; private set; }
protected void Awake()
2020-12-02 21:29:53 +00:00
{
2022-03-03 03:46:33 +00:00
InvisibleAnimator = gameObject.GetRequiredComponent<Animator>();
NetworkAnimator = gameObject.GetRequiredComponent<NetworkAnimator>();
NetworkAnimator.enabled = false;
2022-08-15 19:21:09 +00:00
RequestInitialStatesMessage.SendInitialState += SendInitialState;
2022-03-03 03:46:33 +00:00
}
2022-08-15 19:21:09 +00:00
protected void OnDestroy() => RequestInitialStatesMessage.SendInitialState -= SendInitialState;
2022-08-15 10:14:23 +00:00
2022-08-15 19:22:05 +00:00
/// <summary>
/// This wipes the NetworkAnimator's fields, so it assumes the parameters have changed.
/// Basically just forces it to set all its dirty flags.
/// BUG: this doesnt work for other players because its only called by the host.
2022-08-15 19:22:05 +00:00
/// </summary>
private void SendInitialState(uint to) => NetworkAnimator.Invoke("Awake");
2022-08-15 10:14:23 +00:00
2022-08-18 11:20:52 +00:00
public void Reset() => InSuitedUpState = false;
2022-03-03 03:46:33 +00:00
private void InitCommon(Transform modelRoot)
{
try
2020-12-02 21:29:53 +00:00
{
2022-03-03 03:46:33 +00:00
if (modelRoot == null)
2020-12-02 21:29:53 +00:00
{
DebugLog.ToConsole("Error - Trying to InitCommon with null body!", MessageType.Error);
2022-03-03 03:46:33 +00:00
return;
}
2022-01-23 16:14:42 +00:00
2022-03-03 03:46:33 +00:00
VisibleAnimator = modelRoot.GetComponent<Animator>();
Mirror = modelRoot.gameObject.AddComponent<AnimatorMirror>();
if (isLocalPlayer)
{
2022-08-14 11:33:04 +00:00
Mirror.Init(VisibleAnimator, InvisibleAnimator, NetworkAnimator);
2020-12-02 21:29:53 +00:00
}
2022-03-03 03:46:33 +00:00
else
2020-12-02 21:29:53 +00:00
{
2022-08-14 11:33:04 +00:00
Mirror.Init(InvisibleAnimator, VisibleAnimator, null);
2020-12-02 21:29:53 +00:00
}
2022-03-03 03:46:33 +00:00
NetworkAnimator.enabled = true;
NetworkAnimator.Invoke("Awake");
2022-03-03 03:46:33 +00:00
_suitedAnimController = Instantiate(QSBCore.NetworkAssetBundle.LoadAsset<RuntimeAnimatorController>("Assets/GameAssets/AnimatorController/Player.controller"));
_unsuitedAnimController = Instantiate(QSBCore.NetworkAssetBundle.LoadAsset<AnimatorOverrideController>("Assets/GameAssets/AnimatorOverrideController/PlayerUnsuitedOverride.overrideController"));
_suitedGraphics = modelRoot.GetChild(1).gameObject;
_unsuitedGraphics = modelRoot.GetChild(0).gameObject;
2022-03-03 03:46:33 +00:00
VisibleAnimator.SetLayerWeight(2, 1f);
2021-05-07 13:58:37 +00:00
}
2022-03-03 03:46:33 +00:00
catch (Exception ex)
2020-12-02 21:29:53 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Exception thrown when running InitCommon on {(modelRoot != null ? modelRoot.name : "NULL BODY")}. {ex.Message} Stacktrace: {ex.StackTrace}", MessageType.Error);
}
2022-03-03 03:46:33 +00:00
}
2021-06-18 21:38:32 +00:00
2022-03-03 03:46:33 +00:00
public void InitLocal(Transform body)
{
InitCommon(body);
InitAccelerationSync();
}
public void InitRemote(Transform body)
{
InitCommon(body);
SetSuitState(QSBSceneManager.CurrentScene == OWScene.EyeOfTheUniverse);
InitAccelerationSync();
ThrusterManager.CreateRemotePlayerVFX(Player);
2022-08-28 20:35:31 +00:00
ThrusterManager.CreateRemotePlayerSFX(Player);
2022-03-03 03:46:33 +00:00
Delay.RunWhen(() => Player.CameraBody != null,
() => body.GetComponent<PlayerHeadRotationSync>().Init(Player.CameraBody.transform));
}
private void InitAccelerationSync()
{
2022-05-19 13:34:49 +00:00
Player.JetpackAcceleration = GetComponent<JetpackAccelerationSync>();
2022-03-03 03:46:33 +00:00
var thrusterModel = hasAuthority ? Locator.GetPlayerBody().GetComponent<ThrusterModel>() : null;
Player.JetpackAcceleration.Init(thrusterModel);
}
public void SetSuitState(bool suitedUp)
{
if (!Player.IsReady)
{
2022-03-03 03:46:33 +00:00
return;
}
2021-06-18 21:38:32 +00:00
2022-03-03 03:46:33 +00:00
if (Player == QSBPlayerManager.LocalPlayer)
{
2022-03-03 03:46:33 +00:00
new PlayerSuitMessage(suitedUp).Send();
}
2021-12-10 22:13:39 +00:00
2022-03-03 03:46:33 +00:00
if (InSuitedUpState == suitedUp)
{
2022-03-03 03:46:33 +00:00
return;
}
2021-12-10 14:54:51 +00:00
2022-03-03 03:46:33 +00:00
InSuitedUpState = suitedUp;
if (_unsuitedAnimController == null)
{
DebugLog.ToConsole($"Error - Unsuited controller is null. ({PlayerId})", MessageType.Error);
}
2021-12-10 14:54:51 +00:00
2022-03-03 03:46:33 +00:00
if (_suitedAnimController == null)
{
DebugLog.ToConsole($"Error - Suited controller is null. ({PlayerId})", MessageType.Error);
}
2021-12-11 20:06:02 +00:00
2022-03-03 03:46:33 +00:00
if (_unsuitedGraphics == null)
{
DebugLog.ToConsole($"Warning - _unsuitedGraphics is null! ({PlayerId})", MessageType.Warning);
}
2021-06-18 21:38:32 +00:00
2022-03-03 03:46:33 +00:00
if (_suitedGraphics == null)
{
DebugLog.ToConsole($"Warning - _suitedGraphics is null! ({PlayerId})", MessageType.Warning);
}
2022-03-03 03:46:33 +00:00
var controller = suitedUp ? _suitedAnimController : _unsuitedAnimController;
if (_unsuitedGraphics != null)
{
_unsuitedGraphics?.SetActive(!suitedUp);
}
2022-03-03 03:46:33 +00:00
if (_suitedGraphics != null)
{
_suitedGraphics?.SetActive(suitedUp);
}
2022-03-03 03:46:33 +00:00
if (InvisibleAnimator == null)
{
DebugLog.ToConsole($"Error - InvisibleAnimator is null. ({PlayerId})", MessageType.Error);
}
else
{
InvisibleAnimator.runtimeAnimatorController = controller;
}
2022-03-03 03:46:33 +00:00
if (VisibleAnimator == null)
{
DebugLog.ToConsole($"Error - VisibleAnimator is null. ({PlayerId})", MessageType.Error);
}
else
{
VisibleAnimator.runtimeAnimatorController = controller;
}
2022-03-03 03:46:33 +00:00
// Avoids "jumping" when putting on suit
if (VisibleAnimator != null)
{
2022-08-13 19:11:48 +00:00
VisibleAnimator.SetBool("Grounded", true);
2022-03-03 03:46:33 +00:00
}
2022-03-03 03:46:33 +00:00
if (InvisibleAnimator != null)
{
2022-08-13 19:11:48 +00:00
InvisibleAnimator.SetBool("Grounded", true);
2022-03-03 03:46:33 +00:00
}
2022-03-03 03:46:33 +00:00
if (NetworkAnimator == null)
{
DebugLog.ToConsole($"Error - NetworkAnimator is null. ({PlayerId})", MessageType.Error);
}
2022-03-03 03:46:33 +00:00
else if (Mirror == null)
{
DebugLog.ToConsole($"Error - Mirror is null. ({PlayerId})", MessageType.Error);
}
Mirror.RebuildFloatParams();
NetworkAnimator.Invoke("Awake");
2020-12-02 21:29:53 +00:00
}
}