Enemies base
This commit is contained in:
1 parent
cb05da5e89
commit
b02e201cd0
532 files changed
+60713
-58412
No files matched your search
@@ -52,8 +52,14 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
public int FullPathHash;
|
||||
public float NormalizedTime;
|
||||
public float LayerWeight;
|
||||
|
||||
// Данные на случай, если снапшот сделан посреди CrossFade-перехода:
|
||||
public bool IsInTransition;
|
||||
public int NextFullPathHash;
|
||||
public float NextNormalizedTime;
|
||||
public float RemainingTransitionDuration; // в секундах
|
||||
}
|
||||
|
||||
|
||||
public struct ParameterSnapshot
|
||||
{
|
||||
public int NameHash;
|
||||
@@ -62,56 +68,76 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
public int IntValue;
|
||||
public bool BoolValue;
|
||||
}
|
||||
|
||||
|
||||
private LayerStateSnapshot[] _layers;
|
||||
private ParameterSnapshot[] _parameters;
|
||||
|
||||
private Vector3 _localPosition;
|
||||
private Quaternion _localRotation;
|
||||
private float _speed;
|
||||
|
||||
|
||||
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;
|
||||
|
||||
|
||||
int layerCount = animator.layerCount;
|
||||
if (_layers == null || _layers.Length != layerCount)
|
||||
_layers = new LayerStateSnapshot[layerCount];
|
||||
|
||||
|
||||
for (int i = 0; i < layerCount; i++)
|
||||
{
|
||||
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(i);
|
||||
_layers[i] = new LayerStateSnapshot
|
||||
bool inTransition = animator.IsInTransition(i);
|
||||
|
||||
var layerSnap = new LayerStateSnapshot
|
||||
{
|
||||
FullPathHash = stateInfo.fullPathHash,
|
||||
NormalizedTime = stateInfo.normalizedTime,
|
||||
LayerWeight = animator.GetLayerWeight(i)
|
||||
LayerWeight = animator.GetLayerWeight(i),
|
||||
IsInTransition = inTransition
|
||||
};
|
||||
|
||||
Debug.Log($"Saved animator state {stateInfo.shortNameHash}");
|
||||
|
||||
if (inTransition)
|
||||
{
|
||||
AnimatorStateInfo nextInfo = animator.GetNextAnimatorStateInfo(i);
|
||||
AnimatorTransitionInfo transInfo = animator.GetAnimatorTransitionInfo(i);
|
||||
|
||||
layerSnap.NextFullPathHash = nextInfo.fullPathHash;
|
||||
layerSnap.NextNormalizedTime = nextInfo.normalizedTime;
|
||||
|
||||
// Вычисляем оставшееся время перехода в секундах:
|
||||
float totalTransSeconds = transInfo.durationUnit == DurationUnit.Fixed
|
||||
? transInfo.duration
|
||||
: transInfo.duration * stateInfo.length;
|
||||
|
||||
layerSnap.RemainingTransitionDuration = Mathf.Max(0f, totalTransSeconds * (1f - transInfo.normalizedTime));
|
||||
}
|
||||
|
||||
_layers[i] = layerSnap;
|
||||
}
|
||||
|
||||
|
||||
int paramCount = animator.parameterCount;
|
||||
if (_parameters == null || _parameters.Length != paramCount)
|
||||
_parameters = new ParameterSnapshot[paramCount];
|
||||
|
||||
|
||||
for (int i = 0; i < paramCount; i++)
|
||||
{
|
||||
AnimatorControllerParameter param = animator.GetParameter(i);
|
||||
int hash = param.nameHash;
|
||||
var paramSnapshot = new ParameterSnapshot { NameHash = hash, Type = param.type };
|
||||
|
||||
|
||||
switch (param.type)
|
||||
{
|
||||
case AnimatorControllerParameterType.Float:
|
||||
@@ -128,17 +154,17 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
_parameters[i] = paramSnapshot;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void RestoreTo(Animator targetAnimator)
|
||||
{
|
||||
if (targetAnimator == null || _layers == null) return;
|
||||
|
||||
|
||||
targetAnimator.Rebind();
|
||||
|
||||
|
||||
bool prevRootMotion = targetAnimator.applyRootMotion;
|
||||
targetAnimator.applyRootMotion = false;
|
||||
targetAnimator.speed = _speed;
|
||||
|
||||
|
||||
if (_parameters != null)
|
||||
{
|
||||
for (int i = 0; i < _parameters.Length; i++)
|
||||
@@ -146,7 +172,7 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
var p = _parameters[i];
|
||||
if (targetAnimator.IsParameterControlledByCurve(p.NameHash))
|
||||
continue;
|
||||
|
||||
|
||||
switch (p.Type)
|
||||
{
|
||||
case AnimatorControllerParameterType.Float:
|
||||
@@ -165,21 +191,35 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int layerCount = Mathf.Min(_layers.Length, targetAnimator.layerCount);
|
||||
for (int i = 0; i < layerCount; i++)
|
||||
{
|
||||
var layer = _layers[i];
|
||||
targetAnimator.SetLayerWeight(i, layer.LayerWeight);
|
||||
|
||||
// 1. Ставим исходный стейт
|
||||
targetAnimator.Play(layer.FullPathHash, i, layer.NormalizedTime);
|
||||
targetAnimator.Update(0f);
|
||||
|
||||
// 2. Если в момент снапшота шёл переход (например, в атаку) —
|
||||
// запускаем CrossFade в целевой стейт с того места, где он остановился!
|
||||
if (layer.IsInTransition && layer.NextFullPathHash != 0)
|
||||
{
|
||||
targetAnimator.CrossFade(
|
||||
layer.NextFullPathHash,
|
||||
layer.RemainingTransitionDuration, // у CrossFadeInFixedTime offset в секундах, а у CrossFade с фиксированным временем проще через CrossFadeInFixedTime:
|
||||
i,
|
||||
layer.NextNormalizedTime);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
targetAnimator.Update(0f);
|
||||
|
||||
|
||||
Transform targetTransform = targetAnimator.transform;
|
||||
targetTransform.localPosition = _localPosition;
|
||||
targetTransform.localRotation = _localRotation;
|
||||
|
||||
|
||||
targetAnimator.applyRootMotion = prevRootMotion;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user