Merge branch 'dev' into yann

This commit is contained in:
Yann Dupont 01 2022-04-03 18:31:38 -04:00
commit 3fefb5278f
5 changed files with 71 additions and 14 deletions

View File

@ -51,7 +51,9 @@ public class Arena : MonoBehaviour {
soundManager = FindObjectOfType<SoundManager>();
}
void Start() => StartCoroutine(SpawnEnemies());
void Start() => gameFlowManager.stateChanged += OnGameFlowStateChanged;
void OnDestroy() => gameFlowManager.stateChanged -= OnGameFlowStateChanged;
void SpawnEnemy(int spawnerIndex) {
if (!gameFlowManager.CanDoAction)
@ -93,6 +95,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;

View File

@ -171,7 +171,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;
}

View File

@ -41,7 +41,7 @@ public class GameFlowManager : MonoBehaviour {
[field: SerializeField] TMP_Text startTxt = null!;
[field: SerializeField] TMP_Text endTxt = null!;
public event Action<BaseState>? stateChanged;
public event Action<BaseState, BaseState>? 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,20 +189,20 @@ 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() {
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;
}
}
@ -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...");

View File

@ -5,20 +5,62 @@ using UnityEngine;
public class GameTimer : MonoBehaviour {
TMP_Text label;
float timer;
public bool stopped;
float highScore;
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<TMP_Text>();
timer = 0f;
stopped = true;
highScore = PlayerPrefs.GetFloat(HighScoreKey);
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)
label.text += "\nBest Time: " + TimeSpan.FromSeconds(highScore)
.ToString(@"mm\:ss");
}
public void SaveHighScore() {
if (timer <= highScore)
return;
PlayerPrefs.SetFloat(HighScoreKey, timer);
highScore = timer;
}
public void ResetHighScore() {
PlayerPrefs.DeleteKey(HighScoreKey);
highScore = 0f;
}
}

View File

@ -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() {