From f17f6393041a54e4b15418059f6a9ff9dc2a39cb Mon Sep 17 00:00:00 2001
From: EliteMasterEric <ericmyllyoja@gmail.com>
Date: Wed, 22 May 2024 01:07:20 -0400
Subject: [PATCH] Finish implementing smaller numbers.

---
 assets                                        |  2 +-
 source/funkin/InitState.hx                    |  4 +-
 source/funkin/play/ResultState.hx             | 75 ++++++++++++++++---
 .../play/components/ClearPercentCounter.hx    | 73 ++++++++++++++----
 4 files changed, 124 insertions(+), 30 deletions(-)

diff --git a/assets b/assets
index ce7dabffb..b6d930109 160000
--- a/assets
+++ b/assets
@@ -1 +1 @@
-Subproject commit ce7dabffbebc154c9dda1f01e92dbef83e3405ab
+Subproject commit b6d930109eb69cfd368145e893d81ac1e97ed004
diff --git a/source/funkin/InitState.hx b/source/funkin/InitState.hx
index 6a52eaf5d..d17554d11 100644
--- a/source/funkin/InitState.hx
+++ b/source/funkin/InitState.hx
@@ -227,14 +227,14 @@ class InitState extends FlxState
             tallies:
               {
                 sick: 130,
-                good: 69,
+                good: 70,
                 bad: 69,
                 shit: 69,
                 missed: 69,
                 combo: 69,
                 maxCombo: 69,
                 totalNotesHit: 140,
-                totalNotes: 2000,
+                totalNotes: 200 // 0,
               }
           },
       }));
diff --git a/source/funkin/play/ResultState.hx b/source/funkin/play/ResultState.hx
index d038b7785..fdcd0cc39 100644
--- a/source/funkin/play/ResultState.hx
+++ b/source/funkin/play/ResultState.hx
@@ -37,6 +37,7 @@ class ResultState extends MusicBeatSubState
   final rank:ResultRank;
   final songName:FlxBitmapText;
   final difficulty:FlxSprite;
+  final clearPercentSmall:ClearPercentCounter;
 
   final maskShaderSongName:LeftMaskShader = new LeftMaskShader();
   final maskShaderDifficulty:LeftMaskShader = new LeftMaskShader();
@@ -78,6 +79,10 @@ class ResultState extends MusicBeatSubState
     difficulty = new FlxSprite(555);
     difficulty.zIndex = 1000;
 
+    clearPercentSmall = new ClearPercentCounter(FlxG.width / 2 + 300, FlxG.height / 2 - 100, 100, true);
+    clearPercentSmall.zIndex = 1000;
+    clearPercentSmall.visible = false;
+
     bgFlash = FlxGradient.createGradientFlxSprite(FlxG.width, FlxG.height, [0xFFFFEB69, 0xFFFFE66A], 90);
 
     resultsAnim = FunkinSprite.createSparrow(-200, -10, "resultScreen/results");
@@ -194,7 +199,7 @@ class ResultState extends MusicBeatSubState
     speedOfTween.x = -1.0 * Math.cos(angleRad);
     speedOfTween.y = -1.0 * Math.sin(angleRad);
 
-    timerThenSongName(1.0);
+    timerThenSongName(1.0, false);
 
     songName.shader = maskShaderSongName;
     difficulty.shader = maskShaderDifficulty;
