Add sample lifetime constraints for taiko

This commit is contained in:
Bartłomiej Dach 2020-07-26 15:09:12 +02:00
parent c5b3220d8e
commit 648f9204f5
5 changed files with 74 additions and 59 deletions

View File

@ -0,0 +1,64 @@
// 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;
using osu.Framework.Graphics.Containers;
using osu.Game.Audio;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Skinning;
namespace osu.Game.Rulesets.Taiko.Audio
{
/// <summary>
/// Stores samples for the input drum.
/// The lifetime of the samples is adjusted so that they are only alive during the appropriate sample control point.
/// </summary>
public class DrumSampleContainer : LifetimeManagementContainer
{
private readonly ControlPointInfo controlPoints;
private readonly Dictionary<double, DrumSample> mappings = new Dictionary<double, DrumSample>();
public DrumSampleContainer(ControlPointInfo controlPoints)
{
this.controlPoints = controlPoints;
IReadOnlyList<SampleControlPoint> samplePoints = controlPoints.SamplePoints.Count == 0 ? new[] { controlPoints.SamplePointAt(double.MinValue) } : controlPoints.SamplePoints;
for (int i = 0; i < samplePoints.Count; i++)
{
var samplePoint = samplePoints[i];
var centre = samplePoint.GetSampleInfo();
var rim = samplePoint.GetSampleInfo(HitSampleInfo.HIT_CLAP);
var lifetimeStart = i > 0 ? samplePoint.Time : double.MinValue;
var lifetimeEnd = i + 1 < samplePoints.Count ? samplePoints[i + 1].Time : double.MaxValue;
mappings[samplePoint.Time] = new DrumSample
{
Centre = addSound(centre, lifetimeStart, lifetimeEnd),
Rim = addSound(rim, lifetimeStart, lifetimeEnd)
};
}
}
private SkinnableSound addSound(HitSampleInfo hitSampleInfo, double lifetimeStart, double lifetimeEnd)
{
var drawable = new SkinnableSound(hitSampleInfo)
{
LifetimeStart = lifetimeStart,
LifetimeEnd = lifetimeEnd
};
AddInternal(drawable);
return drawable;
}
public DrumSample SampleAt(double time) => mappings[controlPoints.SamplePointAt(time).Time];
public class DrumSample
{
public SkinnableSound Centre;
public SkinnableSound Rim;
}
}
}

View File

@ -1,52 +0,0 @@
// 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;
using osu.Game.Audio;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Skinning;
namespace osu.Game.Rulesets.Taiko.Audio
{
public class DrumSampleMapping
{
private readonly ControlPointInfo controlPoints;
private readonly Dictionary<double, DrumSample> mappings = new Dictionary<double, DrumSample>();
public readonly List<SkinnableSound> Sounds = new List<SkinnableSound>();
public DrumSampleMapping(ControlPointInfo controlPoints)
{
this.controlPoints = controlPoints;
IEnumerable<SampleControlPoint> samplePoints = controlPoints.SamplePoints.Count == 0 ? new[] { controlPoints.SamplePointAt(double.MinValue) } : controlPoints.SamplePoints;
foreach (var s in samplePoints)
{
var centre = s.GetSampleInfo();
var rim = s.GetSampleInfo(HitSampleInfo.HIT_CLAP);
mappings[s.Time] = new DrumSample
{
Centre = addSound(centre),
Rim = addSound(rim)
};
}
}
private SkinnableSound addSound(HitSampleInfo hitSampleInfo)
{
var drawable = new SkinnableSound(hitSampleInfo);
Sounds.Add(drawable);
return drawable;
}
public DrumSample SampleAt(double time) => mappings[controlPoints.SamplePointAt(time).Time];
public class DrumSample
{
public SkinnableSound Centre;
public SkinnableSound Rim;
}
}
}

View File

@ -111,7 +111,7 @@ private class LegacyHalfDrum : Container, IKeyBindingHandler<TaikoAction>
public readonly Sprite Centre;
[Resolved]
private DrumSampleMapping sampleMappings { get; set; }
private DrumSampleContainer sampleContainer { get; set; }
public LegacyHalfDrum(bool flipped)
{
@ -143,7 +143,7 @@ private void load(ISkinSource skin)
public bool OnPressed(TaikoAction action)
{
Drawable target = null;
var drumSample = sampleMappings.SampleAt(Time.Current);
var drumSample = sampleContainer.SampleAt(Time.Current);
if (action == CentreAction)
{

View File

@ -25,11 +25,11 @@ internal class InputDrum : Container
private const float middle_split = 0.025f;
[Cached]
private DrumSampleMapping sampleMapping;
private DrumSampleContainer sampleContainer;
public InputDrum(ControlPointInfo controlPoints)
{
sampleMapping = new DrumSampleMapping(controlPoints);
sampleContainer = new DrumSampleContainer(controlPoints);
RelativeSizeAxes = Axes.Both;
}
@ -69,7 +69,7 @@ private void load()
}
});
AddRangeInternal(sampleMapping.Sounds);
AddRangeInternal(sampleContainer.Sounds);
}
/// <summary>
@ -93,7 +93,7 @@ private class TaikoHalfDrum : Container, IKeyBindingHandler<TaikoAction>
private readonly Sprite centreHit;
[Resolved]
private DrumSampleMapping sampleMappings { get; set; }
private DrumSampleContainer sampleContainer { get; set; }
public TaikoHalfDrum(bool flipped)
{
@ -154,7 +154,7 @@ public bool OnPressed(TaikoAction action)
Drawable target = null;
Drawable back = null;
var drumSample = sampleMappings.SampleAt(Time.Current);
var drumSample = sampleContainer.SampleAt(Time.Current);
if (action == CentreAction)
{

View File

@ -22,6 +22,9 @@ public class SkinnableSound : SkinReloadableDrawable
[Resolved]
private ISampleStore samples { get; set; }
public override bool RemoveWhenNotAlive => false;
public override bool RemoveCompletedTransforms => false;
public SkinnableSound(ISampleInfo hitSamples)
: this(new[] { hitSamples })
{