From f93d30105b1f3d7caceb09cf4592b35abfad84ee Mon Sep 17 00:00:00 2001 From: Jason Durand 01 Date: Sun, 3 Apr 2022 18:05:13 -0400 Subject: [PATCH 1/2] Only start spawning when starting gameplay --- Assets/Scripts/Arena.cs | 9 ++++++++- Assets/Scripts/Entity.cs | 2 +- Assets/Scripts/GameFlowManager.cs | 18 ++++++++++++------ Assets/Scripts/GameTimer.cs | 18 ++++++++++++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/Arena.cs b/Assets/Scripts/Arena.cs index f75403f..7defbef 100644 --- a/Assets/Scripts/Arena.cs +++ b/Assets/Scripts/Arena.cs @@ -36,7 +36,9 @@ public class Arena : MonoBehaviour { void Awake() => safeZone = GetComponentInChildren(); - void Start() => StartCoroutine(SpawnEnemies()); + void Start() => gameFlowManager.stateChanged += OnGameFlowStateChanged; + + void OnDestroy() => gameFlowManager.stateChanged -= OnGameFlowStateChanged; void SpawnEnemy(int spawnerIndex) { if (!gameFlowManager.CanDoAction) @@ -70,6 +72,11 @@ public class Arena : MonoBehaviour { return safeZone.GetMoatExtents(); } + void OnGameFlowStateChanged(BaseState oldState, BaseState newState) { + if (oldState is GameFlowManager.StartFlowState && newState is GameFlowManager.GameplayFlowState) + StartCoroutine(SpawnEnemies()); + } + #if UNITY_EDITOR void OnDrawGizmosSelected() { Gizmos.color = Color.blue; diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index 8d5b42d..ceff41f 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -159,7 +159,7 @@ public class Entity : MonoBehaviour { halo.SetActive(false); } - void OnGameFlowStateChanged(BaseState newState) { + void OnGameFlowStateChanged(BaseState oldState, BaseState newState) { if (gameFlowManager.pauseLevel >= GameFlowManager.PauseLevel.NothingMoves) rb.velocity = Vector2.zero; } diff --git a/Assets/Scripts/GameFlowManager.cs b/Assets/Scripts/GameFlowManager.cs index 1b60188..6928c16 100644 --- a/Assets/Scripts/GameFlowManager.cs +++ b/Assets/Scripts/GameFlowManager.cs @@ -41,7 +41,7 @@ public class GameFlowManager : MonoBehaviour { [field: SerializeField] TMP_Text startTxt = null!; [field: SerializeField] TMP_Text endTxt = null!; - public event Action? stateChanged; + public event Action? stateChanged; #region Unity Messages @@ -135,9 +135,10 @@ public class GameFlowManager : MonoBehaviour { void SwitchState(BaseState newState) { CurrentState.LeaveState(); + BaseState oldState = CurrentState; CurrentState = newState; newState.EnterState(); - stateChanged?.Invoke(newState); + stateChanged?.Invoke(oldState, newState); } public abstract class GameFlowState : BaseState { @@ -170,7 +171,7 @@ public class GameFlowManager : MonoBehaviour { } } - class StartFlowState : GameFlowState { + public class StartFlowState : GameFlowState { public StartFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} public override void EnterState() { @@ -188,7 +189,7 @@ public class GameFlowManager : MonoBehaviour { public override void LeaveState() => gameFlowManager.startTxt.transform.parent.gameObject.SetActive(false); } - class GameplayFlowState : GameFlowState { + public class GameplayFlowState : GameFlowState { public GameplayFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} public override void EnterState() { @@ -215,6 +216,7 @@ public class GameFlowManager : MonoBehaviour { gameFlowManager.mainMenu.SetMenuMode(MainMenuManager.MenuMode.PauseMenu); gameFlowManager.SetPauseLevel(PauseLevel.TimeStop); gameFlowManager.blurFade.SetBlurred(true); + gameFlowManager.gameTimer.showHighScore = true; } public override void LeaveState() { @@ -224,6 +226,7 @@ public class GameFlowManager : MonoBehaviour { gameFlowManager.mainMenu.gameObject.SetActive(false); gameFlowManager.SetPauseLevel(PauseLevel.NotPaused); gameFlowManager.blurFade.SetBlurred(false); + gameFlowManager.gameTimer.showHighScore = false; } } @@ -233,12 +236,15 @@ public class GameFlowManager : MonoBehaviour { public override void EnterState() { base.EnterState(); - Debug.Log("You died!\nPress Accept to restart!"); gameFlowManager.SetPauseLevel(PauseLevel.PreventActions); gameFlowManager.blurFade.SetBlurred(true); + gameFlowManager.gameTimer.showHighScore = true; } - public override void LeaveState() => gameFlowManager.blurFade.SetBlurred(false); + public override void LeaveState() { + gameFlowManager.blurFade.SetBlurred(false); + gameFlowManager.gameTimer.showHighScore = false; + } public void ReloadGame() { Debug.Log("Reloading scene..."); diff --git a/Assets/Scripts/GameTimer.cs b/Assets/Scripts/GameTimer.cs index 5034abd..bcf0e39 100644 --- a/Assets/Scripts/GameTimer.cs +++ b/Assets/Scripts/GameTimer.cs @@ -5,11 +5,15 @@ using UnityEngine; public class GameTimer : MonoBehaviour { TMP_Text label; float timer; + float highScore; public bool stopped; + public bool showHighScore; + const string HighScoreKey = "High Score"; void Awake() { label = GetComponent(); timer = 0f; + highScore = PlayerPrefs.GetFloat(HighScoreKey); stopped = true; } @@ -18,7 +22,21 @@ public class GameTimer : MonoBehaviour { return; timer += Time.deltaTime; + SaveHighScore(); label.text = TimeSpan.FromSeconds(timer) .ToString(@"mm\:ss"); + if (showHighScore) + label.text += "\nBest Time: " + TimeSpan.FromSeconds(highScore) + .ToString(@"mm\:ss"); + } + + public void SaveHighScore() { + PlayerPrefs.SetFloat(HighScoreKey, timer); + highScore = timer; + } + + public void ResetHighScore() { + PlayerPrefs.DeleteKey(HighScoreKey); + highScore = 0f; } } \ No newline at end of file From 5f4b0142db47a6a245d734f6fdfd18d5ac4649a3 Mon Sep 17 00:00:00 2001 From: Jason Durand 01 Date: Sun, 3 Apr 2022 18:23:46 -0400 Subject: [PATCH 2/2] Very simple high score --- Assets/Scripts/GameFlowManager.cs | 12 +++++------ Assets/Scripts/GameTimer.cs | 34 ++++++++++++++++++++++++++----- Assets/Scripts/MainMenuManager.cs | 4 +++- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/Assets/Scripts/GameFlowManager.cs b/Assets/Scripts/GameFlowManager.cs index 6928c16..e9b999b 100644 --- a/Assets/Scripts/GameFlowManager.cs +++ b/Assets/Scripts/GameFlowManager.cs @@ -195,14 +195,14 @@ public class GameFlowManager : MonoBehaviour { public override void EnterState() { base.EnterState(); - gameFlowManager.gameTimer.stopped = false; + gameFlowManager.gameTimer.Stopped = false; gameFlowManager.SetPauseLevel(PauseLevel.NotPaused); } public override void 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.SetPauseLevel(PauseLevel.TimeStop); gameFlowManager.blurFade.SetBlurred(true); - gameFlowManager.gameTimer.showHighScore = true; + gameFlowManager.gameTimer.ShowHighScore = true; } public override void LeaveState() { @@ -226,7 +226,7 @@ public class GameFlowManager : MonoBehaviour { gameFlowManager.mainMenu.gameObject.SetActive(false); gameFlowManager.SetPauseLevel(PauseLevel.NotPaused); 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.blurFade.SetBlurred(true); - gameFlowManager.gameTimer.showHighScore = true; + gameFlowManager.gameTimer.ShowHighScore = true; } public override void LeaveState() { gameFlowManager.blurFade.SetBlurred(false); - gameFlowManager.gameTimer.showHighScore = false; + gameFlowManager.gameTimer.ShowHighScore = false; } public void ReloadGame() { diff --git a/Assets/Scripts/GameTimer.cs b/Assets/Scripts/GameTimer.cs index bcf0e39..75b1336 100644 --- a/Assets/Scripts/GameTimer.cs +++ b/Assets/Scripts/GameTimer.cs @@ -6,31 +6,55 @@ public class GameTimer : MonoBehaviour { TMP_Text label; float timer; float highScore; - public bool stopped; - public bool showHighScore; + bool stopped; + + 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"; void Awake() { label = GetComponent(); timer = 0f; highScore = PlayerPrefs.GetFloat(HighScoreKey); - stopped = true; + Stopped = true; } void Update() { - if (stopped) + if (Stopped) return; timer += Time.deltaTime; SaveHighScore(); + UpdateLabel(); + } + + void UpdateLabel() { label.text = TimeSpan.FromSeconds(timer) .ToString(@"mm\:ss"); - if (showHighScore) + if (ShowHighScore) label.text += "\nBest Time: " + TimeSpan.FromSeconds(highScore) .ToString(@"mm\:ss"); } public void SaveHighScore() { + if (timer <= highScore) + return; + PlayerPrefs.SetFloat(HighScoreKey, timer); highScore = timer; } diff --git a/Assets/Scripts/MainMenuManager.cs b/Assets/Scripts/MainMenuManager.cs index 1b112a7..bf602a2 100644 --- a/Assets/Scripts/MainMenuManager.cs +++ b/Assets/Scripts/MainMenuManager.cs @@ -26,7 +26,9 @@ public class MainMenuManager : MonoBehaviour { public void SetMenuMode(MenuMode 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() {