1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-20 00:52:56 +08:00
osu-lazer/osu.Game/Beatmaps/Objects/DrawableHitObject.cs

92 lines
2.3 KiB
C#
Raw Normal View History

//Copyright (c) 2007-2016 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 System.Linq;
using System.Text;
using System.Threading.Tasks;
using osu.Framework;
using osu.Framework.Graphics.Containers;
using osu.Game.Beatmaps.Objects.Osu.Drawable;
namespace osu.Game.Beatmaps.Objects
{
public abstract class DrawableHitObject : Container, IStateful<ArmedState>
{
//todo: move to a more central implementation. this logic should not be at a drawable level.
public Action<DrawableHitObject> OnHit;
public Action<DrawableHitObject> OnMiss;
public Func<DrawableHitObject, bool> AllowHit;
public HitObject HitObject;
public DrawableHitObject(HitObject hitObject)
{
HitObject = hitObject;
2016-11-02 18:42:07 +08:00
Depth = -(float)hitObject.StartTime;
}
private ArmedState state;
public ArmedState State
{
get { return state; }
set
{
if (state == value) return;
state = value;
UpdateState(state);
}
}
protected double? HitTime;
protected virtual bool Hit()
{
if (State != ArmedState.Disarmed)
return false;
if (AllowHit?.Invoke(this) == false)
return false;
HitTime = Time.Current;
State = ArmedState.Armed;
return true;
}
2016-11-01 22:24:14 +08:00
protected override void LoadComplete()
{
2016-11-01 22:24:14 +08:00
base.LoadComplete();
UpdateState(state);
}
private bool counted;
protected override void Update()
{
base.Update();
if (Time.Current >= HitObject.EndTime && !counted)
{
counted = true;
if (state == ArmedState.Armed)
OnHit?.Invoke(this);
else
OnMiss?.Invoke(this);
}
}
protected abstract void UpdateState(ArmedState state);
}
public enum ArmedState
{
Disarmed,
Armed
}
}