osu/osu.Game.Rulesets.Mania/Timing/GravityScrollingContainer.cs

61 lines
2.5 KiB
C#
Raw Normal View History

2018-01-05 11:21:19 +00:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-06-02 09:20:14 +00:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Game.Rulesets.Timing;
2017-06-09 07:20:55 +00:00
namespace osu.Game.Rulesets.Mania.Timing
{
/// <summary>
/// A <see cref="ScrollingContainer"/> that emulates a form of gravity where hit objects speed up over time.
/// </summary>
internal class GravityScrollingContainer : ScrollingContainer
{
private readonly MultiplierControlPoint controlPoint;
public GravityScrollingContainer(MultiplierControlPoint controlPoint)
{
this.controlPoint = controlPoint;
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
2017-06-02 06:30:59 +00:00
// The gravity-adjusted start position
float startPos = (float)computeGravityTime(controlPoint.StartTime);
2017-06-02 06:30:59 +00:00
// The gravity-adjusted end position
float endPos = (float)computeGravityTime(controlPoint.StartTime + RelativeChildSize.Y);
2017-06-02 02:35:51 +00:00
Y = startPos;
Height = endPos - startPos;
2017-06-02 06:30:59 +00:00
}
2017-06-02 02:35:51 +00:00
2017-06-02 06:30:59 +00:00
/// <summary>
/// Applies gravity to a time value based on the current time.
/// </summary>
/// <param name="time">The time value gravity should be applied to.</param>
/// <returns>The time after gravity is applied to <paramref name="time"/>.</returns>
private double computeGravityTime(double time)
{
double relativeTime = relativeTimeAt(time);
2017-06-02 02:35:51 +00:00
2017-06-02 06:30:59 +00:00
// The sign of the relative time, this is used to apply backwards acceleration leading into startTime
double sign = relativeTime < 0 ? -1 : 1;
2017-06-02 02:35:51 +00:00
return VisibleTimeRange - acceleration * relativeTime * relativeTime * sign;
}
2017-06-02 06:30:59 +00:00
/// <summary>
/// The acceleration due to "gravity" of the content of this container.
/// </summary>
private double acceleration => 1 / VisibleTimeRange;
2017-06-02 06:30:59 +00:00
/// <summary>
/// Computes the current time relative to <paramref name="time"/>, accounting for <see cref="ScrollingContainer.VisibleTimeRange"/>.
2017-06-02 06:30:59 +00:00
/// </summary>
/// <param name="time">The non-offset time.</param>
/// <returns>The current time relative to <paramref name="time"/> - <see cref="ScrollingContainer.VisibleTimeRange"/>. </returns>
private double relativeTimeAt(double time) => Time.Current - time + VisibleTimeRange;
}
}