Skip to content

Commit

Permalink
optimize JudgementCounter by not using linq
Browse files Browse the repository at this point in the history
  • Loading branch information
flustix committed Dec 17, 2024
1 parent 4f110ba commit 78f1778
Showing 1 changed file with 55 additions and 53 deletions.
108 changes: 55 additions & 53 deletions fluXis.Game/Screens/Gameplay/HUD/Components/JudgementCounter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using fluXis.Game.Graphics;
using fluXis.Game.Graphics.Sprites;
using fluXis.Game.Graphics.UserInterface.Color;
using fluXis.Game.Scoring;
using fluXis.Game.Scoring.Enums;
using fluXis.Game.Scoring.Processing;
using fluXis.Game.Scoring.Structs;
using fluXis.Game.Skinning;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
Expand All @@ -24,6 +26,16 @@ private void load()
Masking = true;
EdgeEffect = FluXisStyles.ShadowSmall;

var dict = new Dictionary<Judgement, string>
{
{ Judgement.Flawless, "FL" },
{ Judgement.Perfect, "PF" },
{ Judgement.Great, "GR" },
{ Judgement.Alright, "AL" },
{ Judgement.Okay, "OK" },
{ Judgement.Miss, "MS" },
};

Children = new Drawable[]
{
new Box
Expand All @@ -36,25 +48,27 @@ private void load()
Direction = FillDirection.Vertical,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = Screen.HitWindows.GetTimings().Select(t => new CounterItem(ScoreProcessor, t)).ToArray()
Children = Screen.HitWindows.GetTimings().Select(t => new CounterItem(JudgementProcessor, t, dict[t.Judgement])).ToArray()
}
};
}

private partial class CounterItem : Container
{
private readonly ScoreProcessor scoreProcessor;
private readonly Timing timing;
private JudgementProcessor processor { get; }
private Timing timing { get; }
private string defaultText { get; }

private int count;

private Box background;
private FluXisSpriteText text;

public CounterItem(ScoreProcessor scoreProcessor, Timing timing)
public CounterItem(JudgementProcessor processor, Timing timing, string defaultText)
{
this.scoreProcessor = scoreProcessor;
this.processor = processor;
this.timing = timing;
this.defaultText = defaultText;
}

[BackgroundDependencyLoader]
Expand All @@ -73,6 +87,7 @@ private void load(SkinManager skinManager)
},
text = new FluXisSpriteText
{
Text = defaultText,
Font = FluXisFont.YoureGone,
FontSize = 24,
Anchor = Anchor.Centre,
Expand All @@ -82,69 +97,56 @@ private void load(SkinManager skinManager)
};
}

private int getCount()
protected override void LoadComplete()
{
switch (timing.Judgement)
{
case Judgement.Miss:
return scoreProcessor.Miss;

case Judgement.Okay:
return scoreProcessor.Okay;
base.LoadComplete();

case Judgement.Alright:
return scoreProcessor.Alright;
processor.ResultAdded += addResult;
processor.ResultReverted += revertResult;
}

case Judgement.Great:
return scoreProcessor.Great;
protected override void Dispose(bool isDisposing)
{
processor.ResultAdded -= addResult;
processor.ResultReverted -= revertResult;

case Judgement.Perfect:
return scoreProcessor.Perfect;
base.Dispose(isDisposing);
}

case Judgement.Flawless:
return scoreProcessor.Flawless;
private void addResult(HitResult result)
{
if (result.Judgement != timing.Judgement)
return;

default:
return 0;
}
count++;
updateText();
lightUp();
}

protected override void Update()
private void revertResult(HitResult result)
{
var actualCount = getCount();
if (result.Judgement != timing.Judgement)
return;

if (actualCount != 0)
{
if (actualCount != count)
{
count = actualCount;
lightUp();
text.FontSize = count switch
{
> 0 and < 100 => 24,
< 1000 => 20,
< 10000 => 16,
_ => 12 // > 9999
};
}
count--;
updateText();
}

text.Text = count.ToString();
}
else
private void updateText()
{
if (count > 0)
{
text.Text = timing.Judgement switch
text.FontSize = count switch
{
Judgement.Flawless => "FL",
Judgement.Perfect => "PF",
Judgement.Great => "GR",
Judgement.Alright => "AL",
Judgement.Okay => "OK",
Judgement.Miss => "MS",
_ => "??"
> 0 and < 100 => 24,
< 1000 => 20,
< 10000 => 16,
_ => 12 // > 9999
};
text.Text = count.ToString();
}

base.Update();
else
text.Text = defaultText;
}

private void lightUp()
Expand Down

0 comments on commit 78f1778

Please sign in to comment.