1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 17:27:24 +08:00

Refactor mascot to only contain state transitions

This commit is contained in:
Bartłomiej Dach 2020-04-29 20:28:46 +02:00
parent 43e768240f
commit 6e2ed0c4f3
3 changed files with 67 additions and 97 deletions

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints; using osu.Game.Beatmaps.ControlPoints;
@ -41,26 +42,25 @@ namespace osu.Game.Rulesets.Taiko.Tests.Skinning
[Test] [Test]
public void TestStateTextures() public void TestStateTextures()
{ {
AddStep("Set beatmap", () => setBeatmap()); AddStep("set beatmap", () => setBeatmap());
AddStep("Create mascot (idle)", () => AddStep("create mascot", () =>
{ {
SetContents(() => new TestDrawableTaikoMascot()); SetContents(() => new TestDrawableTaikoMascot());
}); });
AddStep("Clear state", () => setState(TaikoMascotAnimationState.Clear)); AddStep("clear state", () => setState(TaikoMascotAnimationState.Clear));
AddStep("kiai state", () => setState(TaikoMascotAnimationState.Kiai));
AddStep("Kiai state", () => setState(TaikoMascotAnimationState.Kiai)); AddStep("fail state", () => setState(TaikoMascotAnimationState.Fail));
AddStep("idle state", () => setState(TaikoMascotAnimationState.Idle));
AddStep("Fail state", () => setState(TaikoMascotAnimationState.Fail));
} }
[Test] [Test]
public void TestPlayfield() public void TestPlayfield()
{ {
AddStep("Set beatmap", () => setBeatmap()); AddStep("set beatmap", () => setBeatmap());
AddStep("Create ruleset", () => AddStep("create drawable ruleset", () =>
{ {
SetContents(() => SetContents(() =>
{ {
@ -69,21 +69,21 @@ namespace osu.Game.Rulesets.Taiko.Tests.Skinning
}); });
}); });
AddStep("Create hit (great)", () => addJudgement(HitResult.Miss)); AddStep("new judgement (miss)", () => addJudgement(HitResult.Miss));
AddUntilStep("Wait for idle state", () => checkForState(TaikoMascotAnimationState.Fail)); AddUntilStep("wait for fail state", () => assertState(TaikoMascotAnimationState.Fail));
AddStep("Create hit (great)", () => addJudgement(HitResult.Great)); AddStep("new judgement (great)", () => addJudgement(HitResult.Great));
AddUntilStep("Wait for idle state", () => checkForState(TaikoMascotAnimationState.Idle)); AddUntilStep("wait for idle state", () => assertState(TaikoMascotAnimationState.Idle));
} }
[Test] [Test]
public void TestKiai() public void TestKiai()
{ {
AddStep("Set beatmap", () => setBeatmap(true)); AddStep("set beatmap", () => setBeatmap(true));
AddUntilStep("Wait for beatmap to be loaded", () => Beatmap.Value.Track.IsLoaded); AddUntilStep("wait for beatmap to be loaded", () => Beatmap.Value.Track.IsLoaded);
AddStep("Create kiai ruleset", () => AddStep("create drawable ruleset", () =>
{ {
Beatmap.Value.Track.Start(); Beatmap.Value.Track.Start();
@ -94,10 +94,10 @@ namespace osu.Game.Rulesets.Taiko.Tests.Skinning
}); });
}); });
AddUntilStep("Wait for idle state", () => checkForState(TaikoMascotAnimationState.Fail)); AddUntilStep("wait for fail state", () => assertState(TaikoMascotAnimationState.Fail));
AddStep("Create hit (great)", () => addJudgement(HitResult.Great)); AddStep("new judgement (great)", () => addJudgement(HitResult.Great));
AddUntilStep("Wait for kiai state", () => checkForState(TaikoMascotAnimationState.Kiai)); AddUntilStep("wait for kiai state", () => assertState(TaikoMascotAnimationState.Kiai));
} }
private void setBeatmap(bool kiai = false) private void setBeatmap(bool kiai = false)
@ -129,7 +129,7 @@ namespace osu.Game.Rulesets.Taiko.Tests.Skinning
private void setState(TaikoMascotAnimationState state) private void setState(TaikoMascotAnimationState state)
{ {
foreach (var mascot in mascots) foreach (var mascot in mascots)
mascot?.ShowState(state); mascot.State.Value = state;
} }
private void addJudgement(HitResult result) private void addJudgement(HitResult result)
@ -143,7 +143,7 @@ namespace osu.Game.Rulesets.Taiko.Tests.Skinning
} }
} }
private bool checkForState(TaikoMascotAnimationState state) => mascots.All(d => d.State == state); private bool assertState(TaikoMascotAnimationState state) => mascots.All(d => d.State.Value == state);
private class TestDrawableTaikoMascot : DrawableTaikoMascot private class TestDrawableTaikoMascot : DrawableTaikoMascot
{ {
@ -152,10 +152,7 @@ namespace osu.Game.Rulesets.Taiko.Tests.Skinning
{ {
} }
protected override TaikoMascotAnimationState GetFinalAnimationState(EffectControlPoint effectPoint, TaikoMascotAnimationState playfieldState) public new Bindable<TaikoMascotAnimationState> State => base.State;
{
return State;
}
} }
} }
} }

