1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 14:47:25 +08:00

Refactor mascot animations to split logic paths

This commit is contained in:
Bartłomiej Dach 2020-04-29 21:27:02 +02:00
parent 6e2ed0c4f3
commit e81d33dcec
4 changed files with 133 additions and 146 deletions

View File

@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
@ -27,6 +26,7 @@ namespace osu.Game.Rulesets.Taiko.Tests.Skinning
public override IReadOnlyList<Type> RequiredTypes => base.RequiredTypes.Concat(new[]
{
typeof(DrawableTaikoMascot),
typeof(TaikoMascotAnimation)
}).ToList();
[Cached(typeof(IScrollingInfo))]
@ -36,23 +36,18 @@ namespace osu.Game.Rulesets.Taiko.Tests.Skinning
TimeRange = { Value = 5000 },
};
private IEnumerable<TestDrawableTaikoMascot> mascots => this.ChildrenOfType<TestDrawableTaikoMascot>();
private IEnumerable<DrawableTaikoMascot> mascots => this.ChildrenOfType<DrawableTaikoMascot>();
private IEnumerable<TaikoPlayfield> playfields => this.ChildrenOfType<TaikoPlayfield>();
[Test]
public void TestStateTextures()
public void TestStateAnimations()
{
AddStep("set beatmap", () => setBeatmap());
AddStep("create mascot", () =>
{
SetContents(() => new TestDrawableTaikoMascot());
});
AddStep("clear state", () => setState(TaikoMascotAnimationState.Clear));
AddStep("kiai state", () => setState(TaikoMascotAnimationState.Kiai));
AddStep("fail state", () => setState(TaikoMascotAnimationState.Fail));
AddStep("idle state", () => setState(TaikoMascotAnimationState.Idle));
AddStep("clear state", () => SetContents(() => new TaikoMascotAnimation(TaikoMascotAnimationState.Clear)));
AddStep("idle state", () => SetContents(() => new TaikoMascotAnimation(TaikoMascotAnimationState.Idle)));
AddStep("kiai state", () => SetContents(() => new TaikoMascotAnimation(TaikoMascotAnimationState.Kiai)));
AddStep("fail state", () => SetContents(() => new TaikoMascotAnimation(TaikoMascotAnimationState.Fail)));
}
[Test]
@ -126,12 +121,6 @@ namespace osu.Game.Rulesets.Taiko.Tests.Skinning
});
}
private void setState(TaikoMascotAnimationState state)
{
foreach (var mascot in mascots)
mascot.State.Value = state;
}
private void addJudgement(HitResult result)
{
foreach (var playfield in playfields)
@ -144,15 +133,5 @@ namespace osu.Game.Rulesets.Taiko.Tests.Skinning
}
private bool assertState(TaikoMascotAnimationState state) => mascots.All(d => d.State.Value == state);
private class TestDrawableTaikoMascot : DrawableTaikoMascot
{
public TestDrawableTaikoMascot(TaikoMascotAnimationState startingState = TaikoMascotAnimationState.Idle)
: base(startingState)
{
}
public new Bindable<TaikoMascotAnimationState> State => base.State;
}
}
}

View File

