Very simple high score

This commit is contained in:
Jason Durand 01 2022-04-03 18:23:46 -04:00
parent f93d30105b
commit 5f4b0142db
3 changed files with 38 additions and 12 deletions

View File

@ -195,14 +195,14 @@ public class GameFlowManager : MonoBehaviour {
public override void EnterState() { public override void EnterState() {
base.EnterState(); base.EnterState();
gameFlowManager.gameTimer.stopped = false; gameFlowManager.gameTimer.Stopped = false;
gameFlowManager.SetPauseLevel(PauseLevel.NotPaused); gameFlowManager.SetPauseLevel(PauseLevel.NotPaused);
} }
public override void LeaveState() { public override void LeaveState() {
base.LeaveState(); base.LeaveState();
gameFlowManager.gameTimer.stopped = true; gameFlowManager.gameTimer.Stopped = true;
} }
} }
@ -216,7 +216,7 @@ public class GameFlowManager : MonoBehaviour {
gameFlowManager.mainMenu.SetMenuMode(MainMenuManager.MenuMode.PauseMenu); gameFlowManager.mainMenu.SetMenuMode(MainMenuManager.MenuMode.PauseMenu);
gameFlowManager.SetPauseLevel(PauseLevel.TimeStop); gameFlowManager.SetPauseLevel(PauseLevel.TimeStop);
gameFlowManager.blurFade.SetBlurred(true); gameFlowManager.blurFade.SetBlurred(true);
gameFlowManager.gameTimer.showHighScore = true; gameFlowManager.gameTimer.ShowHighScore = true;
} }
public override void LeaveState() { public override void LeaveState() {
@ -226,7 +226,7 @@ public class GameFlowManager : MonoBehaviour {
gameFlowManager.mainMenu.gameObject.SetActive(false); gameFlowManager.mainMenu.gameObject.SetActive(false);
gameFlowManager.SetPauseLevel(PauseLevel.NotPaused); gameFlowManager.SetPauseLevel(PauseLevel.NotPaused);
gameFlowManager.blurFade.SetBlurred(false); gameFlowManager.blurFade.SetBlurred(false);
gameFlowManager.gameTimer.showHighScore = false; gameFlowManager.gameTimer.ShowHighScore = false;
} }
} }
@ -238,12 +238,12 @@ public class GameFlowManager : MonoBehaviour {
gameFlowManager.SetPauseLevel(PauseLevel.PreventActions); gameFlowManager.SetPauseLevel(PauseLevel.PreventActions);
gameFlowManager.blurFade.SetBlurred(true); gameFlowManager.blurFade.SetBlurred(true);
gameFlowManager.gameTimer.showHighScore = true; gameFlowManager.gameTimer.ShowHighScore = true;
} }
public override void LeaveState() { public override void LeaveState() {
gameFlowManager.blurFade.SetBlurred(false); gameFlowManager.blurFade.SetBlurred(false);
gameFlowManager.gameTimer.showHighScore = false; gameFlowManager.gameTimer.ShowHighScore = false;
} }
public void ReloadGame() { public void ReloadGame() {

View File

@ -6,31 +6,55 @@ public class GameTimer : MonoBehaviour {
TMP_Text label; TMP_Text label;
float timer; float timer;
float highScore; float highScore;
public bool stopped; bool stopped;
public bool showHighScore;
public bool Stopped {
get => stopped;
set {
stopped = value;
UpdateLabel();
}
}
public bool ShowHighScore {
get => showHighScore;
set {
showHighScore = value;
UpdateLabel();
}
}
bool showHighScore;
const string HighScoreKey = "High Score"; const string HighScoreKey = "High Score";
void Awake() { void Awake() {
label = GetComponent<TMP_Text>(); label = GetComponent<TMP_Text>();
timer = 0f; timer = 0f;
highScore = PlayerPrefs.GetFloat(HighScoreKey); highScore = PlayerPrefs.GetFloat(HighScoreKey);
stopped = true; Stopped = true;
} }
void Update() { void Update() {
if (stopped) if (Stopped)
return; return;
timer += Time.deltaTime; timer += Time.deltaTime;
SaveHighScore(); SaveHighScore();
UpdateLabel();
}
void UpdateLabel() {
label.text = TimeSpan.FromSeconds(timer) label.text = TimeSpan.FromSeconds(timer)
.ToString(@"mm\:ss"); .ToString(@"mm\:ss");
if (showHighScore) if (ShowHighScore)
label.text += "\nBest Time: " + TimeSpan.FromSeconds(highScore) label.text += "\nBest Time: " + TimeSpan.FromSeconds(highScore)
.ToString(@"mm\:ss"); .ToString(@"mm\:ss");
} }
public void SaveHighScore() { public void SaveHighScore() {
if (timer <= highScore)
return;
PlayerPrefs.SetFloat(HighScoreKey, timer); PlayerPrefs.SetFloat(HighScoreKey, timer);
highScore = timer; highScore = timer;
} }

View File

@ -26,7 +26,9 @@ public class MainMenuManager : MonoBehaviour {
public void SetMenuMode(MenuMode mode) { public void SetMenuMode(MenuMode mode) {
this.mode = mode; this.mode = mode;
startButton.gameObject.SetActive(mode == MenuMode.MainMenu); bool mainMenu = mode == MenuMode.MainMenu;
startButton.gameObject.SetActive(mainMenu);
titleLable.gameObject.SetActive(mainMenu);
} }
public void ResetMenuState() { public void ResetMenuState() {