2019-01-24 08:43:03 +00: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-07-05 13:48:54 +00:00
|
|
|
|
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
|
{
|
2018-07-06 03:13:23 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A container that adds the ability to shake its contents.
|
|
|
|
|
/// </summary>
|
2018-07-05 13:48:54 +00:00
|
|
|
|
public class ShakeContainer : Container
|
|
|
|
|
{
|
2018-12-04 11:33:29 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The length of a single shake.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float ShakeDuration = 80;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Total number of shakes. May be shortened if possible.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float TotalShakes = 4;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pixels of displacement per shake.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float ShakeMagnitude = 8;
|
|
|
|
|
|
2018-07-06 03:13:23 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Shake the contents of this container.
|
|
|
|
|
/// </summary>
|
2018-07-06 08:24:30 +00:00
|
|
|
|
/// <param name="maximumLength">The maximum length the shake should last.</param>
|
2018-12-04 11:33:29 +00:00
|
|
|
|
public void Shake(double? maximumLength = null)
|
2018-07-05 13:48:54 +00:00
|
|
|
|
{
|
2018-07-06 03:07:39 +00:00
|
|
|
|
const float shake_amount = 8;
|
2018-07-05 13:48:54 +00:00
|
|
|
|
|
2018-07-06 08:24:30 +00:00
|
|
|
|
// if we don't have enough time, don't bother shaking.
|
2018-12-04 11:33:29 +00:00
|
|
|
|
if (maximumLength < ShakeDuration * 2)
|
2018-07-06 08:24:30 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2018-12-04 11:33:29 +00:00
|
|
|
|
var sequence = this.MoveToX(shake_amount, ShakeDuration / 2, Easing.OutSine).Then()
|
|
|
|
|
.MoveToX(-shake_amount, ShakeDuration, Easing.InOutSine).Then();
|
2018-07-06 08:24:30 +00:00
|
|
|
|
|
|
|
|
|
// if we don't have enough time for the second shake, skip it.
|
2018-12-04 11:33:29 +00:00
|
|
|
|
if (!maximumLength.HasValue || maximumLength >= ShakeDuration * 4)
|
2019-11-11 11:53:22 +00:00
|
|
|
|
{
|
2018-07-06 08:24:30 +00:00
|
|
|
|
sequence = sequence
|
2018-12-04 11:33:29 +00:00
|
|
|
|
.MoveToX(shake_amount, ShakeDuration, Easing.InOutSine).Then()
|
|
|
|
|
.MoveToX(-shake_amount, ShakeDuration, Easing.InOutSine).Then();
|
2019-11-11 11:53:22 +00:00
|
|
|
|
}
|
2018-07-06 08:24:30 +00:00
|
|
|
|
|
2018-12-04 11:33:29 +00:00
|
|
|
|
sequence.MoveToX(0, ShakeDuration / 2, Easing.InSine);
|
2018-07-05 13:48:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|