1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 04:49:40 +08:00
osu-lazer/osu.Game/Rulesets/Objects/Drawables/DrawableHitObject.cs

203 lines
6.2 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
2016-12-08 18:54:22 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2017-04-18 15:05:58 +08:00
using osu.Game.Rulesets.Judgements;
using Container = osu.Framework.Graphics.Containers.Container;
2017-04-18 15:05:58 +08:00
using osu.Game.Rulesets.Objects.Types;
using OpenTK.Graphics;
using osu.Game.Audio;
using System.Linq;
2017-04-18 15:05:58 +08:00
namespace osu.Game.Rulesets.Objects.Drawables
{
public abstract class DrawableHitObject : Container
{
public readonly HitObject HitObject;
/// <summary>
/// The colour used for various elements of this DrawableHitObject.
/// </summary>
public virtual Color4 AccentColour { get; set; }
protected DrawableHitObject(HitObject hitObject)
{
HitObject = hitObject;
}
}
public abstract class DrawableHitObject<TObject> : DrawableHitObject
where TObject : HitObject
{
public new readonly TObject HitObject;
protected DrawableHitObject(TObject hitObject)
: base(hitObject)
{
HitObject = hitObject;
}
}
public abstract class DrawableHitObject<TObject, TJudgement> : DrawableHitObject<TObject>
where TObject : HitObject
where TJudgement : Judgement
{
public event Action<DrawableHitObject<TObject, TJudgement>> OnJudgement;
public override bool HandleInput => Interactive;
public bool Interactive = true;
public TJudgement Judgement;
protected List<SampleChannel> Samples = new List<SampleChannel>();
protected DrawableHitObject(TObject hitObject)
: base(hitObject)
{
}
private ArmedState state;
public ArmedState State
{
get { return state; }
set
{
if (state == value)
return;
state = value;
if (!IsLoaded)
return;
UpdateState(state);
2016-12-08 18:54:22 +08:00
if (State == ArmedState.Hit)
PlaySamples();
}
}
protected void PlaySamples()
{
Samples.ForEach(s => s?.Play());
}
protected override void LoadComplete()
{
base.LoadComplete();
//force application of the state that was set before we loaded.
UpdateState(State);
}
/// <summary>
/// Whether this hit object and all of its nested hit objects have been judged.
/// </summary>
2017-05-11 16:16:01 +08:00
public bool Judged => (Judgement?.Result ?? HitResult.None) != HitResult.None && (NestedHitObjects?.All(h => h.Judged) ?? true);
/// <summary>
/// Process a hit of this hitobject. Carries out judgement.
/// </summary>
/// <returns>Whether a hit was processed.</returns>
protected bool UpdateJudgement(bool userTriggered)
{
2017-08-21 14:35:16 +08:00
if (Judgement == null)
return false;
var partial = Judgement as IPartialJudgement;
2017-03-31 10:54:12 +08:00
// Never re-process non-partial hits
if (Judgement.Result != HitResult.None && partial == null)
return false;
// Update the judgement state
double endTime = (HitObject as IHasEndTime)?.EndTime ?? HitObject.StartTime;
Judgement.TimeOffset = Time.Current - endTime;
// Update the judgement state
bool hadResult = Judgement.Result != HitResult.None;
CheckJudgement(userTriggered);
// Don't process judgements with no result
if (Judgement.Result == HitResult.None)
return false;
// Don't process judgements that previously had results but the results were unchanged
if (hadResult && partial?.Changed != true)
return false;
switch (Judgement.Result)
{
default:
State = ArmedState.Hit;
break;
case HitResult.Miss:
State = ArmedState.Miss;
break;
}
OnJudgement?.Invoke(this);
if (partial != null)
partial.Changed = false;
return true;
}
protected virtual void CheckJudgement(bool userTriggered)
{
if (NestedHitObjects != null)
{
foreach (var d in NestedHitObjects)
d.CheckJudgement(userTriggered);
}
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
UpdateJudgement(false);
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
foreach (SampleInfo sample in HitObject.Samples)
{
SampleChannel channel = audio.Sample.Get($@"Gameplay/{sample.Bank}-{sample.Name}");
if (channel == null)
continue;
channel.Volume.Value = sample.Volume;
Samples.Add(channel);
}
//we may be setting a custom judgement in test cases or what not.
if (Judgement == null)
Judgement = CreateJudgement();
2017-03-06 12:59:11 +08:00
}
private List<DrawableHitObject<TObject, TJudgement>> nestedHitObjects;
protected IEnumerable<DrawableHitObject<TObject, TJudgement>> NestedHitObjects => nestedHitObjects;
2017-03-06 12:59:11 +08:00
protected virtual void AddNested(DrawableHitObject<TObject, TJudgement> h)
2017-03-06 12:59:11 +08:00
{
if (nestedHitObjects == null)
nestedHitObjects = new List<DrawableHitObject<TObject, TJudgement>>();
2017-03-06 12:59:11 +08:00
h.OnJudgement += d => OnJudgement?.Invoke(d);
2017-03-06 12:59:11 +08:00
nestedHitObjects.Add(h);
}
protected abstract TJudgement CreateJudgement();
protected abstract void UpdateState(ArmedState state);
}
}