1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 20:47:30 +08:00
osu-lazer/osu.Game.Rulesets.Mania/Timing/Drawables/DrawableGravityTimingChange.cs

74 lines
2.9 KiB
C#
Raw Normal View History

2017-06-02 10:35:51 +08:00
using System;
using osu.Framework.Graphics;
using osu.Framework.MathUtils;
using osu.Framework.Physics;
2017-06-02 10:35:51 +08:00
using osu.Game.Rulesets.Objects.Drawables;
namespace osu.Game.Rulesets.Mania.Timing.Drawables
{
public class DrawableGravityTimingChange : DrawableTimingChange
{
2017-06-02 14:30:59 +08:00
// Amount of time to travel this container such that
// at time = 0, gravityTime = timeSpan and
// at time = travel_time, gravityTime = 0
// Where gravityTime is used as the position of the content
private const double travel_time = 1000;
public DrawableGravityTimingChange(TimingChange timingChange)
: base(timingChange)
{
}
protected override void Update()
{
base.Update();
2017-06-02 14:30:59 +08:00
// The gravity-adjusted start position
float startY = (float)computeGravityTime(TimingChange.Time);
// The gravity-adjusted end position
float endY = (float)computeGravityTime(TimingChange.Time + Content.RelativeCoordinateSpace.Y);
2017-06-02 10:35:51 +08:00
2017-06-02 14:30:59 +08:00
Content.Y = startY;
Content.Height = endY - startY;
}
2017-06-02 10:35:51 +08:00
2017-06-02 14:30:59 +08: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 10:35:51 +08:00
2017-06-02 14:30:59 +08: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 10:35:51 +08:00
2017-06-02 14:30:59 +08:00
return timeSpan - acceleration / 2 * relativeTime * relativeTime * sign;
}
2017-06-02 14:30:59 +08:00
/// <summary>
/// The time spanned by this container.
/// </summary>
private double timeSpan => RelativeCoordinateSpace.Y;
/// <summary>
/// The acceleration due to "gravity" of the content of this container.
/// </summary>
private double acceleration => 2 * timeSpan / travel_time / travel_time;
/// <summary>
/// Computes the current time relative to <paramref name="time"/>, accounting for <see cref="travel_time"/>.
/// </summary>
/// <param name="time">The non-offset time.</param>
/// <returns>The current time relative to <paramref name="time"/> - <see cref="travel_time"/>. </returns>
private double relativeTimeAt(double time) => Time.Current - time + travel_time;
/// <summary>
/// The velocity of the content of this container at a time.
/// </summary>
/// <param name="time">The non-offset time.</param>
/// <returns>The velocity at <paramref name="time"/>.</returns>
private double velocityAt(double time) => acceleration * relativeTimeAt(time);
}
}