Skip to content

Commit a37e2e7

Browse files
committed
[O] UnstableRate 2P support
1 parent c3303c9 commit a37e2e7

File tree

1 file changed

+53
-20
lines changed

1 file changed

+53
-20
lines changed

AquaMai.Mods/Utils/UnstableRate.cs

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace AquaMai.Mods.Utils;
1515
public class UnstableRate
1616
{
1717
// The playfield goes from bottom left (-1080, -960) to top right (0, 120)
18-
private const float BaselineHeight = -480;
19-
private const float BaselineCenter = -540;
18+
// 由于使用了 local space,所以高度和中心都是 0
19+
private const float BaselineHeight = 0;
20+
private const float BaselineCenter = 0;
2021
private const float BaselineHScale = 25;
2122
private const float CenterMarkerHeight = 20;
2223

@@ -29,6 +30,24 @@ public class UnstableRate
2930

3031
private const float TimingBin = 16.666666f;
3132

33+
[ConfigEntry("默认显示")]
34+
public static bool defaultOn = false;
35+
36+
// 0: 不显示,1: 显示,剩下来留给以后
37+
public static int[] displayType = [1, 1];
38+
39+
public static void OnBeforePatch()
40+
{
41+
if (defaultOn)
42+
{
43+
displayType = [1, 1];
44+
}
45+
else
46+
{
47+
displayType = [0, 0];
48+
}
49+
}
50+
3251
private struct Timing
3352
{
3453
// Timings are in multiple of TimingBin (16.666666ms)
@@ -44,30 +63,38 @@ private struct Timing
4463
new() { windowStart = 3, windowEnd = 6, color = new Color(0.102f, 0.731f, 0.078f) }, // Great
4564
new() { windowStart = 6, windowEnd = 9, color = new Color(0.925f, 0.730f, 0.110f) }, // Good
4665
];
47-
private static readonly Timing Miss = new() {windowStart = 999, windowEnd = 999, color = Color.grey };
66+
private static readonly Timing Miss = new() { windowStart = 999, windowEnd = 999, color = Color.grey };
4867
private static readonly Material LineMaterial = new(Shader.Find("Sprites/Default"));
4968

50-
private static GameMonitor _monitor;
69+
private static GameObject[] baseObjects = new GameObject[2];
5170

5271
[HarmonyPostfix]
5372
[HarmonyPatch(typeof(GameProcess), "OnStart")]
54-
public static void OnGameProcessStart(GameProcess __instance)
73+
public static void OnGameProcessStart(GameProcess __instance, GameMonitor[] ____monitors)
5574
{
56-
_monitor = Traverse.Create(__instance).Field("_monitors").GetValue<GameMonitor[]>()[0];
57-
5875
// Set up the baseline (the static part of the display)
59-
SetupBaseline();
76+
for (int i = 0; i < 2; i++)
77+
{
78+
if (displayType[i] == 0) continue;
79+
var main = ____monitors[i].gameObject.transform.Find("Canvas/Main");
80+
var go = new GameObject("[AquaMai] UnstableRate");
81+
go.transform.SetParent(main, false);
82+
baseObjects[i] = go;
83+
SetupBaseline(go);
84+
}
6085
}
6186

6287
[HarmonyPostfix]
6388
[HarmonyPatch(typeof(NoteBase), "Judge")]
64-
public static void OnJudge(NoteBase __instance)
89+
public static void OnJudge(NoteBase __instance, float ___JudgeTimingDiffMsec)
6590
{
91+
if (displayType[__instance.MonitorId] == 0) return;
92+
6693
// How many milliseconds early or late the player hit
67-
var msec = Traverse.Create(__instance).Field("JudgeTimingDiffMsec").GetValue<float>();
94+
var msec = ___JudgeTimingDiffMsec;
6895

6996
// Account for the offset
70-
var optionJudgeTiming = Singleton<GamePlayManager>.Instance.GetGameScore(0).UserOption.GetJudgeTimingFrame();
97+
var optionJudgeTiming = Singleton<GamePlayManager>.Instance.GetGameScore(__instance.MonitorId).UserOption.GetJudgeTimingFrame();
7198
msec -= optionJudgeTiming * TimingBin;
7299

73100
// Don't process misses
@@ -77,8 +104,13 @@ public static void OnJudge(NoteBase __instance)
77104
return;
78105
}
79106

107+
var go = baseObjects[__instance.MonitorId];
108+
if (go == null)
109+
{
110+
return;
111+
}
80112
// Create judgement tick
81-
var line = CreateLine();
113+
var line = CreateLine(go);
82114

83115
line.SetPosition(0, new Vector3(BaselineCenter + BaselineHScale * (msec / TimingBin), BaselineHeight + JudgeHeight, 0));
84116
line.SetPosition(1, new Vector3(BaselineCenter + BaselineHScale * (msec / TimingBin), BaselineHeight - JudgeHeight, 0));
@@ -96,13 +128,13 @@ public static void OnJudge(NoteBase __instance)
96128

97129
[HarmonyPostfix]
98130
[HarmonyPatch(typeof(HoldNote), "JudgeHoldHead")]
99-
public static void OnJudgeHold(HoldNote __instance)
131+
public static void OnJudgeHold(HoldNote __instance, float ___JudgeTimingDiffMsec)
100132
{
101133
// The calculations are the same for the hold note heads
102-
OnJudge(__instance);
134+
OnJudge(__instance, ___JudgeTimingDiffMsec);
103135
}
104136

105-
private static void SetupBaseline()
137+
private static void SetupBaseline(GameObject go)
106138
{
107139
LineRenderer line;
108140

@@ -112,7 +144,7 @@ private static void SetupBaseline()
112144
// Draw each timing window in a different color
113145
foreach (var timing in Timings)
114146
{
115-
line = CreateLine();
147+
line = CreateLine(go, flatCaps: true);
116148

117149
line.SetPosition(0, new Vector3(BaselineCenter + sign * BaselineHScale * timing.windowStart, BaselineHeight, 0));
118150
line.SetPosition(1, new Vector3(BaselineCenter + sign * BaselineHScale * timing.windowEnd, BaselineHeight, 0));
@@ -123,7 +155,7 @@ private static void SetupBaseline()
123155
}
124156

125157
// Center marker
126-
line = CreateLine();
158+
line = CreateLine(go);
127159

128160
// Setting z-coordinate to -1 to make sure it stays in the foreground
129161
line.SetPosition(0, new Vector3(BaselineCenter, BaselineHeight + CenterMarkerHeight, -1));
@@ -133,19 +165,20 @@ private static void SetupBaseline()
133165
line.endColor = Color.white;
134166
}
135167

136-
private static LineRenderer CreateLine()
168+
private static LineRenderer CreateLine(GameObject go, bool flatCaps = false)
137169
{
138170
var obj = new GameObject();
139-
obj.transform.SetParent(_monitor.transform);
171+
obj.transform.SetParent(go.transform, false);
140172

141173
// We can't add the line directly as a component of the monitor, because it can only
142174
// have one LineRenderer component at a time.
143175
var line = obj.AddComponent<LineRenderer>();
144176
line.material = LineMaterial;
177+
line.useWorldSpace = false;
145178
line.startWidth = LineThickness;
146179
line.endWidth = LineThickness;
147180
line.positionCount = 2;
148-
line.numCapVertices = 6; // Make the ends round
181+
line.numCapVertices = flatCaps ? 0 : 6;
149182

150183
return line;
151184
}

0 commit comments

Comments
 (0)