View File

@ -1,29 +1,36 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System; using System.Collections.Generic;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio.Track; using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Textures; using osu.Framework.Graphics.Textures;
using osu.Game.Beatmaps.ControlPoints; using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Scoring;
namespace osu.Game.Rulesets.Taiko.UI namespace osu.Game.Rulesets.Taiko.UI
{ {
public class DrawableTaikoMascot : BeatSyncedContainer public class DrawableTaikoMascot : BeatSyncedContainer
{ {
private TaikoMascotTextureAnimation idleDrawable, clearDrawable, kiaiDrawable, failDrawable; protected Bindable<TaikoMascotAnimationState> State { get; }
private EffectControlPoint lastEffectControlPoint;
private TaikoMascotAnimationState playfieldState;
public TaikoMascotAnimationState State { get; private set; } private readonly Dictionary<TaikoMascotAnimationState, TaikoMascotTextureAnimation> animations;
private Drawable currentAnimation;
private bool lastHitMissed;
private bool kiaiMode;
public DrawableTaikoMascot(TaikoMascotAnimationState startingState = TaikoMascotAnimationState.Idle) public DrawableTaikoMascot(TaikoMascotAnimationState startingState = TaikoMascotAnimationState.Idle)
{ {
RelativeSizeAxes = Axes.Both; RelativeSizeAxes = Axes.Both;
State = startingState; State = new Bindable<TaikoMascotAnimationState>(startingState);
animations = new Dictionary<TaikoMascotAnimationState, TaikoMascotTextureAnimation>();
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -31,81 +38,53 @@ namespace osu.Game.Rulesets.Taiko.UI
{ {
InternalChildren = new[] InternalChildren = new[]
{ {
idleDrawable = new TaikoMascotTextureAnimation(TaikoMascotAnimationState.Idle), animations[TaikoMascotAnimationState.Idle] = new TaikoMascotTextureAnimation(TaikoMascotAnimationState.Idle),
clearDrawable = new TaikoMascotTextureAnimation(TaikoMascotAnimationState.Clear), animations[TaikoMascotAnimationState.Clear] = new TaikoMascotTextureAnimation(TaikoMascotAnimationState.Clear),
kiaiDrawable = new TaikoMascotTextureAnimation(TaikoMascotAnimationState.Kiai), animations[TaikoMascotAnimationState.Kiai] = new TaikoMascotTextureAnimation(TaikoMascotAnimationState.Kiai),
failDrawable = new TaikoMascotTextureAnimation(TaikoMascotAnimationState.Fail), animations[TaikoMascotAnimationState.Fail] = new TaikoMascotTextureAnimation(TaikoMascotAnimationState.Fail),
}; };
ShowState(State); updateState();
} }
public void ShowState(TaikoMascotAnimationState state) protected override void LoadComplete()
{ {
foreach (var child in InternalChildren) base.LoadComplete();
child.Hide();
State = state; animations.Values.ForEach(animation => animation.Hide());
State.BindValueChanged(mascotStateChanged, true);
var drawable = getStateDrawable(State);
drawable.Show();
} }
/// <summary> public void OnNewResult(JudgementResult result)
/// Sets the playfield state used for determining the final state.
/// </summary>
/// <remarks>
/// If you're looking to change the state manually, please look at <see cref="ShowState"/>.
/// </remarks>
public void SetPlayfieldState(TaikoMascotAnimationState state)
{ {
playfieldState = state; lastHitMissed = result.Type == HitResult.Miss && result.Judgement.AffectsCombo;
updateState();
if (lastEffectControlPoint != null)
ShowState(GetFinalAnimationState(lastEffectControlPoint, playfieldState));
}
private TaikoMascotTextureAnimation getStateDrawable(TaikoMascotAnimationState state)
{
switch (state)
{
case TaikoMascotAnimationState.Idle:
return idleDrawable;
case TaikoMascotAnimationState.Clear:
return clearDrawable;
case TaikoMascotAnimationState.Kiai:
return kiaiDrawable;
case TaikoMascotAnimationState.Fail:
return failDrawable;
default:
throw new ArgumentOutOfRangeException(nameof(state), $"There's no animation available for state {state}");
}
}
protected virtual TaikoMascotAnimationState GetFinalAnimationState(EffectControlPoint effectPoint, TaikoMascotAnimationState playfieldState)
{
if (playfieldState == TaikoMascotAnimationState.Fail)
return playfieldState;
return effectPoint.KiaiMode ? TaikoMascotAnimationState.Kiai : TaikoMascotAnimationState.Idle;
} }
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, TrackAmplitudes amplitudes) protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, TrackAmplitudes amplitudes)
{ {
base.OnNewBeat(beatIndex, timingPoint, effectPoint, amplitudes); kiaiMode = effectPoint.KiaiMode;
updateState();
}
var state = GetFinalAnimationState(lastEffectControlPoint = effectPoint, playfieldState); private void updateState()
ShowState(state); {
State.Value = getNextState();
}
if (state == TaikoMascotAnimationState.Clear) private TaikoMascotAnimationState getNextState()
return; {
if (lastHitMissed)
return TaikoMascotAnimationState.Fail;
var drawable = getStateDrawable(state); return kiaiMode ? TaikoMascotAnimationState.Kiai : TaikoMascotAnimationState.Idle;
drawable.Move(); }
private void mascotStateChanged(ValueChangedEvent<TaikoMascotAnimationState> state)
{
currentAnimation?.Hide();
currentAnimation = animations[state.NewValue];
currentAnimation.Show();
} }
} }
} }

View File

@ -15,7 +15,6 @@ using osu.Game.Rulesets.Taiko.Objects.Drawables;
using osu.Game.Rulesets.Taiko.Judgements; using osu.Game.Rulesets.Taiko.Judgements;
using osu.Game.Rulesets.Taiko.Objects; using osu.Game.Rulesets.Taiko.Objects;
using osu.Game.Skinning; using osu.Game.Skinning;
using osu.Game.Rulesets.Scoring;
namespace osu.Game.Rulesets.Taiko.UI namespace osu.Game.Rulesets.Taiko.UI
{ {
@ -216,12 +215,7 @@ namespace osu.Game.Rulesets.Taiko.UI
if (mascotDrawable.Drawable is DrawableTaikoMascot mascot) if (mascotDrawable.Drawable is DrawableTaikoMascot mascot)
{ {
var miss = result.Type == HitResult.Miss; mascot.OnNewResult(result);
if (miss && judgedObject.HitObject is StrongHitObject)
miss = result.Judgement.AffectsCombo;
mascot.SetPlayfieldState(miss ? TaikoMascotAnimationState.Fail : TaikoMascotAnimationState.Idle);
} }
} }