291 lines
12 KiB
C#
291 lines
12 KiB
C#
using Code.OfficeGame.BattleMode.Health;
|
|
using KinematicCharacterController;
|
|
using UnityEngine;
|
|
|
|
namespace Code.OfficeGame.BattleMode.Actions
|
|
{
|
|
public struct BonePoseSnapshot
|
|
{
|
|
public Vector3[] LocalPositions;
|
|
public Quaternion[] LocalRotations;
|
|
|
|
public static BonePoseSnapshot Capture(Transform[] bones)
|
|
{
|
|
if (bones == null || bones.Length == 0)
|
|
return default;
|
|
|
|
int count = bones.Length;
|
|
var pos = new Vector3[count];
|
|
var rot = new Quaternion[count];
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
pos[i] = bones[i].localPosition;
|
|
rot[i] = bones[i].localRotation;
|
|
}
|
|
|
|
return new BonePoseSnapshot
|
|
{
|
|
LocalPositions = pos,
|
|
LocalRotations = rot
|
|
};
|
|
}
|
|
|
|
public readonly void ApplyTo(Transform[] bones)
|
|
{
|
|
if (bones == null || LocalPositions == null) return;
|
|
|
|
int count = Mathf.Min(bones.Length, LocalPositions.Length);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
bones[i].localPosition = LocalPositions[i];
|
|
bones[i].localRotation = LocalRotations[i];
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct EntitySnapshot
|
|
{
|
|
// --- Уровень 1: Визуальный (для O(1) скраббинга ползунком) ---
|
|
public Vector3 Position;
|
|
public Quaternion Rotation;
|
|
public BonePoseSnapshot BonePose;
|
|
public HealthData CurrentHealth;
|
|
|
|
// --- Уровень 2: Полное состояние симуляции (для BakeFrom) ---
|
|
public KinematicCharacterMotorState MotorState;
|
|
public bool IsFrozen;
|
|
public Vector3 MoveInputVector;
|
|
public Vector3 LookInputVector;
|
|
public Vector3 InternalVelocityAdd;
|
|
public AnimatorSnapshot AnimatorSnapshot;
|
|
|
|
public static EntitySnapshot GetSnapshot(EntityProperties entity, Transform[] cachedBones)
|
|
{
|
|
var motor = entity.KinematicCharacterMotor;
|
|
var moveCtrl = entity.MovementController;
|
|
|
|
return new EntitySnapshot
|
|
{
|
|
Position = motor.TransientPosition,
|
|
Rotation = motor.TransientRotation,
|
|
BonePose = BonePoseSnapshot.Capture(cachedBones),
|
|
CurrentHealth = entity.EntityHealth != null ? entity.EntityHealth.HealthData.CurrentValue : default,
|
|
|
|
MotorState = motor.GetState(),
|
|
IsFrozen = moveCtrl.Freeze,
|
|
MoveInputVector = moveCtrl.MoveInputVector,
|
|
LookInputVector = moveCtrl.LookInputVector,
|
|
InternalVelocityAdd = moveCtrl.InternalVelocityAdd,
|
|
AnimatorSnapshot = AnimatorSnapshot.GetSnapshot(entity.animator)
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Мгновенное O(1) восстановление визуала для ползунка и воспроизведения ленты.
|
|
/// Не трогает Mecanim и не мутирует EntityHealth.
|
|
/// </summary>
|
|
public static void ApplyVisualOnly(EntityProperties entity, Transform[] cachedBones, in EntitySnapshot snapshot)
|
|
{
|
|
entity.KinematicCharacterMotor.SetPositionAndRotation(
|
|
snapshot.Position,
|
|
snapshot.Rotation,
|
|
bypassInterpolation: true);
|
|
|
|
snapshot.BonePose.ApplyTo(cachedBones);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Полное детерминированное восстановление для старта/перезапекания физической симуляции.
|
|
/// </summary>
|
|
public static void RestoreForSimulation(EntityProperties entity, Transform[] cachedBones, in EntitySnapshot snapshot)
|
|
{
|
|
var motor = entity.KinematicCharacterMotor;
|
|
var moveCtrl = entity.MovementController;
|
|
|
|
moveCtrl.Freeze = snapshot.IsFrozen;
|
|
moveCtrl.MoveInputVector = snapshot.MoveInputVector;
|
|
moveCtrl.LookInputVector = snapshot.LookInputVector;
|
|
moveCtrl.InternalVelocityAdd = snapshot.InternalVelocityAdd;
|
|
|
|
motor.ApplyState(snapshot.MotorState, bypassInterpolation: true);
|
|
|
|
if (entity.EntityHealth != null)
|
|
entity.EntityHealth.SetHealthData(snapshot.CurrentHealth);
|
|
|
|
snapshot.AnimatorSnapshot.RestoreTo(entity.animator);
|
|
snapshot.BonePose.ApplyTo(cachedBones);
|
|
}
|
|
}
|
|
|
|
public struct AnimatorSnapshot
|
|
{
|
|
public struct LayerStateSnapshot
|
|
{
|
|
public int FullPathHash;
|
|
public float NormalizedTime;
|
|
public float StateLength;
|
|
public float LayerWeight;
|
|
|
|
public bool IsInTransition;
|
|
public int NextFullPathHash;
|
|
public float NextNormalizedTime;
|
|
public float NextStateLength;
|
|
public float TotalTransitionDuration;
|
|
public float ElapsedTransitionTime;
|
|
}
|
|
|
|
public struct ParameterSnapshot
|
|
{
|
|
public int NameHash;
|
|
public AnimatorControllerParameterType Type;
|
|
public float FloatValue;
|
|
public int IntValue;
|
|
public bool BoolValue;
|
|
}
|
|
|
|
private LayerStateSnapshot[] _layers;
|
|
private ParameterSnapshot[] _parameters;
|
|
private Vector3 _localPosition;
|
|
private Quaternion _localRotation;
|
|
private float _speed;
|
|
private bool _applyRootMotion;
|
|
|
|
public static AnimatorSnapshot GetSnapshot(Animator animator)
|
|
{
|
|
var snapshot = new AnimatorSnapshot();
|
|
snapshot.CaptureFrom(animator);
|
|
return snapshot;
|
|
}
|
|
|
|
public void CaptureFrom(Animator animator)
|
|
{
|
|
if (animator == null) return;
|
|
|
|
Transform animTransform = animator.transform;
|
|
_localPosition = animTransform.localPosition;
|
|
_localRotation = animTransform.localRotation;
|
|
_speed = animator.speed;
|
|
_applyRootMotion = animator.applyRootMotion;
|
|
|
|
int layerCount = animator.layerCount;
|
|
_layers = new LayerStateSnapshot[layerCount];
|
|
|
|
for (int i = 0; i < layerCount; i++)
|
|
{
|
|
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(i);
|
|
bool inTransition = animator.IsInTransition(i);
|
|
|
|
var layerSnap = new LayerStateSnapshot
|
|
{
|
|
FullPathHash = stateInfo.fullPathHash,
|
|
NormalizedTime = stateInfo.normalizedTime,
|
|
StateLength = Mathf.Max(0.0001f, stateInfo.length),
|
|
LayerWeight = animator.GetLayerWeight(i),
|
|
IsInTransition = inTransition
|
|
};
|
|
|
|
if (inTransition)
|
|
{
|
|
AnimatorStateInfo nextInfo = animator.GetNextAnimatorStateInfo(i);
|
|
AnimatorTransitionInfo transInfo = animator.GetAnimatorTransitionInfo(i);
|
|
|
|
float totalTransSeconds = transInfo.durationUnit == DurationUnit.Fixed
|
|
? transInfo.duration
|
|
: transInfo.duration * stateInfo.length;
|
|
|
|
layerSnap.NextFullPathHash = nextInfo.fullPathHash;
|
|
layerSnap.NextNormalizedTime = nextInfo.normalizedTime;
|
|
layerSnap.NextStateLength = Mathf.Max(0.0001f, nextInfo.length);
|
|
layerSnap.TotalTransitionDuration = Mathf.Max(0.0001f, totalTransSeconds);
|
|
layerSnap.ElapsedTransitionTime = layerSnap.TotalTransitionDuration * transInfo.normalizedTime;
|
|
}
|
|
|
|
_layers[i] = layerSnap;
|
|
}
|
|
|
|
int paramCount = animator.parameterCount;
|
|
_parameters = new ParameterSnapshot[paramCount];
|
|
|
|
for (int i = 0; i < paramCount; i++)
|
|
{
|
|
AnimatorControllerParameter param = animator.GetParameter(i);
|
|
int hash = param.nameHash;
|
|
var pSnap = new ParameterSnapshot { NameHash = hash, Type = param.type };
|
|
|
|
switch (param.type)
|
|
{
|
|
case AnimatorControllerParameterType.Float: pSnap.FloatValue = animator.GetFloat(hash); break;
|
|
case AnimatorControllerParameterType.Int: pSnap.IntValue = animator.GetInteger(hash); break;
|
|
case AnimatorControllerParameterType.Bool:
|
|
case AnimatorControllerParameterType.Trigger: pSnap.BoolValue = animator.GetBool(hash); break;
|
|
}
|
|
_parameters[i] = pSnap;
|
|
}
|
|
}
|
|
|
|
public readonly void RestoreTo(Animator targetAnimator)
|
|
{
|
|
if (targetAnimator == null || _layers == null) return;
|
|
|
|
targetAnimator.applyRootMotion = false;
|
|
targetAnimator.speed = _speed;
|
|
|
|
if (_parameters != null)
|
|
{
|
|
for (int i = 0; i < _parameters.Length; i++)
|
|
{
|
|
var p = _parameters[i];
|
|
if (targetAnimator.IsParameterControlledByCurve(p.NameHash)) continue;
|
|
|
|
switch (p.Type)
|
|
{
|
|
case AnimatorControllerParameterType.Float: targetAnimator.SetFloat(p.NameHash, p.FloatValue); break;
|
|
case AnimatorControllerParameterType.Int: targetAnimator.SetInteger(p.NameHash, p.IntValue); break;
|
|
case AnimatorControllerParameterType.Bool: targetAnimator.SetBool(p.NameHash, p.BoolValue); break;
|
|
case AnimatorControllerParameterType.Trigger:
|
|
if (p.BoolValue) targetAnimator.SetTrigger(p.NameHash);
|
|
else targetAnimator.ResetTrigger(p.NameHash);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
int layerCount = Mathf.Min(_layers.Length, targetAnimator.layerCount);
|
|
for (int i = 0; i < layerCount; i++)
|
|
{
|
|
var layer = _layers[i];
|
|
targetAnimator.SetLayerWeight(i, layer.LayerWeight);
|
|
|
|
if (!layer.IsInTransition || layer.NextFullPathHash == 0)
|
|
{
|
|
float timeInSeconds = Mathf.Max(0f, layer.NormalizedTime * layer.StateLength);
|
|
targetAnimator.CrossFadeInFixedTime(layer.FullPathHash, 0f, i, timeInSeconds);
|
|
targetAnimator.Update(0f);
|
|
}
|
|
else
|
|
{
|
|
// Защита от отрицательного времени (фикс бага старого AnimatorSnapshot)
|
|
float startSecondsA = Mathf.Max(0f, (layer.NormalizedTime * layer.StateLength) - layer.ElapsedTransitionTime);
|
|
float startSecondsB = Mathf.Max(0f, (layer.NextNormalizedTime * layer.NextStateLength) - layer.ElapsedTransitionTime);
|
|
|
|
targetAnimator.CrossFadeInFixedTime(layer.FullPathHash, 0f, i, startSecondsA);
|
|
targetAnimator.Update(0f);
|
|
|
|
targetAnimator.CrossFadeInFixedTime(
|
|
layer.NextFullPathHash,
|
|
layer.TotalTransitionDuration,
|
|
i,
|
|
startSecondsB);
|
|
|
|
targetAnimator.Update(layer.ElapsedTransitionTime);
|
|
}
|
|
}
|
|
|
|
Transform targetTransform = targetAnimator.transform;
|
|
targetTransform.localPosition = _localPosition;
|
|
targetTransform.localRotation = _localRotation;
|
|
targetAnimator.applyRootMotion = _applyRootMotion;
|
|
}
|
|
}
|
|
} |