Avoid unnecessary array/LINQ operations when replay frames have no action changes

This commit is contained in:
Dean Herbert 2021-08-26 04:01:37 +09:00
parent f02b6b3657
commit f4199958d9
1 changed files with 18 additions and 3 deletions

View File

@ -42,9 +42,24 @@ public void Apply(InputState state, IInputStateChangeHandler handler)
if (!(state is RulesetInputManagerInputState<T> inputState))
throw new InvalidOperationException($"{nameof(ReplayState<T>)} should only be applied to a {nameof(RulesetInputManagerInputState<T>)}");
var lastPressed = inputState.LastReplayState?.PressedActions ?? new List<T>();
var released = lastPressed.Except(PressedActions).ToArray();
var pressed = PressedActions.Except(lastPressed).ToArray();
T[] released = Array.Empty<T>();
T[] pressed = Array.Empty<T>();
var lastPressed = inputState.LastReplayState?.PressedActions;
if (lastPressed == null || lastPressed.Count == 0)
{
pressed = PressedActions.ToArray();
}
else if (PressedActions.Count == 0)
{
released = lastPressed.ToArray();
}
else if (!lastPressed.SequenceEqual(PressedActions))
{
released = lastPressed.Except(PressedActions).ToArray();
pressed = PressedActions.Except(lastPressed).ToArray();
}
inputState.LastReplayState = this;