Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit 5e4c5cf

Browse files
committed
Leaderboard now gets disabled when speed is changed.
1 parent dd72a62 commit 5e4c5cf

4 files changed

Lines changed: 155 additions & 48 deletions

File tree

PracticePlugin/Plugin.cs

Lines changed: 98 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ namespace PracticePlugin
1111
{
1212
public class Plugin : IPlugin
1313
{
14+
public string Name
15+
{
16+
get { return "Practice Plugin"; }
17+
}
18+
19+
public string Version
20+
{
21+
get { return "v2.1"; }
22+
}
23+
1424
public const float MaxSize = 5f;
1525
public const float StepSize = 0.05f;
16-
26+
1727
public static GameObject SettingsObject;
1828

1929
public static float TimeScale
@@ -22,38 +32,44 @@ public static float TimeScale
2232
set
2333
{
2434
_timeScale = value;
25-
if (_songAudio != null)
35+
if (!IsEqualToOne(_timeScale))
2636
{
27-
_songAudio.pitch = _timeScale;
37+
HasTimeScaleChanged = true;
38+
39+
if (_audioTimeSync != null)
40+
{
41+
_audioTimeSync.forcedAudioSync = true;
42+
}
43+
}
44+
else
45+
{
46+
if (_audioTimeSync != null)
47+
{
48+
_audioTimeSync.forcedAudioSync = false;
49+
}
2850
}
2951

30-
if (_noteCutAudioSource != null)
52+
if (_songAudio != null)
3153
{
32-
_noteCutAudioSource.pitch = _timeScale;
54+
_songAudio.pitch = _timeScale;
3355
}
3456
}
3557
}
58+
3659
private static float _timeScale = 1;
3760

3861
public static bool NoFail { get; private set; }
39-
40-
private bool _init;
41-
private MainGameSceneSetupData _mainGameSceneSetupData;
42-
private AudioTimeSyncController _audioTimeSync;
62+
63+
public static bool HasTimeScaleChanged { get; private set; }
64+
65+
private static bool _init;
66+
private static MainGameSceneSetupData _mainGameSceneSetupData;
67+
private static AudioTimeSyncController _audioTimeSync;
4368
private static AudioSource _songAudio;
44-
private static AudioSource _noteCutAudioSource;
45-
private string _lastLevelId;
46-
47-
public string Name
48-
{
49-
get { return "Practice Plugin"; }
50-
}
69+
private static string _lastLevelId;
70+
private static SpeedSettingsCreator _speedSettingsCreator;
71+
private static bool _resetNoFail;
5172

52-
public string Version
53-
{
54-
get { return "v2.0"; }
55-
}
56-
5773
public void OnApplicationStart()
5874
{
5975
if (_init) return;
@@ -70,6 +86,13 @@ private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene scene)
7086
{
7187
if (scene.buildIndex == 1)
7288
{
89+
if (_resetNoFail)
90+
{
91+
var resultsViewController =
92+
Resources.FindObjectsOfTypeAll<ResultsViewController>().FirstOrDefault();
93+
resultsViewController.continueButtonPressedEvent += ResultsViewControllerOnContinueButtonPressedEvent;
94+
}
95+
7396
if (SettingsObject == null)
7497
{
7598
var volumeSettings = Resources.FindObjectsOfTypeAll<VolumeSettingsController>().FirstOrDefault();
@@ -78,7 +101,8 @@ private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene scene)
78101
SettingsObject.SetActive(false);
79102
volumeSettings.gameObject.SetActive(true);
80103
var volume = SettingsObject.GetComponent<VolumeSettingsController>();
81-
ReflectionUtil.CopyComponent(volume, typeof(IncDecSettingsController), typeof(SpeedSettingsController), SettingsObject);
104+
ReflectionUtil.CopyComponent(volume, typeof(IncDecSettingsController),
105+
typeof(SpeedSettingsController), SettingsObject);
82106
Object.DestroyImmediate(volume);
83107
SettingsObject.GetComponentInChildren<TMP_Text>().text = "SPEED";
84108
Object.DontDestroyOnLoad(SettingsObject);
@@ -89,63 +113,99 @@ private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene scene)
89113
if (_mainGameSceneSetupData == null)
90114
{
91115
_mainGameSceneSetupData = Resources.FindObjectsOfTypeAll<MainGameSceneSetupData>().FirstOrDefault();
116+
if (_mainGameSceneSetupData == null) return;
117+
_mainGameSceneSetupData.didFinishEvent += MainGameSceneSetupDataOnDidFinishEvent;
92118
}
93-
94-
if (_mainGameSceneSetupData == null || scene.buildIndex != 5)
119+
120+
if (scene.buildIndex != 5)
95121
{
96122
return;
97123
}
98124

99-
if (_lastLevelId != _mainGameSceneSetupData.difficultyLevel.level.levelID && !string.IsNullOrEmpty(_lastLevelId))
125+
if (_lastLevelId != _mainGameSceneSetupData.difficultyLevel.level.levelID &&
126+
!string.IsNullOrEmpty(_lastLevelId))
100127
{
128+
HasTimeScaleChanged = false;
101129
TimeScale = 1;
102130
_lastLevelId = _mainGameSceneSetupData.difficultyLevel.level.levelID;
103131
}
104132

133+
if (IsEqualToOne(TimeScale))
134+
{
135+
HasTimeScaleChanged = false;
136+
}
137+
105138
_lastLevelId = _mainGameSceneSetupData.difficultyLevel.level.levelID;
106-
139+
107140
_audioTimeSync = Resources.FindObjectsOfTypeAll<AudioTimeSyncController>().FirstOrDefault();
108-
_audioTimeSync.forcedAudioSync = true;
109-
_songAudio = ReflectionUtil.GetPrivateField<AudioSource>(_audioTimeSync, "_audioSource");
141+
_songAudio = _audioTimeSync.GetPrivateField<AudioSource>("_audioSource");
110142
NoFail = !_mainGameSceneSetupData.gameplayOptions.validForScoreUse;
111-
143+
112144
if (!NoFail)
113145
{
114146
TimeScale = Mathf.Clamp(TimeScale, 1, MaxSize);
115147
}
116-
148+
117149
NoteHitPitchChanger.ReplacePrefab();
118150

119-
var canvas = Resources.FindObjectsOfTypeAll<HorizontalLayoutGroup>().FirstOrDefault(x => x.name == "Buttons")
151+
var canvas = Resources.FindObjectsOfTypeAll<HorizontalLayoutGroup>()
152+
.FirstOrDefault(x => x.name == "Buttons")
120153
.transform.parent;
121-
canvas.gameObject.AddComponent<SpeedSettingsCreator>();
154+
_speedSettingsCreator = canvas.gameObject.AddComponent<SpeedSettingsCreator>();
155+
_speedSettingsCreator.ValueChangedEvent += SpeedSettingsCreatorOnValueChangedEvent;
122156
TimeScale = TimeScale;
123157
}
124158
}
125159