@ -17,9 +17,10 @@ namespace osu.Game.Rulesets.Taiko.UI
{
public class DrawableTaikoMascot : BeatSyncedContainer
{
protected Bindable<TaikoMascotAnimationState> State { get; }
public IBindable<TaikoMascotAnimationState> State => state;
private readonly Dictionary<TaikoMascotAnimationState, TaikoMascotTextureAnimation> animations;
private readonly Bindable<TaikoMascotAnimationState> state;
private readonly Dictionary<TaikoMascotAnimationState, TaikoMascotAnimation> animations;
private Drawable currentAnimation;
private bool lastHitMissed;
@ -29,8 +30,8 @@ namespace osu.Game.Rulesets.Taiko.UI
{
RelativeSizeAxes = Axes.Both;
State = new Bindable<TaikoMascotAnimationState>(startingState);
animations = new Dictionary<TaikoMascotAnimationState, TaikoMascotTextureAnimation>();
state = new Bindable<TaikoMascotAnimationState>(startingState);
animations = new Dictionary<TaikoMascotAnimationState, TaikoMascotAnimation>();
}
[BackgroundDependencyLoader]
@ -38,10 +39,10 @@ namespace osu.Game.Rulesets.Taiko.UI
{
InternalChildren = new[]
{
animations[TaikoMascotAnimationState.Idle] = new TaikoMascotTextureAnimation(TaikoMascotAnimationState.Idle),
animations[TaikoMascotAnimationState.Clear] = new TaikoMascotTextureAnimation(TaikoMascotAnimationState.Clear),
animations[TaikoMascotAnimationState.Kiai] = new TaikoMascotTextureAnimation(TaikoMascotAnimationState.Kiai),
animations[TaikoMascotAnimationState.Fail] = new TaikoMascotTextureAnimation(TaikoMascotAnimationState.Fail),
animations[TaikoMascotAnimationState.Idle] = new TaikoMascotAnimation(TaikoMascotAnimationState.Idle),
animations[TaikoMascotAnimationState.Clear] = new TaikoMascotAnimation(TaikoMascotAnimationState.Clear),
animations[TaikoMascotAnimationState.Kiai] = new TaikoMascotAnimation(TaikoMascotAnimationState.Kiai),
animations[TaikoMascotAnimationState.Fail] = new TaikoMascotAnimation(TaikoMascotAnimationState.Fail),
};
updateState();
@ -69,7 +70,7 @@ namespace osu.Game.Rulesets.Taiko.UI
private void updateState()
{
State.Value = getNextState();
state.Value = getNextState();
}
private TaikoMascotAnimationState getNextState()

View File

@ -0,0 +1,116 @@
// 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.
using System;
using osu.Framework.Allocation;
using osu.Framework.Audio.Track;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Animations;
using osu.Framework.Graphics.Textures;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Graphics.Containers;
using osu.Game.Skinning;
namespace osu.Game.Rulesets.Taiko.UI
{
public sealed class TaikoMascotAnimation : BeatSyncedContainer
{
private readonly TextureAnimation textureAnimation;
private int currentFrame;
public TaikoMascotAnimation(TaikoMascotAnimationState state)
{
InternalChild = textureAnimation = createTextureAnimation(state).With(animation =>
{
animation.Origin = animation.Anchor = Anchor.BottomLeft;
RelativeSizeAxes = Axes.Both;
});
RelativeSizeAxes = Axes.Both;
Origin = Anchor = Anchor.BottomLeft;
}
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, TrackAmplitudes amplitudes)
{
// assume that if the animation is playing on its own, it's independent from the beat and doesn't need to be touched.
if (textureAnimation.FrameCount == 0 || textureAnimation.IsPlaying)
return;
textureAnimation.GotoFrame(currentFrame);
currentFrame = (currentFrame + 1) % textureAnimation.FrameCount;
}
private static TextureAnimation createTextureAnimation(TaikoMascotAnimationState state)
{
switch (state)
{
case TaikoMascotAnimationState.Clear:
return new ClearMascotTextureAnimation();
case TaikoMascotAnimationState.Idle:
case TaikoMascotAnimationState.Kiai:
case TaikoMascotAnimationState.Fail:
return new ManualMascotTextureAnimation(state);
default:
throw new ArgumentOutOfRangeException(nameof(state), $"Mascot animations for state {state} are not supported");
}
}
private class ManualMascotTextureAnimation : TextureAnimation
{
private readonly TaikoMascotAnimationState state;
public ManualMascotTextureAnimation(TaikoMascotAnimationState state)
{
this.state = state;
IsPlaying = false;
}
[BackgroundDependencyLoader]
private void load(ISkinSource skin)
{
for (int frameIndex = 0; true; frameIndex++)
{
var texture = getAnimationFrame(skin, state, frameIndex);
if (texture == null)
break;
AddFrame(texture);
}
}
}
private class ClearMascotTextureAnimation : TextureAnimation
{
private const float clear_animation_speed = 1000 / 10f;
private static readonly int[] clear_animation_sequence = { 0, 1, 2, 3, 4, 5, 6, 5, 6, 5, 4, 3, 2, 1, 0 };
public ClearMascotTextureAnimation()
{
DefaultFrameLength = clear_animation_speed;
}
[BackgroundDependencyLoader]
private void load(ISkinSource skin)
{
foreach (var frameIndex in clear_animation_sequence)
{
var texture = getAnimationFrame(skin, TaikoMascotAnimationState.Clear, frameIndex);
if (texture == null)
continue;
AddFrame(texture);
}
}
}
private static Texture getAnimationFrame(ISkinSource skin, TaikoMascotAnimationState state, int frameIndex)
=> skin.GetTexture($"pippidon{state.ToString().ToLower()}{frameIndex}");
}
}

View File

@ -1,109 +0,0 @@
// 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.
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Animations;
using osu.Game.Skinning;
namespace osu.Game.Rulesets.Taiko.UI
{
public sealed class TaikoMascotTextureAnimation : TextureAnimation
{
private const float clear_animation_speed = 1000 / 10f;
private static readonly int[] clear_animation_sequence = { 0, 1, 2, 3, 4, 5, 6, 5, 6, 5, 4, 3, 2, 1, 0 };
private int currentFrame;
public TaikoMascotAnimationState State { get; }
public TaikoMascotTextureAnimation(TaikoMascotAnimationState state)
: base(true)
{
State = state;
// We're animating on beat if it's not the clear animation
if (state == TaikoMascotAnimationState.Clear)
DefaultFrameLength = clear_animation_speed;
else
this.Stop();
Origin = Anchor.BottomLeft;
Anchor = Anchor.BottomLeft;
}
[BackgroundDependencyLoader]
private void load(ISkinSource skin)
{
if (State == TaikoMascotAnimationState.Clear)
{
foreach (var textureIndex in clear_animation_sequence)
{
if (!addFrame(skin, textureIndex))
break;
}
}
else
{
for (int i = 0; true; i++)
{
if (!addFrame(skin, i))
break;
}
}
}
private bool addFrame(ISkinSource skin, int textureIndex)
{
var textureName = getStateTextureName(textureIndex);
var texture = skin.GetTexture(textureName);
if (texture == null)
return false;
AddFrame(texture);
return true;
}
/// <summary>
/// Advances the current frame by one.
/// </summary>
public void Move()
{
// Check whether there are frames before causing a crash.
if (FrameCount == 0)
return;
if (currentFrame >= FrameCount)
currentFrame = 0;
GotoFrame(currentFrame);
currentFrame += 1;
}
private string getStateTextureName(int i) => $"pippidon{getStateString(State)}{i}";
private string getStateString(TaikoMascotAnimationState state)
{
switch (state)
{
case TaikoMascotAnimationState.Clear:
return "clear";
case TaikoMascotAnimationState.Fail:
return "fail";
case TaikoMascotAnimationState.Idle:
return "idle";
case TaikoMascotAnimationState.Kiai:
return "kiai";
default:
throw new ArgumentOutOfRangeException(nameof(state), $"There's no case for animation state {state} available");
}
}
}
}