Only start spawning when starting gameplay

This commit is contained in:
Jason Durand 01 2022-04-03 18:05:13 -04:00
parent 5bd494aaca
commit f93d30105b
4 changed files with 39 additions and 8 deletions

View File

@ -36,7 +36,9 @@ public class Arena : MonoBehaviour {
void Awake() => safeZone = GetComponentInChildren<SafeZone>(); void Awake() => safeZone = GetComponentInChildren<SafeZone>();
void Start() => StartCoroutine(SpawnEnemies()); void Start() => gameFlowManager.stateChanged += OnGameFlowStateChanged;
void OnDestroy() => gameFlowManager.stateChanged -= OnGameFlowStateChanged;
void SpawnEnemy(int spawnerIndex) { void SpawnEnemy(int spawnerIndex) {
if (!gameFlowManager.CanDoAction) if (!gameFlowManager.CanDoAction)
@ -70,6 +72,11 @@ public class Arena : MonoBehaviour {
return safeZone.GetMoatExtents(); return safeZone.GetMoatExtents();
} }
void OnGameFlowStateChanged(BaseState oldState, BaseState newState) {
if (oldState is GameFlowManager.StartFlowState && newState is GameFlowManager.GameplayFlowState)
StartCoroutine(SpawnEnemies());
}
#if UNITY_EDITOR #if UNITY_EDITOR
void OnDrawGizmosSelected() { void OnDrawGizmosSelected() {
Gizmos.color = Color.blue; Gizmos.color = Color.blue;

View File

@ -159,7 +159,7 @@ public class Entity : MonoBehaviour {
halo.SetActive(false); halo.SetActive(false);
} }
void OnGameFlowStateChanged(BaseState newState) { void OnGameFlowStateChanged(BaseState oldState, BaseState newState) {
if (gameFlowManager.pauseLevel >= GameFlowManager.PauseLevel.NothingMoves) if (gameFlowManager.pauseLevel >= GameFlowManager.PauseLevel.NothingMoves)
rb.velocity = Vector2.zero; rb.velocity = Vector2.zero;
} }

View File

@ -41,7 +41,7 @@ public class GameFlowManager : MonoBehaviour {
[field: SerializeField] TMP_Text startTxt = null!; [field: SerializeField] TMP_Text startTxt = null!;
[field: SerializeField] TMP_Text endTxt = null!; [field: SerializeField] TMP_Text endTxt = null!;
public event Action<BaseState>? stateChanged; public event Action<BaseState, BaseState>? stateChanged;
#region Unity Messages #region Unity Messages
@ -135,9 +135,10 @@ public class GameFlowManager : MonoBehaviour {
void SwitchState(BaseState newState) { void SwitchState(BaseState newState) {
CurrentState.LeaveState(); CurrentState.LeaveState();
BaseState oldState = CurrentState;
CurrentState = newState; CurrentState = newState;
newState.EnterState(); newState.EnterState();
stateChanged?.Invoke(newState); stateChanged?.Invoke(oldState, newState);
} }
public abstract class GameFlowState : BaseState { 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 StartFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {}
public override void EnterState() { public override void EnterState() {
@ -188,7 +189,7 @@ public class GameFlowManager : MonoBehaviour {
public override void LeaveState() => gameFlowManager.startTxt.transform.parent.gameObject.SetActive(false); 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 GameplayFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {}
public override void EnterState() { public override void EnterState() {
@ -215,6 +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;
} }
public override void LeaveState() { public override void LeaveState() {
@ -224,6 +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;
} }
} }
@ -233,12 +236,15 @@ public class GameFlowManager : MonoBehaviour {
public override void EnterState() { public override void EnterState() {
base.EnterState(); base.EnterState();
Debug.Log("You died!\nPress Accept to restart!");
gameFlowManager.SetPauseLevel(PauseLevel.PreventActions); gameFlowManager.SetPauseLevel(PauseLevel.PreventActions);
gameFlowManager.blurFade.SetBlurred(true); 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() { public void ReloadGame() {
Debug.Log("Reloading scene..."); Debug.Log("Reloading scene...");

View File

@ -5,11 +5,15 @@ using UnityEngine;
public class GameTimer : MonoBehaviour { public class GameTimer : MonoBehaviour {
TMP_Text label; TMP_Text label;
float timer; float timer;
float highScore;
public bool stopped; public bool stopped;
public bool showHighScore;
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);
stopped = true; stopped = true;
} }
@ -18,7 +22,21 @@ public class GameTimer : MonoBehaviour {
return; return;
timer += Time.deltaTime; timer += Time.deltaTime;
SaveHighScore();
label.text = TimeSpan.FromSeconds(timer) label.text = TimeSpan.FromSeconds(timer)
.ToString(@"mm\:ss"); .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;
} }
} }