126-
private void ScoreControllerOnNoteWasCutEvent(NoteData arg1, NoteCutInfo arg2, int arg3)
160+
private void ResultsViewControllerOnContinueButtonPressedEvent(ResultsViewController obj)
127161
{
128-
throw new NotImplementedException();
162+
PersistentSingleton<GameDataModel>.instance.gameDynamicData.GetCurrentPlayerDynamicData()
163+
.gameplayOptions.noEnergy = false;
164+
}
165+
166+
private void MainGameSceneSetupDataOnDidFinishEvent(MainGameSceneSetupData arg1, LevelCompletionResults results)
167+
{
168+
if (!NoFail && HasTimeScaleChanged && results != null &&
169+
results.levelEndStateType == LevelCompletionResults.LevelEndStateType.Cleared)
170+
{
171+
arg1.gameplayOptions.noEnergy = true;
172+
_resetNoFail = true;
173+
}
174+
}
175+
176+
private void SpeedSettingsCreatorOnValueChangedEvent(float timeScale)
177+
{
178+
if (!IsEqualToOne(timeScale))
179+
{
180+
HasTimeScaleChanged = true;
181+
}
182+
183+
TimeScale = timeScale;
184+
}
185+
186+
private static bool IsEqualToOne(float value)
187+
{
188+
return Math.Abs(value - 1) < 0.000000001f;
129189
}
130190

131191
public void OnLevelWasLoaded(int level)
132192
{
133-
193+
134194
}
135195

136196
public void OnLevelWasInitialized(int level)
137197
{
138-
198+
139199
}
140200

141201
public void OnUpdate()
142202
{
143-
203+
144204
}
145205

146206
public void OnFixedUpdate()
147207
{
148-
208+
149209
}
150210
}
151211
}

