2019-01-24 16:43: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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-10-07 15:31:42 +08:00
|
|
|
|
using System.Collections.Generic;
|
2021-01-30 03:32:45 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2021-03-26 18:09:44 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2022-11-09 12:36:52 +08:00
|
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
2017-10-07 15:31:42 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2020-01-09 12:43:44 +08:00
|
|
|
|
using osu.Framework.Utils;
|
2021-01-30 03:32:45 +08:00
|
|
|
|
using osu.Game.Rulesets.Objects.Drawables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-12-04 19:21:53 +08:00
|
|
|
|
namespace osu.Game.Rulesets.Osu.Skinning.Default
|
2017-10-07 15:31:42 +08:00
|
|
|
|
{
|
2021-03-26 18:09:44 +08:00
|
|
|
|
public partial class SpinnerSpmCalculator : Component
|
2017-10-07 15:31:42 +08:00
|
|
|
|
{
|
2021-03-26 18:09:44 +08:00
|
|
|
|
private readonly Queue<RotationRecord> records = new Queue<RotationRecord>();
|
|
|
|
|
private const double spm_count_duration = 595; // not using hundreds to avoid frame rounding issues
|
2021-01-30 03:32:45 +08:00
|
|
|
|
|
2021-03-26 18:09:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The resultant spins per minute value, which is updated via <see cref="SetRotation"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IBindable<double> Result => result;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-03-26 18:09:44 +08:00
|
|
|
|
private readonly Bindable<double> result = new BindableDouble();
|
|
|
|
|
|
|
|
|
|
[Resolved]
|
2022-11-09 12:36:52 +08:00
|
|
|
|
private DrawableHitObject drawableSpinner { get; set; } = null!;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-01-30 03:32:45 +08:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
drawableSpinner.HitObjectApplied += resetState;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 01:36:06 +08:00
|
|
|
|
private RotationRecord lastRecord;
|
|
|
|
|
|
2017-10-07 15:42:10 +08:00
|
|
|
|
public void SetRotation(float currentRotation)
|
|
|
|
|
{
|
2017-11-06 16:22:22 +08:00
|
|
|
|
// If we've gone back in time, it's fine to work with a fresh set of records for now
|
2024-02-25 01:36:06 +08:00
|
|
|
|
if (records.Count > 0 && Time.Current < lastRecord.Time)
|
2017-11-06 16:22:22 +08:00
|
|
|
|
records.Clear();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2023-01-20 20:00:01 +08:00
|
|
|
|
// Never calculate SPM by same time of record to avoid 0 / 0 = NaN or X / 0 = Infinity result.
|
2024-02-25 01:36:06 +08:00
|
|
|
|
if (records.Count > 0 && Precision.AlmostEquals(Time.Current, lastRecord.Time))
|
2023-01-20 20:00:01 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2017-10-07 15:42:10 +08:00
|
|
|
|
if (records.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var record = records.Peek();
|
2017-10-12 17:28:26 +08:00
|
|
|
|
while (records.Count > 0 && Time.Current - records.Peek().Time > spm_count_duration)
|
2017-10-07 15:42:10 +08:00
|
|
|
|
record = records.Dequeue();
|
2019-12-18 08:03:12 +08:00
|
|
|
|
|
2021-03-26 18:09:44 +08:00
|
|
|
|
result.Value = (currentRotation - record.Rotation) / (Time.Current - record.Time) * 1000 * 60 / 360;
|
2017-10-07 15:42:10 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2024-02-25 01:36:06 +08:00
|
|
|
|
records.Enqueue(lastRecord = new RotationRecord { Rotation = currentRotation, Time = Time.Current });
|
2017-10-07 15:42:10 +08:00
|
|
|
|
}
|
2021-01-30 03:32:45 +08:00
|
|
|
|
|
|
|
|
|
private void resetState(DrawableHitObject hitObject)
|
|
|
|
|
{
|
2021-03-26 18:09:44 +08:00
|
|
|
|
result.Value = 0;
|
2021-01-30 03:32:45 +08:00
|
|
|
|
records.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
2022-11-09 12:36:52 +08:00
|
|
|
|
if (drawableSpinner.IsNotNull())
|
2021-01-30 03:32:45 +08:00
|
|
|
|
drawableSpinner.HitObjectApplied -= resetState;
|
|
|
|
|
}
|
2021-03-26 18:09:44 +08:00
|
|
|
|
|
|
|
|
|
private struct RotationRecord
|
|
|
|
|
{
|
|
|
|
|
public float Rotation;
|
|
|
|
|
public double Time;
|
|
|
|
|
}
|
2017-10-07 15:31:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|