Skip to content

Commit

Permalink
Refactored performance text sizing code (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
grishhung authored May 9, 2023
1 parent 247bb08 commit 0b355d5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 42 deletions.
3 changes: 2 additions & 1 deletion Assets/Scenes/PlayScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ MonoBehaviour:
- {fileID: 1536164096}
- {fileID: 1294580434}
- {fileID: 2089210895}
settingsContainer: {fileID: 0}
--- !u!114 &8545367
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -4029,6 +4028,8 @@ MonoBehaviour:
trackCamera: {fileID: 1489966287}
silentMixerGroup: {fileID: -541741410412472071, guid: d5f8521c55f50a44eb1e292204769821,
type: 2}
fontSize: 24
animTimeLength: 1
--- !u!1 &1684901692
GameObject:
m_ObjectHideFlags: 0
Expand Down
47 changes: 6 additions & 41 deletions Assets/Script/PlayMode/MicPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ private string EndPhraseName {

private string lastSecondHarmonyLyric = "";

public PerformanceTextSizer perfTextSizer;
public float fontSize;
public float animTimeLength;
private float animTimeRemaining;

private void Start() {
Instance = this;
Expand Down Expand Up @@ -285,9 +285,7 @@ private void Start() {
starsKeeper = new(scoreKeeper, micInputs[0].player.chosenInstrument, phrases, ptsPerPhrase);

// Prepare performance text characteristics
fontSize = 24.0f;
animTimeLength = 1.0f;
animTimeRemaining = 0.0f;
perfTextSizer = new PerformanceTextSizer(fontSize, animTimeLength);
preformaceText.color = Color.white;
}

Expand Down Expand Up @@ -619,9 +617,9 @@ private void Update() {
}
}

// Animate performance text over one second
animTimeRemaining -= Time.deltaTime;
preformaceText.fontSize = PerformanceTextFontSize(fontSize, animTimeLength, animTimeRemaining);
// Animate and get performance text size given the current timestamp
perfTextSizer.animTimeRemaining -= Time.deltaTime;
preformaceText.fontSize = perfTextSizer.PerformanceTextFontSize();

// Update combo text
if (Multiplier == 1) {
Expand Down Expand Up @@ -770,7 +768,7 @@ private void UpdateEndPhrase() {
};

// Begin animation and start countdown
animTimeRemaining = animTimeLength;
perfTextSizer.animTimeRemaining = animTimeLength;

// Add to sing percent
totalSingPercent += Mathf.Min(bestPercent, 1f);
Expand Down Expand Up @@ -952,38 +950,5 @@ private void StarpowerAction(InputStrategy inputStrategy) {
starpowerActive = true;
}
}

private float PerformanceTextFontSize(float fontSize, float animTimeLength,
float animTimeRemaining, bool representsPeakSize = false) {
// Define the relative size before font sizing is applied
float relativeSize = 0.0f;
float halfwayPoint = animTimeLength / 2.0f;

// Determine the text size relative to its countdown timestamp
if ((animTimeRemaining > animTimeLength) || (animTimeRemaining < 0.0f)) {
relativeSize = 0.0f;
} else if (animTimeRemaining >= halfwayPoint) {
if (animTimeRemaining > (animTimeLength - 0.16666f)) {
relativeSize = (-1290.0f * Mathf.Pow(
animTimeRemaining - animTimeLength + 0.16666f, 4.0f)) + 0.9984f;
} else {
float denominator = 1.0f + Mathf.Pow((float) Math.E,
-50.0f * (animTimeRemaining - animTimeLength + 0.25f));
relativeSize = (0.1f / denominator) + 0.9f;
}
} else if (animTimeRemaining < halfwayPoint) {
if (animTimeRemaining < 0.16666f) {
relativeSize = (-1290.0f * Mathf.Pow(
animTimeRemaining - 0.16666f, 4.0f)) + 0.9984f;
} else {
float denominator = 1.0f + Mathf.Pow((float) Math.E,
-50.0f * (animTimeRemaining - 0.25f));
relativeSize = (-0.1f / denominator) + 1.0f;
}
}

// Consider if fontSize represents the "peak" or the "rest" size
return relativeSize * fontSize * (representsPeakSize ? 1.0f : 1.11111f);
}
}
}
66 changes: 66 additions & 0 deletions Assets/Script/PlayMode/PerformanceTextSizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace YARG.PlayMode {
public class PerformanceTextSizer {
// Used to set the keyframes of the animation
public float fontSize { get; set; }
public float animTimeLength { get; set; }
public float animTimeRemaining { get; set; }

// Set whether fontSize represents the "peak" or the "rest" size
// Note that rest size == peak size * 0.9f for animation purposes
private bool representsPeakSize { get; set; }

public PerformanceTextSizer(float fs, float atl, bool rps = false) {
fontSize = fs;
animTimeLength = atl;
animTimeRemaining = 0.0f;
representsPeakSize = rps;
}

/// <returns>
/// The font size of the text given the current animation timestamp
/// </returns>
/// <summary>
/// The first 1/6s = the rise to the peak size
/// The second 1/6s = the fall to the rest size
/// The second to last 1/6s = the rise back to peak size
/// The last 1/6s = the fall back to nothingness
/// Returned size will be 0f if animTimeRemaining <= 0
/// </summary>
public float PerformanceTextFontSize() {
// Define the relative size before font sizing is applied
float relativeSize = 0.0f;
float halfwayPoint = animTimeLength / 2.0f;

// Determine the text size relative to its countdown timestamp
if ((animTimeRemaining > animTimeLength) || (animTimeRemaining < 0.0f)) {
relativeSize = 0.0f;
} else if (animTimeRemaining >= halfwayPoint) {
if (animTimeRemaining > (animTimeLength - 0.16666f)) {
relativeSize = (-1290.0f * Mathf.Pow(
animTimeRemaining - animTimeLength + 0.16666f, 4.0f)) + 0.9984f;
} else {
float denominator = 1.0f + Mathf.Pow((float) Math.E,
-50.0f * (animTimeRemaining - animTimeLength + 0.25f));
relativeSize = (0.1f / denominator) + 0.9f;
}
} else if (animTimeRemaining < halfwayPoint) {
if (animTimeRemaining < 0.16666f) {
relativeSize = (-1290.0f * Mathf.Pow(
animTimeRemaining - 0.16666f, 4.0f)) + 0.9984f;
} else {
float denominator = 1.0f + Mathf.Pow((float) Math.E,
-50.0f * (animTimeRemaining - 0.25f));
relativeSize = (-0.1f / denominator) + 1.0f;
}
}

// Size appropriately
return relativeSize * fontSize * (representsPeakSize ? 1.0f : 1.11111f);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Script/PlayMode/PerformanceTextSizer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b355d5

Please sign in to comment.