1
0
mirror of https://github.com/ppy/osu.git synced 2026-05-16 19:23:11 +08:00
Files
osu-lazer/osu.Game/Screens/Utility/ScrollingGameplay.cs
T
Bartłomiej Dach 8b542e5442 Refactor hit windows class structure to reduce rigidity
This change pulls back a significant degree of overspecialisation and
rigidity in the class structure of `HitWindows` to make subsequent
changes to hit windows, whose purpose is to improve replay playback
accuracy, possible to do cleanly.

Notably:

- `HitWindows` is full abstract now. In a few use cases, and as a
  reference for ruleset implementors, `DefaultHitWindows` is provided as
  a separate class instead.

  This fixes the weirdness wherein `HitWindows` always declared 6 fields
  for result types but some of them would never be set to a non-zero
  value or read.

- `HitWindow.GetRanges()` is deleted because it is overspecialised and
  prevents being able to adjust hitwindows by ±0.5ms cleanly which will
  be required later.

  The fallout of this is that the assertion that used `GetRanges()` in
  the `HitWindows` ctor must use something else now, and the closest
  thing to it was `GetAllAvailableWindows()`, which didn't return
  the miss window - so I made it return the miss window and fixed the
  one consumer that didn't want it (bar hit error meter) to skip it.

- Diff also contains some clean-up around `DifficultyRange` to unify
  handling of it.
2025-06-25 08:40:12 +02:00

201 lines
6.1 KiB
C#

// 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;
using System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Framework.Input.States;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Scoring;
using osu.Game.Screens.Utility.SampleComponents;
using osuTK;
using osuTK.Input;
namespace osu.Game.Screens.Utility
{
public partial class ScrollingGameplay : LatencySampleComponent
{
private const float judgement_position = 0.8f;
private const float bar_height = 20;
private int nextLocation;
private readonly List<HitEvent> hitEvents = new List<HitEvent>();
private double? lastGeneratedBeatTime;
private Container circles = null!;
protected override void LoadComplete()
{
base.LoadComplete();
InternalChildren = new Drawable[]
{
new Box
{
Name = "judgement bar",
Colour = OverlayColourProvider.Content2,
RelativeSizeAxes = Axes.X,
RelativePositionAxes = Axes.Y,
Y = judgement_position,
Height = bar_height,
},
circles = new Container
{
RelativeSizeAxes = Axes.Both,
},
};
SampleBPM.BindValueChanged(_ =>
{
circles.Clear();
lastGeneratedBeatTime = null;
});
}
protected override void UpdateAtLimitedRate(InputState inputState)
{
double beatLength = 60000 / SampleBPM.Value;
int nextBeat = (int)(Clock.CurrentTime / beatLength) + 1;
// We want to generate a few hit objects ahead of the current time (to allow them to animate).
double generateUpTo = (nextBeat + 2) * beatLength;
while (lastGeneratedBeatTime == null || lastGeneratedBeatTime < generateUpTo)
{
double time = ++nextBeat * beatLength;
if (time <= lastGeneratedBeatTime)
continue;
newBeat(time);
lastGeneratedBeatTime = time;
}
}
private void newBeat(double time)
{
const float columns = 4;
float adjustedXPos = ((1f + nextLocation++ % columns) - columns / 2) / columns;
circles.Add(new SampleNote(time)
{
RelativePositionAxes = Axes.Both,
X = 0.5f + SampleVisualSpacing.Value * (adjustedXPos * 0.5f),
Scale = new Vector2(0.4f + (0.8f * SampleVisualSpacing.Value), 1),
Hit = hit,
});
}
private void hit(HitEvent h)
{
hitEvents.Add(h);
}
public partial class SampleNote : LatencySampleComponent
{
public HitEvent? HitEvent;
public Action<HitEvent>? Hit { get; set; }
public readonly double HitTime;
private Box box = null!;
private const float size = 100;
private const float duration = 200;
public SampleNote(double hitTime)
{
HitTime = hitTime;
Origin = Anchor.Centre;
AutoSizeAxes = Axes.Both;
AlwaysPresent = true;
}
[BackgroundDependencyLoader]
private void load()
{
InternalChildren = new Drawable[]
{
box = new Box
{
Colour = OverlayColourProvider.Content1,
Size = new Vector2(size, bar_height),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
};
}
protected override bool OnKeyDown(KeyDownEvent e)
{
if (!IsActive.Value)
return false;
if (Math.Abs(Clock.CurrentTime - HitTime) > duration)
return false;
// Allow using any key that isn't used by the latency certifier itself.
switch (e.Key)
{
case Key.Space:
case Key.Number1:
case Key.Number2:
case Key.Tab:
return false;
}
attemptHit();
return true;
}
protected override void UpdateAtLimitedRate(InputState inputState)
{
if (HitEvent == null)
{
double preempt = (float)IBeatmapDifficultyInfo.DifficultyRange(SampleApproachRate.Value, 1800, 1200, 450);
Alpha = (float)Math.Clamp((Clock.CurrentTime - HitTime + 600) / 400, 0, 1);
Y = judgement_position - (float)((HitTime - Clock.CurrentTime) / preempt);
if (Clock.CurrentTime > HitTime + duration)
Expire();
}
}
private void attemptHit() => Schedule(() =>
{
if (HitEvent != null)
return;
// in case it was hit outside of display range, show immediately
// so the user isn't confused.
this.FadeIn();
box
.FadeOut(duration / 2)
.ScaleTo(1.5f, duration / 2);
HitEvent = new HitEvent(Clock.CurrentTime - HitTime, 1.0, HitResult.Good, new HitObject
{
HitWindows = new DefaultHitWindows(),
}, null, null);
Hit?.Invoke(HitEvent.Value);
this.Delay(duration).Expire();
});
}
}
}