Skip to content

Commit

Permalink
Refactor: 将杂率的计算移入 MusicGameModule(从 MusicGame3DUIPanel) (#241)
Browse files Browse the repository at this point in the history
* Refactor: 将杂率的计算移入 MusicGameModule(从 MusicGame3DUIPanel)

* feat: MusicGameModule.cs 添加 MaxCombo 变量,并优化性能

* Feat: 重命名 sum 变量,将 OutNum 从 RightNum 中独立出来
  • Loading branch information
Chen-Luan authored Nov 9, 2024
1 parent 5f15956 commit e504ae0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ private void InputDown(InputEventArgs args)
}
else
{
//ToDo: key预计在后续重新设计样式至半透明灰色
key = Instantiate(keyPrefab);
var trans = key.transform;
trans.position = new Vector3(Endpoint.Instance.GetPosWithRatio(args.RangeMin), -0.045f, 20);
Expand Down
21 changes: 19 additions & 2 deletions Cyan-Stars/Assets/Scripts/Gameplay/MusicGame/MusicGameModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CyanStars.Framework.Event;
using CyanStars.Framework.Timeline;
using CyanStars.Framework.Logging;
using UnityEngine;


namespace CyanStars.Gameplay.MusicGame
Expand Down Expand Up @@ -70,18 +71,25 @@ public class MusicGameModule : BaseDataModule

public DistanceBarData DistanceBarData { get; private set; }

/// <summary>
/// 用于计算杂率
/// </summary>
private float deviationsSum;

#region 玩家游戏过程中的实时数据

public int Combo = 0; //Combo数量
public int MaxCombo = 0; //玩家在此次游玩时的最大连击数量
public float Score = 0; //分数
public EvaluateType Grade = default; //评分
public float ImpurityRate = 0; //杂率
public float CurrentDeviation = 0; //当前精准度
public List<float> DeviationList = new List<float>(); //各个音符的偏移
public float MaxScore = 0; //理论最高分
public int ExactNum = 0;
public int GreatNum = 0;
public int RightNum = 0;
public int OutNum = 0;
public int BadNum = 0;
public int MissNum = 0;
public float FullScore = 0; //全谱总分
Expand Down Expand Up @@ -201,14 +209,17 @@ public void ResetPlayingData()
DistanceBarData = null;

Combo = 0; //Combo数量
MaxCombo = 0; //玩家在此次游玩时的最大连击数量
Score = 0; //分数
Grade = default; //评分
ImpurityRate = 0; //杂率
CurrentDeviation = 0; //当前精准度
DeviationList.Clear(); //各个音符的偏移
MaxScore = 0; //理论最高分
ExactNum = 0;
GreatNum = 0;
RightNum = 0;
OutNum = 0;
BadNum = 0;
MissNum = 0;
FullScore = 0; //全谱总分
Expand All @@ -226,6 +237,7 @@ public void RefreshPlayingData(int addCombo, float addScore, EvaluateType grade,
else
{
Combo += addCombo;
MaxCombo = Mathf.Max(Combo, MaxCombo);
Score += addScore;
}

Expand All @@ -236,17 +248,22 @@ public void RefreshPlayingData(int addCombo, float addScore, EvaluateType grade,
EvaluateType.Exact => ExactNum++,
EvaluateType.Great => GreatNum++,
EvaluateType.Right => RightNum++,
EvaluateType.Out => RightNum++,
EvaluateType.Out => OutNum++,
EvaluateType.Bad => BadNum++,
EvaluateType.Miss => MissNum++,
_ => throw new ArgumentException(nameof(grade))
};


// 仅部分音符计算偏移值/杂率,详见 NoteJudger.cs 代码
// currentDeviation 为玩家按下的时间相对于 Note 判定时间之差,单位 s
// 玩家提前按下为+,延后按下为-
if (currentDeviation < 10000)
{
CurrentDeviation = currentDeviation;
DeviationList.Add(currentDeviation);
deviationsSum += Mathf.Abs(currentDeviation);
ImpurityRate = deviationsSum / DeviationList.Count;
ImpurityRate = (float)Mathf.CeilToInt(ImpurityRate * 1000000) / 1000; // 将杂率转换为 00.000ms 格式并向上取整
}

GameRoot.Event.Dispatch(EventConst.MusicGameDataRefreshEvent, this,EmptyEventArgs.Create());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public void ReduceHeight(float deltaTime)
{
if (deltaTime <= 0)
{
Debug.Log("delta time should be great 0");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace CyanStars.Gameplay.MusicGame
public class MusicGame3DUIPanel : BaseUIPanel
{
public TextMeshProUGUI TxtGrade;
public TextMeshProUGUI TxtAccuracy;
public TextMeshProUGUI TxtImpurityRate;
public TextMeshProUGUI TxtScoreRatio;
public TextMeshProUGUI TxtVisibleScore;

Expand All @@ -35,11 +35,11 @@ public override void OnOpen()
Color color = TxtGrade.color;
color.a = 1;
TxtGrade.color = color;
TxtAccuracy.text = $"杂率:{0:F3}s";
TxtAccuracy.color = Color.yellow;
TxtScoreRatio.text = $"{100:F}%";
TxtImpurityRate.text = $"杂率:{0:F3}ms";
TxtImpurityRate.color = Color.yellow;
TxtScoreRatio.text = $"{100:F}%"; //TODO: 已弃用,待删除
TxtScoreRatio.color = Color.yellow;
TxtVisibleScore.text = "000000";;
TxtVisibleScore.text = "000000";

GameRoot.Event.AddListener(EventConst.MusicGameDataRefreshEvent, OnMusicGameDataRefresh);
}
Expand Down Expand Up @@ -93,30 +93,19 @@ private void OnMusicGameDataRefresh(object sender, EventArgs args)
StartCoroutine(FadeGradeTMP());

//刷新杂率
float accuracy = 0, sum = 0;
if (dataModule.DeviationList.Count > 0)
{
foreach (var item in dataModule.DeviationList)
{
sum += Mathf.Abs(item);
}

accuracy = sum / dataModule.DeviationList.Count;
}

TxtAccuracy.text = $"杂率:{accuracy:F3}s";
TxtImpurityRate.text = $"杂率:{dataModule.ImpurityRate:F3}ms";

if (accuracy < 0.03)
if (dataModule.ImpurityRate < 30)
{
TxtAccuracy.color = Color.yellow;
TxtImpurityRate.color = Color.yellow;
}
else if (accuracy < 0.05)
else if (dataModule.ImpurityRate < 50)
{
TxtAccuracy.color = Color.blue;
TxtImpurityRate.color = Color.blue;
}
else
{
TxtAccuracy.color = Color.white;
TxtImpurityRate.color = Color.white;
}

//刷新得分率
Expand All @@ -128,7 +117,7 @@ private void OnMusicGameDataRefresh(object sender, EventArgs args)

TxtScoreRatio.text = $"{(scoreRatio * 100):F}%";

if (dataModule.GreatNum + dataModule.RightNum + dataModule.BadNum +
if (dataModule.GreatNum + dataModule.RightNum + dataModule.OutNum + dataModule.BadNum +
dataModule.MissNum == 0)
{
TxtScoreRatio.color = Color.yellow;
Expand Down

0 comments on commit e504ae0

Please sign in to comment.