PracticePlugin/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("2.0")]
35-
[assembly: AssemblyFileVersion("2.0")]
34+
[assembly: AssemblyVersion("2.1")]
35+
[assembly: AssemblyFileVersion("2.1")]

PracticePlugin/SpeedSettingsController.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
using UnityEngine;
1+
using System;
2+
using UnityEngine;
23

34
namespace PracticePlugin
45
{
56
public class SpeedSettingsController : ListSettingsController
67
{
7-
8-
private int _indexOffset;
8+
public event Action<float> ValueChangedEvent;
99

10+
private int _indexOffset;
11+
1012
protected override void GetInitValues(out int idx, out int numberOfElements)
1113
{
1214
_indexOffset = Plugin.NoFail ? 1 : 20;
@@ -20,7 +22,10 @@ protected override void ApplyValue(int idx)
2022

2123
protected override string TextForValue(int idx)
2224
{
23-
Plugin.TimeScale = Plugin.StepSize * (idx + _indexOffset);
25+
if (ValueChangedEvent != null)
26+
{
27+
ValueChangedEvent(Plugin.StepSize * (idx + _indexOffset));
28+
}
2429
return Plugin.StepSize * 100f * (idx + _indexOffset) + "%";
2530
}
2631
}
Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,68 @@
1-
using UnityEngine;
1+
using System;
2+
using TMPro;
3+
using UnityEngine;
24

35
namespace PracticePlugin
46
{
57
public class SpeedSettingsCreator : MonoBehaviour
68
{
9+
public event Action<float> ValueChangedEvent;
10+
711
private GameObject _speedSettings;
12+
private TMP_Text _leaderboardText;
13+
private float _newTimeScale = 1;
814

915
private void OnEnable()
1016
{
17+
_leaderboardText = new GameObject("Leaderboard Text").AddComponent<TextMeshProUGUI>();
18+
var rectTransform = (RectTransform) _leaderboardText.transform;
19+
rectTransform.SetParent(transform, false);
20+
rectTransform.anchorMin = Vector2.right * 0.5f;
21+
rectTransform.anchorMax = Vector2.right * 0.5f;
22+
rectTransform.sizeDelta = new Vector2(100, 10);
23+
rectTransform.anchoredPosition = new Vector2(0, 15);
24+
_leaderboardText.fontSize = 4f;
25+
_leaderboardText.alignment = TextAlignmentOptions.Center;
26+
if (Plugin.HasTimeScaleChanged)
27+
{
28+
_leaderboardText.text = "Leaderboard has been disabled\nSet speed to 100% and restart to enable again";
29+
}
30+
1131
_speedSettings = Instantiate(Plugin.SettingsObject, transform);
1232
_speedSettings.SetActive(true);
13-
_speedSettings.GetComponent<SpeedSettingsController>().Init();
14-
var rectTransform = (RectTransform) _speedSettings.transform;
33+
34+
rectTransform = (RectTransform) _speedSettings.transform;
1535
rectTransform.anchorMin = Vector2.right * 0.5f;
1636
rectTransform.anchorMax = Vector2.right * 0.5f;
1737
rectTransform.anchoredPosition = new Vector2(0, rectTransform.sizeDelta.y * 1.5f);
38+
39+
40+
var speedController = _speedSettings.GetComponent<SpeedSettingsController>();
41+
speedController.ValueChangedEvent += SpeedControllerOnValueChangedEvent;
42+
speedController.Init();
1843
}
1944

2045
private void OnDisable()
2146
{
22-
if (_speedSettings == null) return;
47+
if (ValueChangedEvent != null)
48+
{
49+
ValueChangedEvent(_newTimeScale);
50+
}
51+
DestroyImmediate(_leaderboardText);
2352
DestroyImmediate(_speedSettings);
2453
}
54+
55+
private void SpeedControllerOnValueChangedEvent(float timeScale)
56+
{
57+
_newTimeScale = timeScale;
58+
if (!Plugin.NoFail && !Plugin.HasTimeScaleChanged && Math.Abs(_newTimeScale - 1) > 0.0000000001f)
59+
{
60+
_leaderboardText.text = "Leaderboard will be disabled!";
61+
}
62+
else
63+
{
64+
_leaderboardText.text = Plugin.HasTimeScaleChanged ? "Leaderboard has been disabled\nSet speed to 100% and restart to enable again" : string.Empty;
65+
}
66+
}
2567
}
2668
}

0 commit comments

Comments
 (0)