Great timeline system (Age of AI WON)
This commit is contained in:
1 parent
2ce0391c16
commit
e312131725
182 files changed
+380070
-470221
No files matched your search
@@ -1,27 +1,66 @@
|
||||
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. KCC Motor
|
||||
// --- Уровень 1: Визуальный (для O(1) скраббинга ползунком) ---
|
||||
public Vector3 Position;
|
||||
public Quaternion Rotation;
|
||||
public Vector3 BaseVelocity;
|
||||
public BonePoseSnapshot BonePose;
|
||||
public HealthData CurrentHealth;
|
||||
|
||||
// 2. MovementController
|
||||
// --- Уровень 2: Полное состояние симуляции (для BakeFrom) ---
|
||||
public KinematicCharacterMotorState MotorState;
|
||||
public bool IsFrozen;
|
||||
public Vector3 MoveInputVector;
|
||||
public Vector3 LookInputVector;
|
||||
|
||||
// 3. Здоровье
|
||||
public HealthData CurrentHealth;
|
||||
|
||||
// 4. Аниматор
|
||||
public Vector3 InternalVelocityAdd;
|
||||
public AnimatorSnapshot AnimatorSnapshot;
|
||||
|
||||
public static EntitySnapshot GetSnapshot(EntityProperties entity)
|
||||
public static EntitySnapshot GetSnapshot(EntityProperties entity, Transform[] cachedBones)
|
||||
{
|
||||
var motor = entity.KinematicCharacterMotor;
|
||||
var moveCtrl = entity.MovementController;
|
||||
@@ -30,49 +69,52 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
{
|
||||
Position = motor.TransientPosition,
|
||||
Rotation = motor.TransientRotation,
|
||||
BaseVelocity = motor.BaseVelocity,
|
||||
BonePose = BonePoseSnapshot.Capture(cachedBones),
|
||||
CurrentHealth = entity.EntityHealth != null ? entity.EntityHealth.HealthData.CurrentValue : default,
|
||||
|
||||
MotorState = motor.GetState(),
|
||||
IsFrozen = moveCtrl.Freeze,
|
||||
// Сохраните текущие входы контроллера (добавьте публичные геттеры в MovementController, если они приватные)
|
||||
MoveInputVector = moveCtrl.MoveInputVector,
|
||||
LookInputVector = moveCtrl.LookInputVector,
|
||||
|
||||
CurrentHealth = entity.EntityHealth != null ? entity.EntityHealth.HealthData.CurrentValue : default,
|
||||
InternalVelocityAdd = moveCtrl.InternalVelocityAdd,
|
||||
AnimatorSnapshot = AnimatorSnapshot.GetSnapshot(entity.animator)
|
||||
};
|
||||
}
|
||||
|
||||
public static void RestoreSnapshot(EntityProperties entity, EntitySnapshot snapshot)
|
||||
/// <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;
|
||||
|
||||
// 1. Восстанавливаем MovementController ДО шага KCC
|
||||
moveCtrl.Freeze = snapshot.IsFrozen;
|
||||
var restoredInputs = new Code.Game.Gameplay.Player.AICharacterInputs
|
||||
{
|
||||
MoveVector = snapshot.MoveInputVector,
|
||||
Look = snapshot.LookInputVector.sqrMagnitude > 0.0001f
|
||||
? Quaternion.LookRotation(snapshot.LookInputVector, motor.CharacterUp)
|
||||
: snapshot.Rotation
|
||||
};
|
||||
moveCtrl.SetInputs(ref restoredInputs);
|
||||
moveCtrl.MoveInputVector = snapshot.MoveInputVector;
|
||||
moveCtrl.LookInputVector = snapshot.LookInputVector;
|
||||
moveCtrl.InternalVelocityAdd = snapshot.InternalVelocityAdd;
|
||||
|
||||
// 2. Восстанавливаем капсулу KCC (bypassInterpolation = true сбрасывает внутренние буферы перемещения)
|
||||
motor.SetPositionAndRotation(snapshot.Position, snapshot.Rotation, bypassInterpolation: true);
|
||||
motor.BaseVelocity = snapshot.BaseVelocity;
|
||||
motor.ApplyState(snapshot.MotorState, bypassInterpolation: true);
|
||||
|
||||
// 3. Восстанавливаем здоровье (без вызова анимаций смерти)
|
||||
if (entity.EntityHealth != null)
|
||||
{
|
||||
entity.EntityHealth.SetHealthData(snapshot.CurrentHealth);
|
||||
}
|
||||
|
||||
// 4. Восстанавливаем точную позу и фазу перехода Animator
|
||||
snapshot.AnimatorSnapshot.RestoreTo(entity.animator);
|
||||
|
||||
// 5. Мгновенно синхронизируем коллайдеры костей в PhysX
|
||||
Physics.SyncTransforms();
|
||||
snapshot.BonePose.ApplyTo(cachedBones);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,8 +197,8 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
layerSnap.NextFullPathHash = nextInfo.fullPathHash;
|
||||
layerSnap.NextNormalizedTime = nextInfo.normalizedTime;
|
||||
layerSnap.NextStateLength = Mathf.Max(0.0001f, nextInfo.length);
|
||||
layerSnap.TotalTransitionDuration = totalTransSeconds;
|
||||
layerSnap.ElapsedTransitionTime = totalTransSeconds * transInfo.normalizedTime;
|
||||
layerSnap.TotalTransitionDuration = Mathf.Max(0.0001f, totalTransSeconds);
|
||||
layerSnap.ElapsedTransitionTime = layerSnap.TotalTransitionDuration * transInfo.normalizedTime;
|
||||
}
|
||||
|
||||
_layers[i] = layerSnap;
|
||||
@@ -182,7 +224,7 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
}
|
||||
}
|
||||
|
||||
public void RestoreTo(Animator targetAnimator)
|
||||
public readonly void RestoreTo(Animator targetAnimator)
|
||||
{
|
||||
if (targetAnimator == null || _layers == null) return;
|
||||
|
||||
@@ -217,29 +259,25 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
|
||||
if (!layer.IsInTransition || layer.NextFullPathHash == 0)
|
||||
{
|
||||
// CrossFadeInFixedTime с длительностью 0f гарантированно убивает любой старый переход без Rebind()!
|
||||
float timeInSeconds = layer.NormalizedTime * layer.StateLength;
|
||||
float timeInSeconds = Mathf.Max(0f, layer.NormalizedTime * layer.StateLength);
|
||||
targetAnimator.CrossFadeInFixedTime(layer.FullPathHash, 0f, i, timeInSeconds);
|
||||
targetAnimator.Update(0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 1. Отматываем оба стейта к моменту начала перехода (минус ElapsedTransitionTime)
|
||||
float startSecondsA = (layer.NormalizedTime * layer.StateLength) - layer.ElapsedTransitionTime;
|
||||
float startSecondsB = (layer.NextNormalizedTime * layer.NextStateLength) - layer.ElapsedTransitionTime;
|
||||
// Защита от отрицательного времени (фикс бага старого 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);
|
||||
|
||||
// 2. Запускаем переход с оригинальной полной длительностью
|
||||
targetAnimator.CrossFadeInFixedTime(
|
||||
layer.NextFullPathHash,
|
||||
layer.TotalTransitionDuration,
|
||||
i,
|
||||
startSecondsB);
|
||||
|
||||
// 3. Прокручиваем аниматор ровно на ElapsedTransitionTime вперед (с выключенным RootMotion),
|
||||
// чтобы внутренний вес смешивания Mecanim встал ровно в ту же долю процента!
|
||||
targetAnimator.Update(layer.ElapsedTransitionTime);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user