1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 11:52:55 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Objects/Drawables/SpinnerSpinHistory.cs

147 lines
6.2 KiB
C#
Raw Normal View History

2023-10-16 18:07:03 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace osu.Game.Rulesets.Osu.Objects.Drawables
{
/// <summary>
/// Stores the spinning history of a single spinner.<br />
/// Instants of movement deltas may be added or removed from this in order to calculate the total rotation for the spinner.
/// </summary>
/// <remarks>
/// A single, full rotation of the spinner is defined as a 360-degree rotation of the spinner, starting from 0, going in a single direction.<br />
/// </remarks>
/// <example>
/// If the player spins 90-degrees clockwise, then changes direction, they need to spin 90-degrees counter-clockwise to return to 0
/// and then continue rotating the spinner for another 360-degrees in the same direction.
/// </example>
public class SpinnerSpinHistory
{
/// <summary>
/// The sum of all complete spins and any current partial spin, in degrees.
/// </summary>
/// <remarks>
/// This is the final scoring value.
/// </remarks>
public float TotalRotation => 360 * completedSpins.Count + currentSpinMaxRotation;
2023-10-16 18:07:03 +08:00
private readonly Stack<CompletedSpin> completedSpins = new Stack<CompletedSpin>();
2023-10-16 18:07:03 +08:00
/// <summary>
2023-10-23 15:46:15 +08:00
/// The total accumulated (absolute) rotation.
2023-10-16 18:07:03 +08:00
/// </summary>
2023-10-23 15:46:15 +08:00
private float totalAccumulatedRotation;
2023-10-16 18:07:03 +08:00
2023-10-23 15:46:15 +08:00
private float totalAccumulatedRotationAtLastCompletion;
2023-10-16 18:07:03 +08:00
/// <summary>
/// For the current spin, represents the maximum absolute rotation (from 0..360) achieved by the user.
2023-10-16 18:07:03 +08:00
/// </summary>
/// <remarks>
/// This is used to report <see cref="TotalRotation"/> in the case a user spins backwards.
/// Basically it allows us to not reduce the total rotation in such a case.
///
/// This also stops spinner "cheese" where a user may rapidly change directions and cause an increase
/// in rotations.
/// </remarks>
private float currentSpinMaxRotation;
2023-10-16 18:07:03 +08:00
/// <summary>
/// The current spin, from -360..360.
/// </summary>
2023-10-23 15:46:15 +08:00
private float currentSpinRotation => totalAccumulatedRotation - totalAccumulatedRotationAtLastCompletion;
2023-10-16 18:07:03 +08:00
private double lastReportTime = double.NegativeInfinity;
/// <summary>
/// Report a delta update based on user input.
/// </summary>
/// <param name="currentTime">The current time.</param>
/// <param name="delta">The delta of the angle moved through since the last report.</param>
public void ReportDelta(double currentTime, float delta)
{
if (delta == 0)
return;
// Importantly, outside of tests the max delta entering here is 180 degrees.
// If it wasn't for tests, we could add this line:
//
// Debug.Assert(Math.Abs(delta) < 180);
//
// For this to be 101% correct, we need to add the ability for important frames to be
// created based on gameplay intrinsics (ie. there should be one frame for any spinner delta 90 < n < 180 degrees).
//
// But this can come later.
2023-10-23 15:46:15 +08:00
totalAccumulatedRotation += delta;
2023-10-16 18:07:03 +08:00
if (currentTime >= lastReportTime)
2023-10-16 18:07:03 +08:00
{
currentSpinMaxRotation = Math.Max(currentSpinMaxRotation, Math.Abs(currentSpinRotation));
2023-10-16 18:07:03 +08:00
// Handle the case where the user has completed another spin.
// Note that this does could be an `if` rather than `while` if the above assertion held true.
// It is a `while` loop to handle tests which throw larger values at this method.
while (currentSpinMaxRotation >= 360)
{
int direction = Math.Sign(currentSpinRotation);
2023-10-16 18:07:03 +08:00
completedSpins.Push(new CompletedSpin(currentTime, direction));
2023-10-16 18:07:03 +08:00
// Incrementing the last completion point will cause `currentSpinRotation` to
// hold the remaining spin that needs to be considered.
2023-10-23 15:46:15 +08:00
totalAccumulatedRotationAtLastCompletion += direction * 360;
// Reset the current max as we are entering a new spin.
// Importantly, carry over the remainder (which is now stored in `currentSpinRotation`).
currentSpinMaxRotation = Math.Abs(currentSpinRotation);
}
}
else
2023-10-16 18:07:03 +08:00
{
// When rewinding, the main thing we care about is getting `totalAbsoluteRotationsAtLastCompletion`
// to the correct value. We can used the stored history for this.
while (completedSpins.TryPeek(out var segment) && segment.CompletionTime > currentTime)
{
completedSpins.Pop();
2023-10-23 15:46:15 +08:00
totalAccumulatedRotationAtLastCompletion -= segment.Direction * 360;
}
// This is a best effort. We may not have enough data to match this 1:1, but that's okay.
// We know that the player is somewhere in a spin.
// In the worst case, this will be lower than expected, and recover in forward playback.
currentSpinMaxRotation = Math.Abs(currentSpinRotation);
2023-10-16 18:07:03 +08:00
}
lastReportTime = currentTime;
2023-10-16 18:07:03 +08:00
}
/// <summary>
/// Represents a single completed spin.
2023-10-16 18:07:03 +08:00
/// </summary>
private class CompletedSpin
2023-10-16 18:07:03 +08:00
{
/// <summary>
/// The time at which this spin completion occurred.
2023-10-16 18:07:03 +08:00
/// </summary>
public readonly double CompletionTime;
2023-10-16 18:07:03 +08:00
/// <summary>
/// The direction this spin completed in.
2023-10-16 18:07:03 +08:00
/// </summary>
public readonly int Direction;
public CompletedSpin(double completionTime, int direction)
2023-10-16 18:07:03 +08:00
{
Debug.Assert(direction == -1 || direction == 1);
CompletionTime = completionTime;
2023-10-16 18:07:03 +08:00
Direction = direction;
}
}
}
}