1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SceneTransitionManager : MonoBehaviour
{
public enum SceneState
{
MainMenu,
LevelSelect,
InGame
}
public static SceneTransitionManager instance;
public string uiScene = "_UI";
public string mainMenuScene = "_StartMenu";
public string levelSelectScene;
public Image placeholderCloudsTransition;
public TMP_Text loadingText;
public float cloudFadeInTime = 1f;
public float cloudFadeOutTime = 2f;
[HideInInspector]
public UnityEvent onSceneChange = new UnityEvent();
private SceneState currentSceneState;
private SceneState lastSceneState;
private int ingameScoreFromLastMatch;
private int goldBonusScoreFromLastMatch;
private int mutatorBonusScoreFromLastMatch;
private string comingFromGameplayScene = "";
private int totalScoreFromLastMatch;
private bool totalScoreFromLastMatchIsNewPersonalRecord;
private LevelData levelDataFromLastMatch;
private bool sceneTransitionIsRunning;
private Color cloudsIn;
private Color cloudsOut;
private Coroutine currentTransition;
public SceneState CurrentSceneState => currentSceneState;
public SceneState LastSceneState => lastSceneState;
public int IngameScoreFromLastMatch => ingameScoreFromLastMatch;
public int GoldBonusScoreFromLastMatch => goldBonusScoreFromLastMatch;
public int MutatorBonusScoreFromLastMatch => mutatorBonusScoreFromLastMatch;
public int TotalScoreFromLastMatch => totalScoreFromLastMatch;
public bool TotalScoreFromLastMatchIsNewPersonalRecord => totalScoreFromLastMatchIsNewPersonalRecord;
public string ComingFromGameplayScene => comingFromGameplayScene;
public LevelData LevelDataFromLastMatch => levelDataFromLastMatch;
public bool SceneTransitionIsRunning => sceneTransitionIsRunning;
private void Awake()
{
if ((bool)instance)
{
Object.Destroy(base.gameObject);
return;
}
instance = this;
Object.DontDestroyOnLoad(base.transform.root.gameObject);
if (!SceneManager.GetSceneByName(uiScene).IsValid())
{
SceneManager.LoadScene(uiScene, LoadSceneMode.Additive);
}
cloudsIn = placeholderCloudsTransition.color;
cloudsOut = cloudsIn;
cloudsOut.a = 0f;
loadingText.alpha = 0f;
}
private void Start()
{
}
public void TransitionFromLevelSelectToLevel(string _levelname)
{
if (currentSceneState != SceneState.LevelSelect)
{
Debug.LogWarning("You should only transition to a specific level from the level selection scene.");
}
else
{
TransitionToScene(_levelname);
}
}
public void TransitionFromNullToLevel(string _levelname)
{
TransitionToScene(_levelname);
}
public void RestartCurrentLevel()
{
TransitionToScene(SceneManager.GetActiveScene().name);
}
public void TransitionFromGameplayToEndScreen(int _score, int _goldBonusScore, int _mutatorBonusScore)
{
comingFromGameplayScene = SceneManager.GetActiveScene().name;
LevelData levelData = (levelDataFromLastMatch = LevelProgressManager.instance.GetLevelDataForScene(comingFromGameplayScene));
if (currentSceneState != SceneState.InGame)
{
Debug.LogWarning("You should only transition to the end screen from a gameply level.");
return;
}
ingameScoreFromLastMatch = _score;
goldBonusScoreFromLastMatch = _goldBonusScore;
mutatorBonusScoreFromLastMatch = _mutatorBonusScore;
levelData.highscore = _score + _goldBonusScore + _mutatorBonusScore;
totalScoreFromLastMatch = levelData.highscore;
totalScoreFromLastMatchIsNewPersonalRecord = totalScoreFromLastMatch > levelData.highscoreBest;
UIFrameManager.TriggerEndOfMatch();
}
public void TransitionFromEndScreenToLevelSelect()
{
TransitionToScene(levelSelectScene);
}
private void TransitionToScene(string _scenename)
{
if (!sceneTransitionIsRunning)
{
lastSceneState = currentSceneState;
if (_scenename == levelSelectScene)
{
currentSceneState = SceneState.LevelSelect;
}
else if (_scenename == mainMenuScene)
{
currentSceneState = SceneState.MainMenu;
}
else
{
currentSceneState = SceneState.InGame;
}
onSceneChange.Invoke();
StartCoroutine(SceneTransitionAnimation(_scenename));
}
}
private float FadeFormula(float f)
{
return Mathf.Pow(f, 4f);
}
private IEnumerator SceneTransitionAnimation(string _scenename)
{
sceneTransitionIsRunning = true;
int sceneCount = SceneManager.sceneCount;
placeholderCloudsTransition.gameObject.SetActive(value: true);
float clock2 = 0f;
while (clock2 <= cloudFadeInTime)
{
float num = Mathf.Clamp(Time.unscaledDeltaTime, 0f, 0.1f);
clock2 += num;
placeholderCloudsTransition.color = Color.Lerp(cloudsOut, cloudsIn, 1f - FadeFormula(1f - clock2 / cloudFadeInTime));
loadingText.alpha = 1f - FadeFormula(1f - clock2 / cloudFadeInTime);
yield return null;
}
placeholderCloudsTransition.color = cloudsIn;
loadingText.alpha = 1f;
List<AsyncOperation> loadOperations = new List<AsyncOperation>();
if (sceneCount > 0)
{
for (int num2 = sceneCount - 1; num2 >= 0; num2--)
{
Scene sceneAt = SceneManager.GetSceneAt(num2);
if (sceneAt.name != uiScene)
{
loadOperations.Add(SceneManager.UnloadSceneAsync(sceneAt));
}
}
}
loadOperations.Add(SceneManager.LoadSceneAsync(_scenename, LoadSceneMode.Additive));
while (loadOperations.Count > 0)
{
for (int num3 = loadOperations.Count - 1; num3 >= 0; num3--)
{
if (loadOperations[num3].isDone)
{
loadOperations.RemoveAt(num3);
}
}
yield return null;
}
SceneManager.SetActiveScene(SceneManager.GetSceneByName(_scenename));
UIFrameManager.instance.CloseAllFrames();
if (_scenename == mainMenuScene)
{
UIFrameManager.instance.SwitchToTitleFrame();
}
clock2 = 0f;
while (clock2 <= cloudFadeOutTime)
{
float num4 = Mathf.Clamp(Time.unscaledDeltaTime, 0f, 0.1f);
clock2 += num4;
placeholderCloudsTransition.color = Color.Lerp(cloudsIn, cloudsOut, FadeFormula(clock2 / cloudFadeOutTime));
loadingText.alpha = 1f - FadeFormula(1f - clock2 / cloudFadeInTime);
yield return null;
}
placeholderCloudsTransition.color = cloudsOut;
loadingText.alpha = 0f;
placeholderCloudsTransition.gameObject.SetActive(value: false);
sceneTransitionIsRunning = false;
}
public void TransitionFromNullToLevelSelect()
{
TransitionToScene(levelSelectScene);
}
public void TransitionFromLevelToLevelSelect()
{
comingFromGameplayScene = SceneManager.GetActiveScene().name;
TransitionToScene(levelSelectScene);
}
public void TransitionToMainMenu()
{
TransitionToScene(mainMenuScene);
}
}
|