osu/osu.Game.Rulesets.Taiko/UI/DrawableTaikoMascot.cs

124 lines
4.8 KiB
C#
Raw Normal View History

2020-04-24 05:57:16 +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.
using System.Collections.Generic;
2020-04-24 05:57:16 +00:00
using osu.Framework.Allocation;
using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Textures;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Graphics.Containers;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Taiko.Judgements;
2020-05-11 20:53:05 +00:00
using osu.Game.Screens.Play;
namespace osu.Game.Rulesets.Taiko.UI
{
2020-04-26 23:40:57 +00:00
public class DrawableTaikoMascot : BeatSyncedContainer
{
2020-05-11 20:53:05 +00:00
public readonly Bindable<TaikoMascotAnimationState> State;
public readonly Bindable<JudgementResult> LastResult;
private readonly Dictionary<TaikoMascotAnimationState, TaikoMascotAnimation> animations;
private TaikoMascotAnimation currentAnimation;
2020-04-30 20:03:39 +00:00
private bool lastObjectHit = true;
private bool kiaiMode;
public DrawableTaikoMascot(TaikoMascotAnimationState startingState = TaikoMascotAnimationState.Idle)
{
Origin = Anchor = Anchor.BottomLeft;
2020-04-26 23:40:57 +00:00
2020-05-11 20:53:05 +00:00
State = new Bindable<TaikoMascotAnimationState>(startingState);
LastResult = new Bindable<JudgementResult>();
animations = new Dictionary<TaikoMascotAnimationState, TaikoMascotAnimation>();
}
2020-05-11 20:53:05 +00:00
[BackgroundDependencyLoader(true)]
private void load(TextureStore textures, GameplayBeatmap gameplayBeatmap)
{
InternalChildren = new[]
{
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),
};
2020-05-11 20:53:05 +00:00
if (gameplayBeatmap != null)
((IBindable<JudgementResult>)LastResult).BindTo(gameplayBeatmap.LastJudgementResult);
2020-04-26 23:40:57 +00:00
}
protected override void LoadComplete()
2020-04-26 23:40:57 +00:00
{
base.LoadComplete();
2020-04-26 23:40:57 +00:00
animations.Values.ForEach(animation => animation.Hide());
2020-05-11 20:53:05 +00:00
State.BindValueChanged(mascotStateChanged, true);
LastResult.BindValueChanged(onNewResult);
2020-04-26 23:40:57 +00:00
}
2020-05-11 20:53:05 +00:00
private void onNewResult(ValueChangedEvent<JudgementResult> resultChangedEvent)
2020-04-27 21:19:18 +00:00
{
2020-05-11 20:53:05 +00:00
var result = resultChangedEvent.NewValue;
if (result == null)
return;
// TODO: missing support for clear/fail state transition at end of beatmap gameplay
if (triggerComboClear(result) || triggerSwellClear(result))
{
2020-05-11 20:53:05 +00:00
State.Value = TaikoMascotAnimationState.Clear;
2020-04-30 22:29:03 +00:00
// always consider a clear equivalent to a hit to avoid clear -> miss transitions
2020-04-30 20:16:25 +00:00
lastObjectHit = true;
}
if (!result.Judgement.AffectsCombo)
return;
lastObjectHit = result.IsHit;
2020-04-27 21:19:18 +00:00
}
2020-06-23 04:49:18 +00:00
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, ChannelAmplitudes amplitudes)
2020-04-26 23:40:57 +00:00
{
kiaiMode = effectPoint.KiaiMode;
2020-04-26 23:40:57 +00:00
}
protected override void Update()
2020-04-26 23:40:57 +00:00
{
base.Update();
2020-05-11 20:53:05 +00:00
State.Value = getNextState();
}
private TaikoMascotAnimationState getNextState()
{
// don't change state if current animation is still playing (and we haven't rewound before it).
// used for clear state - others are manually animated on new beats.
if (currentAnimation?.Completed == false && currentAnimation.DisplayTime <= Time.Current)
2020-05-11 20:53:05 +00:00
return State.Value;
if (!lastObjectHit)
return TaikoMascotAnimationState.Fail;
return kiaiMode ? TaikoMascotAnimationState.Kiai : TaikoMascotAnimationState.Idle;
}
private void mascotStateChanged(ValueChangedEvent<TaikoMascotAnimationState> state)
{
currentAnimation?.Hide();
currentAnimation = animations[state.NewValue];
currentAnimation.Show();
}
private bool triggerComboClear(JudgementResult judgementResult)
=> (judgementResult.ComboAtJudgement + 1) % 50 == 0 && judgementResult.Judgement.AffectsCombo && judgementResult.IsHit;
private bool triggerSwellClear(JudgementResult judgementResult)
=> judgementResult.Judgement is TaikoSwellJudgement && judgementResult.IsHit;
}
}