Files
Cielonos/Assets/Scripts/MainGame/Characters/Base/Subcontrollers/AnimationSubcontrollerBase.cs

290 lines
12 KiB
C#
Raw Normal View History

2025-11-25 08:19:33 -05:00
using System;
using System.Collections.Generic;
using System.Linq;
using SLSFramework.General;
using SLSUtilities.FunctionalAnimation;
using UnityEngine;
using UnityEngine.Serialization;
2025-12-17 04:19:38 -05:00
using Random = UnityEngine.Random;
2025-11-25 08:19:33 -05:00
namespace Cielonos.MainGame.Characters
{
public partial class AnimationSubcontrollerBase : SubcontrollerBase<CharacterBase>
{
public Animator animator;
public Dictionary<DisruptionType, bool> disruptionStatus;
public bool isDuringRootMotion;
public bool isDisablingMoveXZ;
public bool isDisablingMoveY;
public BaseAnimationGroup defaultAnimationGroup;
public FunctionalAnimationSubmodule fullBodyFuncAnimSm;
public Dictionary<string, Action<RuntimeFuncAnim>> registeredFunctions;
public List<FuncAnimInterval> lastFrameIntervals { get; private set; }
public List<FuncAnimInterval> currentIntervals { get; private set; }
public override void Initialize()
{
base.Initialize();
fullBodyFuncAnimSm = new FunctionalAnimationSubmodule(this, "FullBodyAction");
registeredFunctions = new Dictionary<string, Action<RuntimeFuncAnim>>();
defaultAnimationGroup?.SetUp(this);
disruptionStatus = new Dictionary<DisruptionType, bool>()
{
{ DisruptionType.NormalExternal, false },
{ DisruptionType.NormalAction, false },
{ DisruptionType.Movement, false }
};
lastFrameIntervals = new List<FuncAnimInterval>();
currentIntervals = new List<FuncAnimInterval>();
RegisterDefaultFunctions();
}
protected virtual void Update()
{
2025-12-08 05:27:53 -05:00
if (owner.statusSm.isDead)
{
return;
}
2025-11-25 08:19:33 -05:00
fullBodyFuncAnimSm?.UpdateTime();
UpdateIntervalInfo();
}
protected virtual void LateUpdate()
{
fullBodyFuncAnimSm?.UpdateEvents();
2025-12-17 04:19:38 -05:00
BoneShakeLateUpdate();
2025-11-25 08:19:33 -05:00
}
}
public partial class AnimationSubcontrollerBase
{
protected virtual void UpdateIntervalInfo()
{
if (fullBodyFuncAnimSm?.currentRuntimeFuncAnim == null)
{
isDuringRootMotion = false;
return;
}
lastFrameIntervals = currentIntervals;
currentIntervals = fullBodyFuncAnimSm.currentRuntimeFuncAnim.GetEnablingIntervals();
disruptionStatus[DisruptionType.NormalExternal] = currentIntervals.Any(interval => interval.intervalType == IntervalType.ExternalDisruption);
disruptionStatus[DisruptionType.NormalAction] = currentIntervals.Any(interval => interval.intervalType == IntervalType.ActionDisruption);
disruptionStatus[DisruptionType.Movement] = currentIntervals.Any(interval => interval.intervalType == IntervalType.MovementDisruption);
isDuringRootMotion = currentIntervals.Any(interval => interval.intervalType == IntervalType.RootMotion);
}
}
2025-12-17 04:19:38 -05:00
public partial class AnimationSubcontrollerBase
{
private class BoneShakeState
{
public Transform bone;
public Vector3 shakeAxis; // 震动轴
// 物理变量
public float currentAngle = 0f; // 当前偏离角度 (位移 x)
public float velocity = 0f; // 当前震动速度 (速度 v)
}
[Header("Bone Shake Settings")]
// 刚度:越大越硬,回弹越快。对于重型机甲,建议 150-300轻型无人机建议 80-150。
public float stiffness = 200f;
// 阻尼:越大停得越快。建议 10-20。如果太小机器人会像果冻一样晃。
public float damping = 15f;
// 冲击力倍率:将受击力度转化为弹簧的初始速度
public float impactForceMultiplier = 500f;
public List<Transform> testShakeBones; // 用于测试的骨骼列表
private List<BoneShakeState> activeShakes = new List<BoneShakeState>();
private void BoneShakeLateUpdate()
{
float dt = Time.deltaTime;
for (int i = activeShakes.Count - 1; i >= 0; i--)
{
var state = activeShakes[i];
if (state.bone == null) {
activeShakes.RemoveAt(i);
continue;
}
// --- 核心:阻尼弹簧物理公式 (Hooke's Law + Damping) ---
// F = -k * x - d * v
// force = -stiffness * displacement - damping * velocity
float force = -stiffness * state.currentAngle - damping * state.velocity;
// a = F / m (假设质量为1简化计算)
// v += a * dt
state.velocity += force * dt;
// x += v * dt
state.currentAngle += state.velocity * dt;
// --- 应用旋转 ---
// 将计算出的角度应用到轴向上
Quaternion shakeRot = Quaternion.AngleAxis(state.currentAngle, state.shakeAxis);
state.bone.localRotation = state.bone.localRotation * shakeRot;
// --- 移除条件 ---
// 当能量非常小速度和位移都接近0时移除节省性能
if (Mathf.Abs(state.currentAngle) < 0.1f && Mathf.Abs(state.velocity) < 0.1f)
{
activeShakes.RemoveAt(i);
}
}
}
public void ApplyBoneShake(Transform hitBone, Vector3 hitDirection, float intensity)
{
var state = activeShakes.Find(x => x.bone == hitBone);
if (state == null)
{
state = new BoneShakeState();
state.bone = hitBone;
activeShakes.Add(state);
}
// 机械特质:受击瞬间不是直接设置位移,而是给予一个巨大的“初速度” (Impulse)
// 这会让骨骼瞬间弹出去,然后被弹簧拉回来,非常有力量感
state.velocity += intensity * impactForceMultiplier;
// 计算震动轴:依旧是垂直于攻击方向
Vector3 axis = Vector3.Cross(hitDirection, Vector3.up).normalized;
if (axis == Vector3.zero) axis = Vector3.right;
state.shakeAxis = axis;
}
}
2025-11-25 08:19:33 -05:00
public partial class AnimationSubcontrollerBase
{
public virtual void RegisterDefaultFunctions()
{
2025-12-08 05:27:53 -05:00
registeredFunctions.Add("SetRootMotionMoveXMultiplier", anim =>
{
owner.movementSc.rootMotionMoveXMultiplier = anim.runtimeVariables.GetVariable<float>("RootMotionMoveXMultiplier");
});
registeredFunctions.Add("SetRootMotionMoveYMultiplier", anim =>
{
owner.movementSc.rootMotionMoveYMultiplier = anim.runtimeVariables.GetVariable<float>("SetRootMotionMoveYMultiplier");
});
2025-11-25 08:19:33 -05:00
2025-12-08 05:27:53 -05:00
registeredFunctions.Add("SetRootMotionMoveZMultiplier", anim =>
{
owner.movementSc.rootMotionMoveZMultiplier = anim.runtimeVariables.GetVariable<float>("SetRootMotionMoveZMultiplier");
});
2025-11-25 08:19:33 -05:00
}
}
public partial class AnimationSubcontrollerBase
{
public virtual bool SetGetHitDisruption(DisruptionType disruptionType, BreakthroughType breakthroughType)
{
2025-12-08 05:27:53 -05:00
if (owner.reactionSc.breakthroughResistances[breakthroughType])
2025-11-25 08:19:33 -05:00
{
return false;
}
if (fullBodyFuncAnimSm.Stop(disruptionType))
{
return true;
}
return false;
}
2025-12-17 04:19:38 -05:00
public virtual void PlayGetHitBoneShake(float intensity, Vector3 direction = default)
2025-11-25 08:19:33 -05:00
{
2025-12-17 04:19:38 -05:00
if (direction == default)
{
direction = owner.transform.right;
}
else
{
direction = Quaternion.Euler(0, 90, 0) * direction;
}
2025-11-25 08:19:33 -05:00
2025-12-17 04:19:38 -05:00
foreach (Transform bone in testShakeBones)
2025-11-25 08:19:33 -05:00
{
2025-12-17 04:19:38 -05:00
ApplyBoneShake(bone, direction, intensity);
}
}
2025-11-25 08:19:33 -05:00
2025-12-17 04:19:38 -05:00
protected virtual void PlayGetHitAnimation(string getHitAnimPrefix, out float animDuration, Vector3 direction = default)
{
int fullBodyActionIndex = animator.GetLayerIndex("FullBodyAction");
string getHitFrontAnim = getHitAnimPrefix + "Front";
string getHitAnim = getHitAnimPrefix + "Front";
float normalizedTransitionDuration = 0.1f / animator.GetCurrentAnimatorStateInfo(fullBodyActionIndex).length;
animDuration = 0f;
if (animator.HasState(fullBodyActionIndex, Animator.StringToHash(getHitAnim)))
{
2025-11-25 08:19:33 -05:00
direction.y = 0;
direction = direction.normalized;
float angle = Vector3.SignedAngle(transform.forward, direction, Vector3.up);
2025-12-17 04:19:38 -05:00
string directionStr = angle switch
2025-11-25 08:19:33 -05:00
{
2025-12-17 04:19:38 -05:00
> -45f and <= 45f => "Back",
> 45f and <= 135f => "Left",
> -135f and <= -45f => "Right",
_ => "Front"
};
getHitAnim = getHitAnimPrefix + directionStr;
if (direction == default || !animator.HasState(fullBodyActionIndex, Animator.StringToHash(getHitAnim)))
2025-11-25 08:19:33 -05:00
{
2025-12-17 04:19:38 -05:00
animator.CrossFade(getHitFrontAnim, normalizedTransitionDuration, fullBodyActionIndex, 0);
2025-11-25 08:19:33 -05:00
}
else
{
2025-12-17 04:19:38 -05:00
animator.CrossFade(getHitAnim, normalizedTransitionDuration, fullBodyActionIndex, 0);
2025-11-25 08:19:33 -05:00
}
2025-12-17 04:19:38 -05:00
animDuration = animator.GetCurrentAnimatorStateInfo(fullBodyActionIndex).length;
}
else if (animator.HasState(fullBodyActionIndex, Animator.StringToHash(getHitAnimPrefix)))
{
getHitAnim = getHitAnimPrefix;
animator.CrossFade(getHitAnim, normalizedTransitionDuration, fullBodyActionIndex, 0);
animDuration = animator.GetCurrentAnimatorStateInfo(fullBodyActionIndex).length;
2025-11-25 08:19:33 -05:00
}
else
{
if (animator.HasState(fullBodyActionIndex, Animator.StringToHash("GetHit")))
{
2025-12-17 04:19:38 -05:00
animator.CrossFade("GetHit", normalizedTransitionDuration, fullBodyActionIndex, 0);
animDuration = animator.GetCurrentAnimatorStateInfo(fullBodyActionIndex).length;
2025-11-25 08:19:33 -05:00
}
else
{
animator.CrossFade("Empty", 0f, fullBodyActionIndex, 0);
}
}
}
2025-12-17 04:19:38 -05:00
public virtual void PlayGetHitMediumAnimation(out float animDuration, Vector3 direction = default)
{
PlayGetHitAnimation("GetHitMedium", out animDuration, direction);
}
public virtual void PlayGetHitHeavyAnimation(out float animDuration, Vector3 direction = default)
{
PlayGetHitAnimation("GetHitHeavy", out animDuration, direction);
}
public virtual void PlayGetHitDisruptionAnimation(out float animDuration, Vector3 direction = default)
{
PlayGetHitAnimation("GetHitDisruption", out animDuration, direction);
}
2025-11-25 08:19:33 -05:00
}
}