1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 23:47:24 +08:00
osu-lazer/osu.Game/GameModes/Play/ComboCounter.cs

272 lines
7.9 KiB
C#
Raw Normal View History

2016-10-14 06:13:20 +08:00
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Transformations;
using osu.Framework.MathUtils;
using osu.Framework.Timing;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2016-10-16 04:16:02 +08:00
namespace osu.Game.GameModes.Play
2016-10-14 06:13:20 +08:00
{
2016-10-22 16:50:42 +08:00
public abstract class ComboCounter : Container
2016-10-14 06:13:20 +08:00
{
public bool IsRolling
{
get; protected set;
}
2016-10-15 07:23:27 +08:00
protected SpriteText PopOutSpriteText;
2016-10-17 07:30:25 +08:00
protected virtual double PopOutDuration => 150;
2016-10-15 07:23:27 +08:00
protected virtual float PopOutScale => 2.0f;
protected virtual EasingTypes PopOutEasing => EasingTypes.None;
protected virtual float PopOutInitialAlpha => 0.75f;
2016-10-30 07:26:12 +08:00
protected virtual double FadeOutDuration => 100;
2016-10-14 06:13:20 +08:00
/// <summary>
2016-10-16 08:07:07 +08:00
/// Duration in milliseconds for the counter roll-up animation for each element.
2016-10-14 06:13:20 +08:00
/// </summary>
2016-10-15 07:23:27 +08:00
protected virtual double RollingDuration => 20;
2016-10-14 06:13:20 +08:00
/// <summary>
/// Easing for the counter rollover animation.
/// </summary>
2016-10-15 07:23:27 +08:00
protected EasingTypes RollingEasing => EasingTypes.None;
2016-10-14 06:13:20 +08:00
2016-10-16 08:07:07 +08:00
private ulong displayedCount;
2016-10-14 06:13:20 +08:00
/// <summary>
/// Value shown at the current moment.
/// </summary>
2016-10-16 08:07:07 +08:00
public virtual ulong DisplayedCount
2016-10-14 06:13:20 +08:00
{
get
{
2016-10-16 08:07:07 +08:00
return displayedCount;
2016-10-14 06:13:20 +08:00
}
protected set
{
2016-10-16 08:07:07 +08:00
if (displayedCount.Equals(value))
2016-10-14 06:13:20 +08:00
return;
2016-10-16 08:07:07 +08:00
updateDisplayedCount(displayedCount, value, IsRolling);
2016-10-14 06:13:20 +08:00
}
}
protected ulong count;
/// <summary>
/// Actual value of counter.
/// </summary>
public virtual ulong Count
{
get
{
return count;
}
set
{
2016-10-16 08:07:07 +08:00
updateCount(value);
2016-10-14 06:13:20 +08:00
}
}
public void Increment(ulong amount = 1)
{
Count = Count + amount;
}
2016-10-16 08:07:07 +08:00
protected SpriteText DisplayedCountSpriteText;
2016-10-14 06:13:20 +08:00
private float textSize;
2016-10-14 06:13:20 +08:00
public float TextSize
{
get { return textSize; }
set
{
textSize = value;
2016-10-16 08:07:07 +08:00
DisplayedCountSpriteText.TextSize = TextSize;
2016-10-15 07:23:27 +08:00
PopOutSpriteText.TextSize = TextSize;
2016-10-14 06:13:20 +08:00
}
}
/// <summary>
/// Base of all combo counters.
/// </summary>
protected ComboCounter()
{
2016-10-22 16:50:42 +08:00
AutoSizeAxes = Axes.Both;
2016-10-14 06:13:20 +08:00
Children = new Drawable[]
{
2016-10-16 08:07:07 +08:00
DisplayedCountSpriteText = new SpriteText
2016-10-14 06:13:20 +08:00
{
Alpha = 0,
},
2016-10-15 07:23:27 +08:00
PopOutSpriteText = new SpriteText
{
Alpha = 0,
}
2016-10-14 06:13:20 +08:00
};
TextSize = 80;
2016-10-14 06:13:20 +08:00
}
2016-11-01 22:24:14 +08:00
protected override void Load(BaseGame game)
2016-10-14 06:13:20 +08:00
{
base.Load(game);
2016-10-16 08:07:07 +08:00
DisplayedCountSpriteText.Text = FormatCount(Count);
DisplayedCountSpriteText.Anchor = this.Anchor;
DisplayedCountSpriteText.Origin = this.Origin;
2016-10-14 06:13:20 +08:00
StopRolling();
}
/// <summary>
2016-10-16 08:07:07 +08:00
/// Stops rollover animation, forcing the displayed count to be the actual count.
2016-10-14 06:13:20 +08:00
/// </summary>
public void StopRolling()
2016-10-14 06:13:20 +08:00
{
2016-10-16 08:07:07 +08:00
updateCount(Count);
2016-10-14 06:13:20 +08:00
}
2016-10-17 07:30:25 +08:00
/// <summary>
/// Animates roll-back to 0.
/// </summary>
public void Roll()
{
Roll(0);
}
2016-10-14 06:13:20 +08:00
/// <summary>
2016-10-14 08:50:06 +08:00
/// Animates roll-up/roll-back to an specific value.
2016-10-14 06:13:20 +08:00
/// </summary>
/// <param name="newValue">Target value.</param>
2016-10-17 07:30:25 +08:00
public virtual void Roll(ulong newValue)
2016-10-14 06:13:20 +08:00
{
2016-10-16 08:07:07 +08:00
updateCount(newValue, true);
2016-10-14 06:13:20 +08:00
}
/// <summary>
/// Resets count to default value.
/// </summary>
public virtual void ResetCount()
{
2016-10-16 08:07:07 +08:00
updateCount(0);
2016-10-14 06:13:20 +08:00
}
2016-10-15 07:23:27 +08:00
protected virtual string FormatCount(ulong count)
2016-10-14 06:13:20 +08:00
{
return count.ToString();
}
2016-10-16 08:07:07 +08:00
protected abstract void OnDisplayedCountRolling(ulong currentValue, ulong newValue);
protected abstract void OnDisplayedCountIncrement(ulong newValue);
protected abstract void OnDisplayedCountChange(ulong newValue);
protected virtual void OnCountRolling(ulong currentValue, ulong newValue)
{
2016-10-28 16:42:00 +08:00
transformRoll(new TransformComboRoll(), currentValue, newValue);
}
protected virtual void OnCountIncrement(ulong currentValue, ulong newValue) {
2016-10-16 08:07:07 +08:00
DisplayedCount = newValue;
}
protected virtual void OnCountChange(ulong currentValue, ulong newValue) {
2016-10-16 08:07:07 +08:00
DisplayedCount = newValue;
}
private double getProportionalDuration(ulong currentValue, ulong newValue)
{
2016-10-17 07:30:25 +08:00
double difference = currentValue > newValue ? currentValue - newValue : newValue - currentValue;
2016-10-16 08:07:07 +08:00
return difference * RollingDuration;
}
private void updateDisplayedCount(ulong currentValue, ulong newValue, bool rolling)
{
displayedCount = newValue;
if (rolling)
OnDisplayedCountRolling(currentValue, newValue);
else if (currentValue + 1 == newValue)
OnDisplayedCountIncrement(newValue);
else
OnDisplayedCountChange(newValue);
2016-10-14 06:13:20 +08:00
}
2016-10-16 08:07:07 +08:00
private void updateCount(ulong value, bool rolling = false)
2016-10-14 06:13:20 +08:00
{
2016-10-30 07:26:12 +08:00
ulong prevCount = count;
2016-10-16 08:07:07 +08:00
count = value;
2016-10-14 08:50:06 +08:00
if (!rolling)
2016-10-14 06:13:20 +08:00
{
2016-10-16 08:07:07 +08:00
Flush(false, typeof(TransformComboRoll));
2016-10-15 07:23:27 +08:00
IsRolling = false;
2016-10-16 08:07:07 +08:00
DisplayedCount = prevCount;
2016-10-14 06:13:20 +08:00
2016-10-16 08:07:07 +08:00
if (prevCount + 1 == count)
OnCountIncrement(prevCount, count);
else
2016-10-16 08:07:07 +08:00
OnCountChange(prevCount, count);
2016-10-14 06:13:20 +08:00
}
else
2016-10-16 08:07:07 +08:00
{
OnCountRolling(displayedCount, count);
IsRolling = true;
}
2016-10-14 06:13:20 +08:00
}
2016-10-16 08:07:07 +08:00
private void transformRoll(TransformComboRoll transform, ulong currentValue, ulong newValue)
2016-10-14 06:13:20 +08:00
{
2016-10-16 08:07:07 +08:00
Flush(false, typeof(TransformComboRoll));
2016-10-14 06:13:20 +08:00
if (Clock == null)
return;
2016-10-17 07:30:25 +08:00
if (RollingDuration < 1)
2016-10-14 06:13:20 +08:00
{
2016-10-16 08:07:07 +08:00
DisplayedCount = Count;
2016-10-14 06:13:20 +08:00
return;
}
transform.StartTime = Time;
2016-10-16 08:07:07 +08:00
transform.EndTime = Time + getProportionalDuration(currentValue, newValue);
2016-10-14 06:13:20 +08:00
transform.StartValue = currentValue;
transform.EndValue = newValue;
transform.Easing = RollingEasing;
Transforms.Add(transform);
}
2016-10-16 08:07:07 +08:00
protected class TransformComboRoll : Transform<ulong>
2016-10-14 06:13:20 +08:00
{
2016-10-28 16:42:00 +08:00
protected override ulong CurrentValue
2016-10-14 06:13:20 +08:00
{
get
{
2016-10-28 16:42:00 +08:00
double time = CurrentTime ?? 0;
2016-10-14 06:13:20 +08:00
if (time < StartTime) return StartValue;
if (time >= EndTime) return EndValue;
return (ulong)Interpolation.ValueAt(time, StartValue, EndValue, StartTime, EndTime, Easing);
}
}
public override void Apply(Drawable d)
{
base.Apply(d);
2016-10-16 08:07:07 +08:00
(d as ComboCounter).DisplayedCount = CurrentValue;
2016-10-14 06:13:20 +08:00
}
}
}
}