Skip to content

Commit

Permalink
Cache IFrameStableClock in Player for easier access
Browse files Browse the repository at this point in the history
Allows directly referencing rather than going through `DrawableRuleset`.
Helps with testing and implementation of the new song progress display
(ppy#22144).
  • Loading branch information
peppy committed Jan 18, 2023
1 parent f7c4199 commit 7d0388c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ public partial class ClicksPerSecondCalculator : Component
[Resolved]
private IGameplayClock gameplayClock { get; set; } = null!;

[Resolved(canBeNull: true)]
private DrawableRuleset? drawableRuleset { get; set; }
[Resolved]
private IFrameStableClock? frameStableClock { get; set; }

public int Value { get; private set; }

// Even though `FrameStabilityContainer` caches as a `GameplayClock`, we need to check it directly via `drawableRuleset`
// as this calculator is not contained within the `FrameStabilityContainer` and won't see the dependency.
private IGameplayClock clock => drawableRuleset?.FrameStableClock ?? gameplayClock;
private IGameplayClock clock => frameStableClock ?? gameplayClock;

public ClicksPerSecondCalculator()
{
Expand Down
18 changes: 10 additions & 8 deletions osu.Game/Screens/Play/HUD/SongProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ public abstract partial class SongProgress : OverlayContainer, ISkinnableDrawabl
[Resolved]
protected IGameplayClock GameplayClock { get; private set; } = null!;

[Resolved(canBeNull: true)]
private DrawableRuleset? drawableRuleset { get; set; }
[Resolved]
private IFrameStableClock? frameStableClock { get; set; }

/// <summary>
/// The reference clock is used to accurately tell the current playfield's time (including catch-up lag).
/// However, if none is available (i.e. used in tests), we fall back to the gameplay clock.
/// </summary>
protected IClock FrameStableClock => frameStableClock ?? GameplayClock;

private IClock? referenceClock;
private IEnumerable<HitObject>? objects;

public IEnumerable<HitObject> Objects
Expand Down Expand Up @@ -58,12 +63,11 @@ protected override void LoadComplete()
protected virtual void UpdateObjects(IEnumerable<HitObject> objects) { }

[BackgroundDependencyLoader]
private void load()
private void load(DrawableRuleset? drawableRuleset)
{
if (drawableRuleset != null)
{
Objects = drawableRuleset.Objects;
referenceClock = drawableRuleset.FrameStableClock;
}
}

Expand All @@ -74,9 +78,7 @@ protected override void Update()
if (objects == null)
return;

// The reference clock is used to accurately tell the playfield's time. This is obtained from the drawable ruleset.
// However, if no drawable ruleset is available (i.e. used in tests), we fall back to the gameplay clock.
double currentTime = referenceClock?.CurrentTime ?? GameplayClock.CurrentTime;
double currentTime = FrameStableClock.CurrentTime;

bool isInIntro = currentTime < FirstHitTime;

Expand Down
1 change: 1 addition & 0 deletions osu.Game/Screens/Play/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ private void load(AudioManager audio, OsuConfigManager config, OsuGameBase game,

DrawableRuleset = ruleset.CreateDrawableRulesetWith(playableBeatmap, gameplayMods);
dependencies.CacheAs(DrawableRuleset);
dependencies.CacheAs(DrawableRuleset.FrameStableClock);

ScoreProcessor = ruleset.CreateScoreProcessor();
ScoreProcessor.Mods.Value = gameplayMods;
Expand Down

0 comments on commit 7d0388c

Please sign in to comment.