Skip to content

Commit

Permalink
handle legacy animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddio0141 committed Sep 4, 2024
1 parent 8922cdc commit 2a26ae8
Showing 1 changed file with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Reflection;
using HarmonyLib;
using UniTAS.Patcher.Utils;
using UnityEngine;
using Object = UnityEngine.Object;

namespace UniTAS.Patcher.Implementations.FrameAdvancing;
Expand All @@ -10,15 +12,21 @@ namespace UniTAS.Patcher.Implementations.FrameAdvancing;
public partial class FrameAdvancing
{
private readonly List<AnimatorTracker> _trackedAnimators = new();
// private readonly List<Animation> _trackedAnimations = new();
private readonly List<AnimationTracker> _trackedAnimations = new();

private void PauseAnimation()
{
RefreshTrackedAnimators();
RefreshTrackedAnimations();
foreach (var animator in _trackedAnimators)
{
animator.Pause();
}

foreach (var animation in _trackedAnimations)
{
animation.Pause();
}
}

private void ResumeAnimation()
Expand All @@ -28,7 +36,13 @@ private void ResumeAnimation()
animator.Resume();
}

foreach (var animation in _trackedAnimations)
{
animation.Resume();
}

_trackedAnimators.Clear();
_trackedAnimations.Clear();
}

private void RefreshTrackedAnimators()
Expand All @@ -43,6 +57,18 @@ private void RefreshTrackedAnimators()
}
}

private void RefreshTrackedAnimations()
{
var animations = ObjectUtils.FindObjectsOfType<Animation>();
_trackedAnimations.Clear();
_logger.LogDebug(
$"refreshing tracked animations for frame advancing, found {animations.Length} animations in scene");
foreach (var animation in animations)
{
_trackedAnimations.Add(new(animation));
}
}

private class AnimatorTracker(Object animator)
{
private float _speedBeforePause;
Expand Down Expand Up @@ -70,4 +96,28 @@ public void Resume()
SetSpeed.Invoke(animator, [_speedBeforePause]);
}
}

private class AnimationTracker(Animation animation)
{
private readonly Dictionary<AnimationState, float> _speeds = new();

public void Pause()
{
foreach (AnimationState state in animation)
{
_speeds.Add(state, state.speed);
state.speed = 0f;
}
}

public void Resume()
{
foreach (var keyValue in _speeds)
{
keyValue.Key.speed = keyValue.Value;
}

_speeds.Clear();
}
}
}

0 comments on commit 2a26ae8

Please sign in to comment.