quantum-space-buddies/QSB/Animation/Player/Patches/PlayerAnimationPatches.cs

149 lines
4.7 KiB
C#
Raw Normal View History

using HarmonyLib;
2021-12-25 23:03:23 -08:00
using QSB.Animation.Player.Messages;
using QSB.Messaging;
2021-04-24 00:10:29 +01:00
using QSB.Patches;
using QSB.Player;
using QSB.Utility;
2021-04-24 00:10:29 +01:00
using UnityEngine;
namespace QSB.Animation.Player.Patches
2021-04-24 00:10:29 +01:00
{
[HarmonyPatch]
2021-04-27 22:49:12 +01:00
internal class PlayerAnimationPatches : QSBPatch
2021-04-24 00:10:29 +01:00
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerAnimController), nameof(PlayerAnimController.LateUpdate))]
public static bool LateUpdateReplacement(
PlayerAnimController __instance,
PlayerCharacterController ____playerController,
ThrusterModel ____playerJetpack,
ref float ____ungroundedTime,
Animator ____animator,
ref bool ____justBecameGrounded,
ref bool ____justTookFallDamage,
ref bool ____leftFootGrounded,
ref bool ____rightFootGrounded,
ref bool ____rightArmHidden,
GameObject[] ____rightArmObjects,
int ____defaultLayer,
int ____probeOnlyLayer)
2021-04-24 00:10:29 +01:00
{
var isGrounded = ____playerController.IsGrounded();
var isAttached = PlayerState.IsAttached();
var isInZeroG = PlayerState.InZeroG();
var isFlying = ____playerJetpack.GetLocalAcceleration().y > 0f;
var movementVector = Vector3.zero;
if (!isAttached)
{
movementVector = ____playerController.GetRelativeGroundVelocity();
}
2021-06-18 22:38:32 +01:00
2021-04-24 00:10:29 +01:00
if (Mathf.Abs(movementVector.x) < 0.05f)
{
movementVector.x = 0f;
}
2021-06-18 22:38:32 +01:00
2021-04-24 00:10:29 +01:00
if (Mathf.Abs(movementVector.z) < 0.05f)
{
movementVector.z = 0f;
}
2021-06-18 22:38:32 +01:00
2021-04-24 00:10:29 +01:00
if (isFlying)
{
____ungroundedTime = Time.time;
}
2021-06-18 22:38:32 +01:00
2021-04-24 00:10:29 +01:00
var freefallMagnitude = 0f;
var timeInFreefall = 0f;
var lastGroundBody = ____playerController.GetLastGroundBody();
if (!isGrounded && !isAttached && !isInZeroG && lastGroundBody != null)
{
freefallMagnitude = (____playerController.GetAttachedOWRigidbody(false).GetVelocity() - lastGroundBody.GetPointVelocity(____playerController.transform.position)).magnitude;
timeInFreefall = Time.time - ____ungroundedTime;
}
2021-06-18 22:38:32 +01:00
2021-04-24 00:10:29 +01:00
____animator.SetFloat("RunSpeedX", movementVector.x / 3f);
____animator.SetFloat("RunSpeedY", movementVector.z / 3f);
____animator.SetFloat("TurnSpeed", ____playerController.GetTurning());
____animator.SetBool("Grounded", isGrounded || isAttached || PlayerState.IsRecentlyDetached());
2021-10-12 15:31:02 +01:00
____animator.SetLayerWeight(1, ____playerController.GetJumpCrouchFraction());
2021-04-24 00:10:29 +01:00
____animator.SetFloat("FreefallSpeed", freefallMagnitude / 15f * (timeInFreefall / 3f));
____animator.SetBool("InZeroG", isInZeroG || isFlying);
____animator.SetBool("UsingJetpack", isInZeroG && PlayerState.IsWearingSuit());
if (____justBecameGrounded)
{
var playerAnimationSync = QSBPlayerManager.LocalPlayer.AnimationSync;
if (____justTookFallDamage)
{
____animator.SetTrigger("LandHard");
2021-12-25 23:03:23 -08:00
new AnimationTriggerMessage(playerAnimationSync.AttachedNetId, "LandHard").Send();
2021-04-24 00:10:29 +01:00
}
else
{
____animator.SetTrigger("Land");
2021-12-25 23:03:23 -08:00
new AnimationTriggerMessage(playerAnimationSync.AttachedNetId, "Land").Send();
2021-04-24 00:10:29 +01:00
}
}
2021-06-18 22:38:32 +01:00
2021-04-24 00:10:29 +01:00
if (isGrounded)
{
var leftFootLift = ____animator.GetFloat("LeftFootLift");
if (!____leftFootGrounded && leftFootLift < 0.333f)
{
____leftFootGrounded = true;
__instance.RaiseEvent("OnLeftFootGrounded");
2021-04-24 00:10:29 +01:00
}
else if (____leftFootGrounded && leftFootLift > 0.666f)
{
____leftFootGrounded = false;
2021-07-07 09:02:23 +01:00
__instance.RaiseEvent("OnLeftFootLift");
2021-04-24 00:10:29 +01:00
}
2021-06-18 22:38:32 +01:00
2021-04-24 00:10:29 +01:00
var rightFootLift = ____animator.GetFloat("RightFootLift");
if (!____rightFootGrounded && rightFootLift < 0.333f)
{
____rightFootGrounded = true;
2021-07-07 09:02:23 +01:00
__instance.RaiseEvent("OnRightFootGrounded");
2021-04-24 00:10:29 +01:00
}
else if (____rightFootGrounded && rightFootLift > 0.666f)
{
____rightFootGrounded = false;
2021-07-07 09:02:23 +01:00
__instance.RaiseEvent("OnRightFootLift");
2021-04-24 00:10:29 +01:00
}
}
2021-06-18 22:38:32 +01:00
2021-04-24 00:10:29 +01:00
____justBecameGrounded = false;
____justTookFallDamage = false;
var usingTool = Locator.GetToolModeSwapper().GetToolMode() != ToolMode.None;
if ((usingTool && !____rightArmHidden) || (!usingTool && ____rightArmHidden))
{
____rightArmHidden = usingTool;
for (var i = 0; i < ____rightArmObjects.Length; i++)
{
____rightArmObjects[i].layer = (!____rightArmHidden) ? ____defaultLayer : ____probeOnlyLayer;
}
}
return false;
}
2021-12-14 23:31:44 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerAnimController), nameof(PlayerAnimController.OnPlayerJump))]
public static bool OnPlayerJumpReplacement(PlayerAnimController __instance)
{
__instance._ungroundedTime = Time.time;
if (!__instance.isActiveAndEnabled)
{
return false;
}
__instance._animator.SetTrigger("Jump");
var playerAnimationSync = QSBPlayerManager.LocalPlayer.AnimationSync;
2021-12-25 23:03:23 -08:00
new AnimationTriggerMessage(playerAnimationSync.AttachedNetId, "Jump").Send();
2021-12-14 23:31:44 +00:00
return false;
}
2021-04-24 00:10:29 +01:00
}
}