1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-18 05:06:14 +08:00
osu-lazer/osu.Game/Modes/Objects/Drawables/DrawableHitObject.cs

159 lines
4.7 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;
using osu.Framework;
2016-12-08 18:54:22 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Game.Beatmaps.Samples;
using osu.Game.Modes.Judgements;
using Container = osu.Framework.Graphics.Containers.Container;
using osu.Game.Modes.Objects.Types;
2016-11-14 18:49:29 +08:00
namespace osu.Game.Modes.Objects.Drawables
{
public abstract class DrawableHitObject<TJudgement> : Container, IStateful<ArmedState>
where TJudgement : JudgementInfo
{
public override bool HandleInput => Interactive;
public bool Interactive = true;
public TJudgement Judgement;
protected abstract TJudgement CreateJudgementInfo();
2017-03-06 12:59:11 +08:00
protected abstract void UpdateState(ArmedState state);
private ArmedState state;
public ArmedState State
{
get { return state; }
set
{
if (state == value) return;
state = value;
UpdateState(state);
if (IsLoaded)
Expire();
2016-12-08 18:54:22 +08:00
if (State == ArmedState.Hit)
PlaySample();
}
}
2017-03-06 12:59:11 +08:00
protected SampleChannel Sample;
2016-12-08 18:54:22 +08:00
2017-02-16 12:39:01 +08:00
protected virtual void PlaySample()
2016-12-08 18:54:22 +08:00
{
2017-03-06 12:59:11 +08:00
Sample?.Play();
2016-12-08 18:54:22 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
//we may be setting a custom judgement in test cases or what not.
if (Judgement == null)
Judgement = CreateJudgementInfo();
//force application of the state that was set before we loaded.
UpdateState(State);
Expire(true);
}
2017-03-06 12:59:11 +08:00
}
public abstract class DrawableHitObject<TObject, TJudgement> : DrawableHitObject<TJudgement>
2017-03-15 15:29:14 +08:00
where TObject : HitObject
where TJudgement : JudgementInfo
2017-03-06 12:59:11 +08:00
{
public event Action<DrawableHitObject<TObject, TJudgement>> OnJudgement;
2017-03-15 15:29:14 +08:00
public TObject HitObject;
2017-03-15 15:29:14 +08:00
protected DrawableHitObject(TObject hitObject)
{
2017-03-06 12:59:11 +08:00
HitObject = hitObject;
}
/// <summary>
/// Process a hit of this hitobject. Carries out judgement.
/// </summary>
/// <returns>Whether a hit was processed.</returns>
protected bool UpdateJudgement(bool userTriggered)
{
if (Judgement.Result != null)
return false;
double endTime = (HitObject as IHasEndTime)?.EndTime ?? HitObject.StartTime;
Judgement.TimeOffset = Time.Current - endTime;
CheckJudgement(userTriggered);
if (Judgement.Result == null)
return false;
switch (Judgement.Result)
{
default:
State = ArmedState.Hit;
break;
case HitResult.Miss:
State = ArmedState.Miss;
break;
}
OnJudgement?.Invoke(this);
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);
}
2017-03-06 12:59:11 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
SampleType type = HitObject.Sample?.Type ?? SampleType.None;
if (type == SampleType.None)
type = SampleType.Normal;
2017-03-06 12:59:11 +08:00
SampleSet sampleSet = HitObject.Sample?.Set ?? SampleSet.Normal;
Sample = audio.Sample.Get($@"Gameplay/{sampleSet.ToString().ToLower()}-hit{type.ToString().ToLower()}");
2017-03-06 12:59:11 +08:00
}
private List<DrawableHitObject<TObject, TJudgement>> nestedHitObjects;
2017-03-06 12:59:11 +08:00
protected IEnumerable<DrawableHitObject<TObject, TJudgement>> NestedHitObjects => nestedHitObjects;
2017-03-06 12:59:11 +08:00
protected 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);
}
}
}