1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 21:17:33 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSpinner.cs

304 lines
9.8 KiB
C#
Raw Normal View History

// 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
using System;
2018-04-13 17:19:50 +08:00
using System.Linq;
using JetBrains.Annotations;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-11-19 19:40:30 +08:00
using osu.Game.Audio;
using osu.Game.Graphics;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Osu.Judgements;
using osu.Game.Rulesets.Osu.Skinning;
2020-12-04 19:21:53 +08:00
using osu.Game.Rulesets.Osu.Skinning.Default;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Scoring;
2019-09-06 14:24:00 +08:00
using osu.Game.Screens.Ranking;
using osu.Game.Skinning;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Osu.Objects.Drawables
{
public class DrawableSpinner : DrawableOsuHitObject
{
2020-11-05 12:51:46 +08:00
public new Spinner HitObject => (Spinner)base.HitObject;
2018-04-13 17:19:50 +08:00
public new OsuSpinnerJudgementResult Result => (OsuSpinnerJudgementResult)base.Result;
2020-11-05 12:51:46 +08:00
public SpinnerRotationTracker RotationTracker { get; private set; }
public SpinnerSpmCounter SpmCounter { get; private set; }
2020-11-05 12:51:46 +08:00
private Container<DrawableSpinnerTick> ticks;
private SpinnerBonusDisplay bonusDisplay;
2020-11-19 19:40:30 +08:00
private PausableSkinnableSound spinningSample;
2018-11-09 12:58:46 +08:00
2020-11-05 12:51:46 +08:00
private Bindable<bool> isSpinning;
private bool spinnerFrequencyModulate;
2020-11-10 23:22:06 +08:00
public DrawableSpinner()
: this(null)
{
}
public DrawableSpinner([CanBeNull] Spinner s = null)
2019-02-28 12:31:40 +08:00
: base(s)
2018-04-13 17:19:50 +08:00
{
2020-11-05 12:51:46 +08:00
}
2018-04-13 17:19:50 +08:00
2020-11-05 12:51:46 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Origin = Anchor.Centre;
2018-04-13 17:19:50 +08:00
RelativeSizeAxes = Axes.Both;
InternalChildren = new Drawable[]
{
ticks = new Container<DrawableSpinnerTick>(),
new AspectContainer
2018-04-13 17:19:50 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Y,
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
2020-07-29 21:31:18 +08:00
new SkinnableDrawable(new OsuSkinComponent(OsuSkinComponents.SpinnerBody), _ => new DefaultSpinnerDisc()),
RotationTracker = new SpinnerRotationTracker(this)
2018-04-13 17:19:50 +08:00
}
},
2019-12-18 08:04:37 +08:00
SpmCounter = new SpinnerSpmCounter
2018-04-13 17:19:50 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Y = 120,
Alpha = 0
},
bonusDisplay = new SpinnerBonusDisplay
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Y = -120,
},
2020-11-19 19:40:30 +08:00
spinningSample = new PausableSkinnableSound
{
Volume = { Value = 0 },
Looping = true,
Frequency = { Value = spinning_sample_initial_frequency }
}
2018-04-13 17:19:50 +08:00
};
PositionBindable.BindValueChanged(pos => Position = pos.NewValue);
2020-11-05 12:51:46 +08:00
}
2020-07-30 18:34:59 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
isSpinning = RotationTracker.IsSpinning.GetBoundCopy();
isSpinning.BindValueChanged(updateSpinningSample);
}
2020-08-16 02:44:02 +08:00
private const float spinning_sample_initial_frequency = 1.0f;
private const float spinning_sample_modulated_base_frequency = 0.5f;
2020-07-30 18:34:59 +08:00
2020-11-30 18:24:38 +08:00
protected override void OnFree()
2020-11-19 19:40:30 +08:00
{
2020-11-30 18:24:38 +08:00
base.OnFree();
2020-11-19 19:40:30 +08:00
spinningSample.Samples = null;
}
2020-07-30 18:34:59 +08:00
protected override void LoadSamples()
{
base.LoadSamples();
var firstSample = HitObject.Samples.FirstOrDefault();
if (firstSample != null)
{
2020-12-01 14:37:51 +08:00
var clone = HitObject.SampleControlPoint.ApplyTo(firstSample).With("spinnerspin");
2020-07-30 18:34:59 +08:00
2020-11-19 19:40:30 +08:00
spinningSample.Samples = new ISampleInfo[] { clone };
spinningSample.Frequency.Value = spinning_sample_initial_frequency;
2020-07-30 18:34:59 +08:00
}
}
private void updateSpinningSample(ValueChangedEvent<bool> tracking)
{
2020-09-29 11:45:20 +08:00
if (tracking.NewValue)
2020-07-30 18:34:59 +08:00
{
spinningSample?.Play(!spinningSample.IsPlaying);
spinningSample?.VolumeTo(1, 300);
2020-07-30 18:34:59 +08:00
}
else
{
spinningSample?.VolumeTo(0, 300).OnComplete(_ => spinningSample.Stop());
2020-07-30 18:34:59 +08:00
}
}
public override void StopAllSamples()
{
base.StopAllSamples();
spinningSample?.Stop();
}
protected override void AddNestedHitObject(DrawableHitObject hitObject)
{
base.AddNestedHitObject(hitObject);
switch (hitObject)
{
case DrawableSpinnerTick tick:
ticks.Add(tick);
break;
}
}
2020-11-04 15:19:07 +08:00
protected override void UpdateHitStateTransforms(ArmedState state)
{
2020-11-04 15:19:07 +08:00
base.UpdateHitStateTransforms(state);
this.FadeOut(160).Expire();
2020-07-30 18:34:59 +08:00
// skin change does a rewind of transforms, which will stop the spinning sound from playing if it's currently in playback.
isSpinning?.TriggerChange();
}
protected override void ClearNestedHitObjects()
{
base.ClearNestedHitObjects();
2020-11-12 14:59:48 +08:00
ticks.Clear(false);
}
protected override DrawableHitObject CreateNestedHitObject(HitObject hitObject)
{
switch (hitObject)
{
case SpinnerBonusTick bonusTick:
return new DrawableSpinnerBonusTick(bonusTick);
case SpinnerTick tick:
return new DrawableSpinnerTick(tick);
}
return base.CreateNestedHitObject(hitObject);
2018-11-09 12:58:46 +08:00
}
protected override void ApplySkin(ISkinSource skin, bool allowFallback)
{
2020-08-16 02:34:17 +08:00
base.ApplySkin(skin, allowFallback);
spinnerFrequencyModulate = skin.GetConfig<OsuSkinConfiguration, bool>(OsuSkinConfiguration.SpinnerFrequencyModulate)?.Value ?? true;
2018-04-13 17:19:50 +08:00
}
2020-07-30 11:55:34 +08:00
/// <summary>
/// The completion progress of this spinner from 0..1 (clamped).
/// </summary>
public float Progress
{
get
{
2020-11-05 12:51:46 +08:00
if (HitObject.SpinsRequired == 0)
// some spinners are so short they can't require an integer spin count.
// these become implicitly hit.
return 1;
return Math.Clamp(Result.RateAdjustedRotation / 360 / HitObject.SpinsRequired, 0, 1);
}
}
2018-04-13 17:19:50 +08:00
protected override JudgementResult CreateResult(Judgement judgement) => new OsuSpinnerJudgementResult(HitObject, judgement);
protected override void CheckForResult(bool userTriggered, double timeOffset)
2018-04-13 17:19:50 +08:00
{
if (Time.Current < HitObject.StartTime) return;
if (Progress >= 1)
Result.TimeCompleted ??= Time.Current;
2018-04-13 17:19:50 +08:00
2020-11-05 12:51:46 +08:00
if (userTriggered || Time.Current < HitObject.EndTime)
return;
// Trigger a miss result for remaining ticks to avoid infinite gameplay.
foreach (var tick in ticks.Where(t => !t.Result.HasResult))
2020-07-21 18:48:44 +08:00
tick.TriggerResult(false);
ApplyResult(r =>
2018-04-13 17:19:50 +08:00
{
if (Progress >= 1)
r.Type = HitResult.Great;
2018-04-13 17:19:50 +08:00
else if (Progress > .9)
2020-09-29 16:16:55 +08:00
r.Type = HitResult.Ok;
2018-04-13 17:19:50 +08:00
else if (Progress > .75)
r.Type = HitResult.Meh;
2020-11-05 12:51:46 +08:00
else if (Time.Current >= HitObject.EndTime)
r.Type = r.Judgement.MinResult;
});
2018-04-13 17:19:50 +08:00
}
protected override void Update()
{
base.Update();
2020-07-30 18:34:59 +08:00
if (HandleUserInput)
{
bool isValidSpinningTime = Time.Current >= HitObject.StartTime && Time.Current <= HitObject.EndTime;
bool correctButtonPressed = (OsuActionInputManager?.PressedActions.Any(x => x == OsuAction.LeftButton || x == OsuAction.RightButton) ?? false);
RotationTracker.Tracking = !Result.HasResult
&& correctButtonPressed
&& isValidSpinningTime;
}
2020-07-30 18:34:59 +08:00
if (spinningSample != null && spinnerFrequencyModulate)
2020-08-16 02:44:02 +08:00
spinningSample.Frequency.Value = spinning_sample_modulated_base_frequency + Progress;
2018-04-13 17:19:50 +08:00
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
if (!SpmCounter.IsPresent && RotationTracker.Tracking)
SpmCounter.FadeIn(HitObject.TimeFadeIn);
SpmCounter.SetRotation(Result.RateAdjustedRotation);
updateBonusScore();
2018-04-13 17:19:50 +08:00
}
private int wholeSpins;
private void updateBonusScore()
{
if (ticks.Count == 0)
return;
int spins = (int)(Result.RateAdjustedRotation / 360);
2020-07-21 18:21:30 +08:00
if (spins < wholeSpins)
{
2020-07-21 18:21:30 +08:00
// rewinding, silently handle
wholeSpins = spins;
return;
}
2020-07-21 18:21:30 +08:00
while (wholeSpins != spins)
{
2020-10-04 00:08:24 +08:00
var tick = ticks.FirstOrDefault(t => !t.Result.HasResult);
2020-07-21 18:21:30 +08:00
// tick may be null if we've hit the spin limit.
if (tick != null)
{
2020-07-21 18:48:44 +08:00
tick.TriggerResult(true);
2020-07-21 18:21:30 +08:00
if (tick is DrawableSpinnerBonusTick)
2020-11-05 12:51:46 +08:00
bonusDisplay.SetBonusCount(spins - HitObject.SpinsRequired);
}
2020-07-21 18:21:30 +08:00
wholeSpins++;
}
}
2018-04-13 17:19:50 +08:00
}
}