@@ -319,13 +324,15 @@ class ResultState extends MusicBeatSubState
 
   function startRankTallySequence():Void
   {
-    clearPercentTarget = Math.floor((params.scoreData.tallies.sick + params.scoreData.tallies.good) / params.scoreData.tallies.totalNotes * 100);
-    clearPercentTarget = 100;
+    var clearPercentFloat = (params.scoreData.tallies.sick + params.scoreData.tallies.good) / params.scoreData.tallies.totalNotes * 100;
+    clearPercentTarget = Math.floor(clearPercentFloat);
+    // Prevent off-by-one errors.
 
     clearPercentLerp = Std.int(Math.max(0, clearPercentTarget - 36));
 
-    var clearPercentCounter:ClearPercentCounter = new ClearPercentCounter(FlxG.width / 2 + 300, FlxG.height / 2 - 100, clearPercentTarget);
-    clearPercentCounter.curNumber = clearPercentLerp;
+    trace('Clear percent target: ' + clearPercentFloat + ', round: ' + clearPercentTarget);
+
+    var clearPercentCounter:ClearPercentCounter = new ClearPercentCounter(FlxG.width / 2 + 300, FlxG.height / 2 - 100, clearPercentLerp);
     FlxTween.tween(clearPercentCounter, {curNumber: clearPercentTarget}, 1.5,
       {
         ease: FlxEase.quartOut,
@@ -345,10 +352,25 @@ class ResultState extends MusicBeatSubState
           bgFlash.visible = true;
           FlxTween.tween(bgFlash, {alpha: 0}, 0.4);
 
+          // Just to be sure that the lerp didn't mess things up.
+          clearPercentCounter.curNumber = clearPercentTarget;
+
+          clearPercentCounter.flash(true);
+          new FlxTimer().start(0.4, _ -> {
+            clearPercentCounter.flash(false);
+          });
+
           displayRankText();
 
           new FlxTimer().start(2.0, _ -> {
-            // remove(clearPercentCounter);
+            FlxTween.tween(clearPercentCounter, {alpha: 0}, 0.5,
+              {
+                startDelay: 0.5,
+                ease: FlxEase.quartOut,
+                onComplete: _ -> {
+                  remove(clearPercentCounter);
+                }
+              });
 
             afterRankTallySequence();
           });
@@ -406,6 +428,8 @@ class ResultState extends MusicBeatSubState
 
   function afterRankTallySequence():Void
   {
+    showSmallClearPercent();
+
     FunkinSound.playMusic(rank.getMusicPath(),
       {
         startingVolume: 1.0,
@@ -490,7 +514,7 @@ class ResultState extends MusicBeatSubState
     }
   }
 
-  function timerThenSongName(timerLength:Float = 3.0):Void
+  function timerThenSongName(timerLength:Float = 3.0, autoScroll:Bool = true):Void
   {
     movingSongStuff = false;
 
@@ -501,10 +525,17 @@ class ResultState extends MusicBeatSubState
     difficulty.y = -difficulty.height;
     FlxTween.tween(difficulty, {y: diffYTween}, 0.5, {ease: FlxEase.expoOut, startDelay: 0.8});
 
+    if (clearPercentSmall != null)
+    {
+      clearPercentSmall.x = (difficulty.x + difficulty.width) + 60;
+      clearPercentSmall.y = -clearPercentSmall.height;
+      FlxTween.tween(clearPercentSmall, {y: 122 - 5}, 0.5, {ease: FlxEase.expoOut, startDelay: 0.8});
+    }
+
     songName.y = -songName.height;
     var fuckedupnumber = (10) * (songName.text.length / 15);
-    FlxTween.tween(songName, {y: diffYTween - 35 - fuckedupnumber}, 0.5, {ease: FlxEase.expoOut, startDelay: 0.9});
-    songName.x = (difficulty.x + difficulty.width) + 20;
+    FlxTween.tween(songName, {y: diffYTween - 25 - fuckedupnumber}, 0.5, {ease: FlxEase.expoOut, startDelay: 0.9});
+    songName.x = clearPercentSmall.x + clearPercentSmall.width - 30;
 
     new FlxTimer().start(timerLength, _ -> {
       var tempSpeed = FlxPoint.get(speedOfTween.x, speedOfTween.y);
@@ -512,10 +543,29 @@ class ResultState extends MusicBeatSubState
       speedOfTween.set(0, 0);
       FlxTween.tween(speedOfTween, {x: tempSpeed.x, y: tempSpeed.y}, 0.7, {ease: FlxEase.quadIn});
 
-      movingSongStuff = true;
+      movingSongStuff = (autoScroll);
     });
   }
 
+  function showSmallClearPercent():Void
+  {
+    if (clearPercentSmall != null)
+    {
+      add(clearPercentSmall);
+      clearPercentSmall.visible = true;
+      clearPercentSmall.flash(true);
+      new FlxTimer().start(0.4, _ -> {
+        clearPercentSmall.flash(false);
+      });
+
+      clearPercentSmall.curNumber = clearPercentTarget;
+      clearPercentSmall.zIndex = 1000;
+      refresh();
+    }
+
+    movingSongStuff = true;
+  }
+
   var movingSongStuff:Bool = false;
   var speedOfTween:FlxPoint = FlxPoint.get(-1, 1);
 
@@ -523,7 +573,8 @@ class ResultState extends MusicBeatSubState
   {
     super.draw();
 
-    songName.clipRect = FlxRect.get(Math.max(0, 540 - songName.x), 0, FlxG.width, songName.height);
+    songName.clipRect = FlxRect.get(Math.max(0, 520 - songName.x), 0, FlxG.width, songName.height);
+
     // PROBABLY SHOULD FIX MEMORY FREE OR WHATEVER THE PUT() FUNCTION DOES !!!! FEELS LIKE IT STUTTERS!!!
 
     // if (songName != null && songName.frame != null)
@@ -539,8 +590,10 @@ class ResultState extends MusicBeatSubState
     {
       songName.x += speedOfTween.x;
       difficulty.x += speedOfTween.x;
+      clearPercentSmall.x += speedOfTween.x;
       songName.y += speedOfTween.y;
       difficulty.y += speedOfTween.y;
+      clearPercentSmall.y += speedOfTween.y;
 
       if (songName.x + songName.width < 100)
       {
diff --git a/source/funkin/play/components/ClearPercentCounter.hx b/source/funkin/play/components/ClearPercentCounter.hx
index 4c03ec3a9..d296b0b0b 100644
--- a/source/funkin/play/components/ClearPercentCounter.hx
+++ b/source/funkin/play/components/ClearPercentCounter.hx
@@ -1,6 +1,7 @@
 package funkin.play.components;
 
 import funkin.graphics.FunkinSprite;
+import funkin.graphics.shaders.PureColor;
 import flixel.FlxSprite;
 import flixel.group.FlxGroup.FlxTypedGroup;
 import flixel.group.FlxSpriteGroup.FlxTypedSpriteGroup;
@@ -9,25 +10,54 @@ import flixel.tweens.FlxEase;
 import flixel.tweens.FlxTween;
 import flixel.text.FlxText.FlxTextAlign;
 import funkin.util.MathUtil;
+import flixel.util.FlxColor;
 
 /**
  * Numerical counters used to display the clear percent.
  */
 class ClearPercentCounter extends FlxTypedSpriteGroup<FlxSprite>
 {
-  public var curNumber:Int = 0;
-  public var neededNumber:Int = 0;
+  public var curNumber(default, set):Int = 0;
 
-  public function new(x:Float, y:Float, neededNumber:Int = 0)
+  var numberChanged:Bool = false;
+
+  function set_curNumber(val:Int):Int
+  {
+    numberChanged = true;
+    return curNumber = val;
+  }
+
+  var small:Bool = false;
+  var flashShader:PureColor;
+
+  public function new(x:Float, y:Float, startingNumber:Int = 0, small:Bool = false)
   {
     super(x, y);
 
-    this.neededNumber = neededNumber;
+    flashShader = new PureColor(FlxColor.WHITE);
+    flashShader.colorSet = true;
 
-    var clearPercentText:FunkinSprite = FunkinSprite.create(0, 0, 'resultScreen/clearPercent/clearPercentText');
+    curNumber = startingNumber;
+
+    this.small = small;
+
+    var clearPercentText:FunkinSprite = FunkinSprite.create(0, 0, 'resultScreen/clearPercent/clearPercentText${small ? 'Small' : ''}');
+    clearPercentText.x = small ? 40 : 0;
     add(clearPercentText);
 
-    if (curNumber == neededNumber) drawNumbers();
+    drawNumbers();
+  }
+
+  /**
+   * Make the counter flash turn white or stop being all white.
+   * @param enabled Whether the counter should be white.
+   */
+  public function flash(enabled:Bool):Void
+  {
+    for (member in members)
+    {
+      member.shader = enabled ? flashShader : null;
+    }
   }
 
   var tmr:Float = 0;
@@ -36,7 +66,7 @@ class ClearPercentCounter extends FlxTypedSpriteGroup<FlxSprite>
   {
     super.update(elapsed);
 
-    if (curNumber < neededNumber) drawNumbers();
+    if (numberChanged) drawNumbers();
   }
 
   function drawNumbers()
@@ -44,8 +74,6 @@ class ClearPercentCounter extends FlxTypedSpriteGroup<FlxSprite>
     var seperatedScore:Array<Int> = [];
     var tempCombo:Int = Math.round(curNumber);
 
-    var fullNumberDigits:Int = Std.int(Math.max(1, Math.ceil(MathUtil.logBase(10, neededNumber))));
-
     while (tempCombo != 0)
     {
       seperatedScore.push(tempCombo % 10);
@@ -59,19 +87,32 @@ class ClearPercentCounter extends FlxTypedSpriteGroup<FlxSprite>
     for (ind => num in seperatedScore)
     {
       var digitIndex = ind + 1;
+      // If there's only one digit, move it to the right
+      // If there's three digits, move them all to the left
+      var digitOffset = (seperatedScore.length == 1) ? 1 : (seperatedScore.length == 3) ? -1 : 0;
+      var digitSize = small ? 32 : 72;
+      var digitHeightOffset = small ? -4 : 0;
+
+      var xPos = (digitIndex - 1 + digitOffset) * (digitSize * this.scale.x);
+      xPos += small ? -24 : 0;
+      var yPos = (digitIndex - 1 + digitOffset) * (digitHeightOffset * this.scale.y);
+      yPos += small ? 0 : 72;
+
       if (digitIndex >= members.length)
       {
-        var xPos = (digitIndex - 1) * (72 * this.scale.x);
-        var yPos = 72;
-        // Three digits = LRL so two different numbers aren't adjacent to each other.
-        var variant:Bool = (fullNumberDigits % 2 != 0) ? (digitIndex % 2 == 0) : (digitIndex % 2 == 1);
-        var numb:ClearPercentNumber = new ClearPercentNumber(xPos, yPos, num);
+        // Three digits = LLR because the 1 and 0 won't be the same anyway.
+        var variant:Bool = (seperatedScore.length == 3) ? (digitIndex >= 2) : (digitIndex >= 1);
+        // var variant:Bool = (seperatedScore.length % 2 != 0) ? (digitIndex % 2 == 0) : (digitIndex % 2 == 1);
+        var numb:ClearPercentNumber = new ClearPercentNumber(xPos, yPos, num, variant, this.small);
         numb.scale.set(this.scale.x, this.scale.y);
         add(numb);
       }
       else
       {
         members[digitIndex].animation.play(Std.string(num));
+        // Reset the position of the number
+        members[digitIndex].x = xPos + this.x;
+        members[digitIndex].y = yPos + this.y;
       }
     }
   }
@@ -79,11 +120,11 @@ class ClearPercentCounter extends FlxTypedSpriteGroup<FlxSprite>
 
 class ClearPercentNumber extends FlxSprite
 {
-  public function new(x:Float, y:Float, digit:Int, variant:Bool = false)
+  public function new(x:Float, y:Float, digit:Int, variant:Bool, small:Bool)
   {
     super(x, y);
 
-    frames = Paths.getSparrowAtlas('resultScreen/clearPercent/clearPercentNumber${variant ? 'Right' : 'Left'}');
+    frames = Paths.getSparrowAtlas('resultScreen/clearPercent/clearPercentNumber${small ? 'Small' : variant ? 'Right' : 'Left'}');
 
     for (i in 0...10)
     {