fix animation syncing

This commit is contained in:
Mister_Nebula 2020-11-09 21:05:50 +00:00
parent f425be060f
commit 18f87b0b35
7 changed files with 85 additions and 64 deletions

View File

@ -3,7 +3,6 @@ using OWML.ModHelper.Events;
using QSB.EventsCore;
using QSB.Player;
using QSB.Utility;
using System;
using System.Linq;
using UnityEngine;
@ -37,7 +36,7 @@ namespace QSB.Animation
_netAnim.enabled = false;
_netAnim.animator = _anim;
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
QSBSceneManager.OnSceneLoaded += (OWScene scene, bool universe) => LoadControllers(universe);
}
private void OnDestroy()
@ -51,11 +50,15 @@ namespace QSB.Animation
_playerController.OnBecomeGrounded -= OnBecomeGrounded;
_playerController.OnBecomeUngrounded -= OnBecomeUngrounded;
QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
QSBSceneManager.OnSceneLoaded -= (OWScene scene, bool universe) => LoadControllers(universe);
}
private void OnSceneLoaded(OWScene scene, bool inUniverse)
private void LoadControllers(bool universe)
{
if (!universe)
{
return;
}
var reibeckRoot = GameObject.Find("Traveller_HEA_Riebeck_ANIM_Talking");
_riebeckController = reibeckRoot.GetComponent<Animator>().runtimeAnimatorController;
var chertRoot = GameObject.Find("Traveller_HEA_Chert_ANIM_Chatter_Chipper");
@ -68,6 +71,10 @@ namespace QSB.Animation
private void InitCommon(Transform body)
{
if (QSBSceneManager.IsInUniverse)
{
LoadControllers(true);
}
_netAnim.enabled = true;
_bodyAnim = body.GetComponent<Animator>();
Mirror = body.gameObject.AddComponent<AnimatorMirror>();
@ -86,10 +93,17 @@ namespace QSB.Animation
{
_netAnim.SetParameterAutoSend(i, true);
}
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)
{
DebugLog.DebugWrite($"InitLocal ({PlayerId})");
InitCommon(body);
_playerController = body.parent.GetComponent<PlayerCharacterController>();
@ -102,16 +116,12 @@ namespace QSB.Animation
public void InitRemote(Transform body)
{
DebugLog.DebugWrite($"InitRemote ({PlayerId})");
InitCommon(body);
var playerAnimController = body.GetComponent<PlayerAnimController>();
playerAnimController.enabled = false;
_suitedAnimController = AnimControllerPatch.SuitedAnimController;
_unsuitedAnimController = playerAnimController.GetValue<AnimatorOverrideController>("_unsuitedAnimOverride");
_suitedGraphics = playerAnimController.GetValue<GameObject>("_suitedGroup");
_unsuitedGraphics = playerAnimController.GetValue<GameObject>("_unsuitedGroup");
playerAnimController.SetValue("_suitedGroup", new GameObject());
playerAnimController.SetValue("_unsuitedGroup", new GameObject());
playerAnimController.SetValue("_baseAnimController", null);
@ -149,16 +159,14 @@ namespace QSB.Animation
private void SuitUp()
{
GlobalMessenger<uint, AnimationType>.FireEvent(EventNames.QSBChangeAnimType, PlayerId, AnimationType.PlayerSuited);
SetAnimationType(AnimationType.PlayerSuited);
_unsuitedGraphics?.SetActive(false);
_suitedGraphics?.SetActive(true);
}
private void SuitDown()
{
GlobalMessenger<uint, AnimationType>.FireEvent(EventNames.QSBChangeAnimType, PlayerId, AnimationType.PlayerUnsuited);
SetAnimationType(AnimationType.PlayerUnsuited);
_unsuitedGraphics?.SetActive(true);
_suitedGraphics?.SetActive(false);
}
public void SetSuitState(bool state)
@ -177,33 +185,46 @@ namespace QSB.Animation
{
return;
}
DebugLog.DebugWrite($"{_bodyAnim.name} Changing animtype to {Enum.GetName(typeof(AnimationType), type)}");
GlobalMessenger<AnimationType>.FireEvent(EventNames.QSBChangeAnimType, type);
CurrentType = type;
if (_unsuitedAnimController == null)
{
DebugLog.DebugWrite($"Error - Unsuited controller is null. ({PlayerId})", MessageType.Error);
}
if (_suitedAnimController == null)
{
DebugLog.DebugWrite($"Error - Suited controller is null. ({PlayerId})", MessageType.Error);
}
RuntimeAnimatorController controller = default;
switch (type)
{
case AnimationType.PlayerSuited:
_bodyAnim.runtimeAnimatorController = _suitedAnimController;
_anim.runtimeAnimatorController = _suitedAnimController;
DebugLog.DebugWrite("done suited");
controller = _suitedAnimController;
_unsuitedGraphics?.SetActive(false);
_suitedGraphics?.SetActive(true);
break;
case AnimationType.PlayerUnsuited:
_bodyAnim.runtimeAnimatorController = _unsuitedAnimController;
_anim.runtimeAnimatorController = _unsuitedAnimController;
DebugLog.DebugWrite("done unsuited");
controller = _unsuitedAnimController;
_unsuitedGraphics?.SetActive(true);
_suitedGraphics?.SetActive(false);
break;
case AnimationType.Chert:
_bodyAnim.runtimeAnimatorController = _chertController;
_bodyAnim.SetTrigger("Playing");
_anim.runtimeAnimatorController = _chertController;
_anim.SetTrigger("Playing");
controller = _chertController;
break;
}
if (_bodyAnim.runtimeAnimatorController == null)
_anim.runtimeAnimatorController = controller;
_bodyAnim.runtimeAnimatorController = controller;
if (type != AnimationType.PlayerSuited && type != AnimationType.PlayerUnsuited)
{
DebugLog.ToConsole("Error - Somehow set RAC of bodyAnim to null?", MessageType.Error);
_bodyAnim.SetTrigger("Playing");
_anim.SetTrigger("Playing");
}
_netAnim.animator = _anim;
else
{
// Avoids "jumping" look when exiting instrument
_bodyAnim.SetTrigger("Grounded");
_anim.SetTrigger("Grounded");
}
_netAnim.animator = _anim; // Probably not needed.
Mirror.RebuildFloatParams();
for (var i = 0; i < _anim.parameterCount; i++)
{

View File

@ -88,12 +88,7 @@ namespace QSB.Animation
public void RebuildFloatParams()
{
if (_from.runtimeAnimatorController == null)
{
DebugLog.ToConsole($"Error - Controller of \"from\" for player {GetPlayer().PlayerId} is null! Current animtype is {GetPlayer().Animator.CurrentType}.", MessageType.Error);
}
_floatParams.Clear();
DebugLog.DebugWrite($"REBUILD FLOAT PARAMS id {GetPlayer().PlayerId}");
foreach (var param in _from.parameters.Where(p => p.type == AnimatorControllerParameterType.Float))
{
_floatParams.Add(param.name, new AnimFloatParam());

View File

@ -8,21 +8,21 @@ namespace QSB.Animation.Events
{
public override EventType Type => EventType.PlayInstrument;
public override void SetupListener() => GlobalMessenger<AnimationType>.AddListener(EventNames.QSBChangeAnimType, Handler);
public override void SetupListener() => GlobalMessenger<uint, AnimationType>.AddListener(EventNames.QSBChangeAnimType, Handler);
public override void CloseListener() => GlobalMessenger<AnimationType>.RemoveListener(EventNames.QSBChangeAnimType, Handler);
public override void CloseListener() => GlobalMessenger<uint, AnimationType>.RemoveListener(EventNames.QSBChangeAnimType, Handler);
private void Handler(AnimationType type) => SendEvent(CreateMessage(type));
private void Handler(uint player, AnimationType type) => SendEvent(CreateMessage(player, type));
private ChangeAnimTypeMessage CreateMessage(AnimationType type) => new ChangeAnimTypeMessage
private ChangeAnimTypeMessage CreateMessage(uint player, AnimationType type) => new ChangeAnimTypeMessage
{
AboutId = LocalPlayerId,
AboutId = player,
Type = type
};
public override void OnReceiveRemote(ChangeAnimTypeMessage message)
{
DebugLog.DebugWrite("Receive remote ChangeAnimType - " + message.Type);
DebugLog.DebugWrite($"ChangeAnimType for {message.AboutId} - {message.Type}");
QSBPlayerManager.GetPlayer(message.AboutId).Animator.SetAnimationType(message.Type);
}
}

View File

@ -1,6 +1,7 @@
using QSB.EventsCore;
using QSB.Messaging;
using QSB.Player;
using QSB.Utility;
namespace QSB.Animation
{
@ -31,12 +32,14 @@ namespace QSB.Animation
public override void OnReceiveRemote(ToggleMessage message)
{
DebugLog.DebugWrite($"remote suit {(message.ToggleValue ? "on" : "off")} player {message.AboutId}");
var player = QSBPlayerManager.GetPlayer(message.AboutId);
player?.UpdateState(State.Suit, message.ToggleValue);
}
public override void OnReceiveLocal(ToggleMessage message)
{
DebugLog.DebugWrite($"local suit {(message.ToggleValue ? "on" : "off")}");
QSBPlayerManager.LocalPlayer.UpdateState(State.Suit, message.ToggleValue);
var animator = QSBPlayerManager.LocalPlayer.Animator;
if (message.ToggleValue)

View File

@ -1,4 +1,5 @@
using QSB.Animation.Events;
using QSB.Utility;
using UnityEngine;
using UnityEngine.Networking;
using Object = UnityEngine.Object;
@ -338,39 +339,39 @@ namespace QSB.Animation
internal static void OnAnimationClientMessage(NetworkMessage netMsg)
{
netMsg.ReadMessage(AnimationMessage);
GameObject localObject = ClientScene.FindLocalObject(QSBNetworkAnimator.AnimationMessage.netId);
if ((Object)localObject == (Object)null)
var localObject = ClientScene.FindLocalObject(AnimationMessage.netId);
if (localObject == null)
return;
QSBNetworkAnimator component = localObject.GetComponent<QSBNetworkAnimator>();
if (!((Object)component != (Object)null))
var component = localObject.GetComponent<QSBNetworkAnimator>();
if (component == null)
return;
NetworkReader reader = new NetworkReader(QSBNetworkAnimator.AnimationMessage.parameters);
component.HandleAnimMsg(QSBNetworkAnimator.AnimationMessage, reader);
var reader = new NetworkReader(AnimationMessage.parameters);
component.HandleAnimMsg(AnimationMessage, reader);
}
internal static void OnAnimationParametersClientMessage(NetworkMessage netMsg)
{
netMsg.ReadMessage<QSBAnimationParametersMessage>(QSBNetworkAnimator.ParametersMessage);
GameObject localObject = ClientScene.FindLocalObject(QSBNetworkAnimator.ParametersMessage.netId);
if ((Object)localObject == (Object)null)
netMsg.ReadMessage(ParametersMessage);
var localObject = ClientScene.FindLocalObject(ParametersMessage.netId);
if (localObject == null)
return;
QSBNetworkAnimator component = localObject.GetComponent<QSBNetworkAnimator>();
if (!((Object)component != (Object)null))
var component = localObject.GetComponent<QSBNetworkAnimator>();
if (component == null)
return;
NetworkReader reader = new NetworkReader(QSBNetworkAnimator.ParametersMessage.parameters);
component.HandleAnimParamsMsg(QSBNetworkAnimator.ParametersMessage, reader);
var reader = new NetworkReader(ParametersMessage.parameters);
component.HandleAnimParamsMsg(ParametersMessage, reader);
}
internal static void OnAnimationTriggerClientMessage(NetworkMessage netMsg)
{
netMsg.ReadMessage<QSBAnimationTriggerMessage>(QSBNetworkAnimator.TriggersMessage);
GameObject localObject = ClientScene.FindLocalObject(QSBNetworkAnimator.TriggersMessage.netId);
if ((Object)localObject == (Object)null)
netMsg.ReadMessage(TriggersMessage);
var localObject = ClientScene.FindLocalObject(TriggersMessage.netId);
if (localObject == null)
return;
QSBNetworkAnimator component = localObject.GetComponent<QSBNetworkAnimator>();
if (!((Object)component != (Object)null))
var component = localObject.GetComponent<QSBNetworkAnimator>();
if (component == null)
return;
component.HandleAnimTriggerMsg(QSBNetworkAnimator.TriggersMessage.hash);
component.HandleAnimTriggerMsg(TriggersMessage.hash);
}
}
}

View File

@ -1,4 +1,5 @@
using QSB.Animation;
using QSB.EventsCore;
using QSB.Instruments.QSBCamera;
using QSB.Player;
using UnityEngine;
@ -36,6 +37,7 @@ namespace QSB.Instruments
public void SwitchToType(AnimationType type)
{
GlobalMessenger<uint, AnimationType>.FireEvent(EventNames.QSBChangeAnimType, QSBPlayerManager.LocalPlayerId, type);
QSBPlayerManager.LocalPlayer.Animator.SetAnimationType(type);
}
}

View File

@ -148,16 +148,15 @@ namespace QSB
QSBSectorManager.Instance.RebuildSectors();
OrbManager.Instance.QueueBuildSlots();
if (NetworkClient.active && !NetworkServer.active)
if (!NetworkServer.localClientActive)
{
QSBPatchManager.DoPatchType(QSBPatchTypes.OnNonServerClientConnect);
singleton.client.UnregisterHandler(40);
singleton.client.UnregisterHandler(41);
singleton.client.RegisterHandlerSafe(40, new NetworkMessageDelegate(QSBNetworkAnimator.OnAnimationClientMessage));
singleton.client.RegisterHandlerSafe(41, new NetworkMessageDelegate(QSBNetworkAnimator.OnAnimationParametersClientMessage));
}
singleton.client.UnregisterHandler(40);
singleton.client.UnregisterHandler(41);
singleton.client.UnregisterHandler(42);
singleton.client.RegisterHandlerSafe(40, new NetworkMessageDelegate(QSBNetworkAnimator.OnAnimationClientMessage));
singleton.client.RegisterHandlerSafe(41, new NetworkMessageDelegate(QSBNetworkAnimator.OnAnimationParametersClientMessage));
singleton.client.RegisterHandlerSafe(42, new NetworkMessageDelegate(QSBNetworkAnimator.OnAnimationTriggerClientMessage));
QSBPatchManager.DoPatchType(QSBPatchTypes.OnClientConnect);