1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 23:12:56 +08:00

Make an interface for beatmaps

This commit is contained in:
smoogipoo 2018-04-19 18:14:21 +09:00
parent dd7020d5a4
commit b5a55a0dce
5 changed files with 79 additions and 62 deletions

View File

@ -12,47 +12,52 @@ using osu.Game.IO.Serialization.Converters;
namespace osu.Game.Beatmaps namespace osu.Game.Beatmaps
{ {
/// <summary> public interface IBeatmap : IJsonSerializable
/// A Beatmap containing converted HitObjects.
/// </summary>
public class Beatmap<T> : IJsonSerializable
where T : HitObject
{ {
public BeatmapInfo BeatmapInfo = new BeatmapInfo(); /// <summary>
public ControlPointInfo ControlPointInfo = new ControlPointInfo(); /// This beatmap's info.
public List<BreakPeriod> Breaks = new List<BreakPeriod>(); /// </summary>
BeatmapInfo BeatmapInfo { get; }
[JsonIgnore]
public BeatmapMetadata Metadata => BeatmapInfo?.Metadata ?? BeatmapInfo?.BeatmapSet?.Metadata;
/// <summary> /// <summary>
/// The HitObjects this Beatmap contains. /// This beatmap's metadata.
/// </summary> /// </summary>
[JsonConverter(typeof(TypedListConverter<HitObject>))] BeatmapMetadata Metadata { get; }
public List<T> HitObjects = new List<T>();
/// <summary>
/// The control points in this beatmap.
/// </summary>
ControlPointInfo ControlPointInfo { get; }
/// <summary>
/// The breaks in this beatmap.
/// </summary>
List<BreakPeriod> Breaks { get; }
/// <summary> /// <summary>
/// Total amount of break time in the beatmap. /// Total amount of break time in the beatmap.
/// </summary> /// </summary>
[JsonIgnore] double TotalBreakTime { get; }
public double TotalBreakTime => Breaks.Sum(b => b.Duration);
/// <summary> /// <summary>
/// Constructs a new beatmap. /// The hitobjects contained by this beatmap.
/// </summary> /// </summary>
/// <param name="original">The original beatmap to use the parameters of.</param> IEnumerable<HitObject> HitObjects { get; }
public Beatmap(Beatmap<T> original = null)
{
BeatmapInfo = original?.BeatmapInfo.DeepClone() ?? BeatmapInfo;
ControlPointInfo = original?.ControlPointInfo ?? ControlPointInfo;
Breaks = original?.Breaks ?? Breaks;
HitObjects = original?.HitObjects ?? HitObjects;
if (original == null && Metadata == null) /// <summary>
/// Creates a shallow-clone of this beatmap and returns it.
/// </summary>
/// <returns>The shallow-cloned beatmap.</returns>
IBeatmap Clone();
}
/// <summary>
/// A Beatmap containing converted HitObjects.
/// </summary>
public class Beatmap<T> : IBeatmap
where T : HitObject
{ {
// we may have no metadata in cases we weren't sourced from the database. public BeatmapInfo BeatmapInfo { get; set; } = new BeatmapInfo
// let's fill it (and other related fields) so we don't need to null-check it in future usages.
BeatmapInfo = new BeatmapInfo
{ {
Metadata = new BeatmapMetadata Metadata = new BeatmapMetadata
{ {
@ -63,26 +68,41 @@ namespace osu.Game.Beatmaps
Version = @"Normal", Version = @"Normal",
BaseDifficulty = new BeatmapDifficulty() BaseDifficulty = new BeatmapDifficulty()
}; };
}
} [JsonIgnore]
} public BeatmapMetadata Metadata => BeatmapInfo?.Metadata ?? BeatmapInfo?.BeatmapSet?.Metadata;
public ControlPointInfo ControlPointInfo { get; set; } = new ControlPointInfo();
public List<BreakPeriod> Breaks { get; set; } = new List<BreakPeriod>();
/// <summary> /// <summary>
/// A Beatmap containing un-converted HitObjects. /// Total amount of break time in the beatmap.
/// </summary> /// </summary>
[JsonIgnore]
public double TotalBreakTime => Breaks.Sum(b => b.Duration);
/// <summary>
/// The HitObjects this Beatmap contains.
/// </summary>
[JsonConverter(typeof(TypedListConverter<HitObject>))]
public List<T> HitObjects = new List<T>();
IEnumerable<HitObject> IBeatmap.HitObjects => HitObjects;
public Beatmap<T> Clone() => new Beatmap<T>
{
BeatmapInfo = BeatmapInfo.DeepClone(),
ControlPointInfo = ControlPointInfo,
Breaks = Breaks,
HitObjects = HitObjects
};
IBeatmap IBeatmap.Clone() => Clone();
}
public class Beatmap : Beatmap<HitObject> public class Beatmap : Beatmap<HitObject>
{ {
/// <summary> public new Beatmap Clone() => (Beatmap)base.Clone();
/// Constructs a new beatmap.
/// </summary>
/// <param name="original">The original beatmap to use the parameters of.</param>
public Beatmap(Beatmap original)
: base(original)
{
}
public Beatmap()
{
}
} }
} }

View File

@ -37,7 +37,7 @@ namespace osu.Game.Beatmaps
public Beatmap<T> Convert(Beatmap original) public Beatmap<T> Convert(Beatmap original)
{ {
// We always operate on a clone of the original beatmap, to not modify it game-wide // We always operate on a clone of the original beatmap, to not modify it game-wide
return ConvertBeatmap(new Beatmap(original)); return ConvertBeatmap(original.Clone());
} }
void IBeatmapConverter.Convert(Beatmap original) => Convert(original); void IBeatmapConverter.Convert(Beatmap original) => Convert(original);

View File

@ -8,14 +8,5 @@ namespace osu.Game.Beatmaps.Legacy
/// </summary> /// </summary>
public class LegacyBeatmap : Beatmap public class LegacyBeatmap : Beatmap
{ {
/// <summary>
/// Constructs a new beatmap.
/// </summary>
/// <param name="original">The original beatmap to use the parameters of.</param>
internal LegacyBeatmap(Beatmap original = null)
: base(original)
{
HitObjects = original?.HitObjects;
}
} }
} }

View File

@ -18,11 +18,11 @@ namespace osu.Game.Screens.Play
private const float remaining_time_container_max_size = 0.3f; private const float remaining_time_container_max_size = 0.3f;
private const int vertical_margin = 25; private const int vertical_margin = 25;
private List<BreakPeriod> breaks; private IReadOnlyList<BreakPeriod> breaks;
private readonly Container fadeContainer; private readonly Container fadeContainer;
public List<BreakPeriod> Breaks public IReadOnlyList<BreakPeriod> Breaks
{ {
get => breaks; get => breaks;
set set

View File

@ -12,8 +12,14 @@ namespace osu.Game.Tests.Beatmaps
public class TestBeatmap : Beatmap public class TestBeatmap : Beatmap
{ {
public TestBeatmap(RulesetInfo ruleset) public TestBeatmap(RulesetInfo ruleset)
: base(createTestBeatmap())
{ {
var baseBeatmap = createTestBeatmap();
BeatmapInfo = baseBeatmap.BeatmapInfo;
ControlPointInfo = baseBeatmap.ControlPointInfo;
Breaks = baseBeatmap.Breaks;
HitObjects = baseBeatmap.HitObjects;
BeatmapInfo.Ruleset = ruleset; BeatmapInfo.Ruleset = ruleset;
} }