From a02250a7cee202a0b281a149da62423fb96bbde1 Mon Sep 17 00:00:00 2001 From: max-lovell Date: Mon, 25 Nov 2024 17:19:52 +0000 Subject: [PATCH] Some simplifications routing through event system --- .../.idea/projectSettingsUpdater.xml | 3 +- .idea/.idea.Doggo-Nogo/.idea/workspace.xml | 22 +++----- .../Controllers/Level1/Level1Controller.cs | 55 ++++++++++++++++--- .../Level1/Level1IntroController.cs | 24 -------- .../Level1/Level1IntroController.cs.meta | 3 - .../Controllers/Level1/Level1UIController.cs | 50 +++++++++++------ Assets/Scripts/Core/Events/Level1Events.cs | 26 +++++++-- .../Level1Screen/Level1IntroductionView.cs | 16 +----- .../Screens/Level1Screen/Level1ViewManager.cs | 5 ++ 9 files changed, 119 insertions(+), 85 deletions(-) delete mode 100644 Assets/Scripts/Controllers/Level1/Level1IntroController.cs delete mode 100644 Assets/Scripts/Controllers/Level1/Level1IntroController.cs.meta diff --git a/.idea/.idea.Doggo-Nogo/.idea/projectSettingsUpdater.xml b/.idea/.idea.Doggo-Nogo/.idea/projectSettingsUpdater.xml index 4bb9f4d..64af657 100644 --- a/.idea/.idea.Doggo-Nogo/.idea/projectSettingsUpdater.xml +++ b/.idea/.idea.Doggo-Nogo/.idea/projectSettingsUpdater.xml @@ -1,6 +1,7 @@ - \ No newline at end of file diff --git a/.idea/.idea.Doggo-Nogo/.idea/workspace.xml b/.idea/.idea.Doggo-Nogo/.idea/workspace.xml index bab3d3c..2b2acd3 100644 --- a/.idea/.idea.Doggo-Nogo/.idea/workspace.xml +++ b/.idea/.idea.Doggo-Nogo/.idea/workspace.xml @@ -5,13 +5,15 @@ + - - - - - - + + + + + + + @@ -168,6 +163,7 @@ + diff --git a/Assets/Scripts/Controllers/Level1/Level1Controller.cs b/Assets/Scripts/Controllers/Level1/Level1Controller.cs index ebe59dd..3be116d 100644 --- a/Assets/Scripts/Controllers/Level1/Level1Controller.cs +++ b/Assets/Scripts/Controllers/Level1/Level1Controller.cs @@ -10,7 +10,6 @@ public class Level1Controller : MonoBehaviour [Header("Sub Controllers")] [SerializeField] private Level1UIController uiController; - [SerializeField] private Level1IntroController introController; [Header("View References")] [SerializeField] private BoneView boneView; @@ -22,6 +21,9 @@ public class Level1Controller : MonoBehaviour private ReactionTimeProcessor _rtProcessor; private ScoreCalculator _scoreCalculator; private bool _isLevelActive; + private bool _waitingForInstructionsContinueInput; + private bool _waitingForReadyInput; + private bool _changeStagePaused; private void Awake() // initialised this controller and related processes { @@ -39,6 +41,7 @@ private void Awake() // initialised this controller and related processes } InitializeComponents(); + PlayIntroAnimation(); } private bool ValidateControllers() @@ -60,19 +63,51 @@ private void InitializeComponents() _trialController = GetComponent(); _levelData = new Level1Data(gameConfig); _stageData = new Level1StageData(_gameData.metadata, gameConfig); + // Level control bools _isLevelActive = true; + _waitingForInstructionsContinueInput = false; + _waitingForReadyInput = false; + _changeStagePaused = false; GameEvents.GamePhaseChanged(GamePhase.Level1); - - // Run intro - introController.StartIntro(); } - public void StartLevel() + private static void PlayIntroAnimation() + { + Level1Events.Level1Start(); // Trigger level1 + } + + public void AllowIntroContinue() + { + _waitingForInstructionsContinueInput = true; + } + + public void AllowReadyContinue() + { + _waitingForReadyInput = true; + } + + private void Update() { - Level1Events.LevelStarted(); // Guess nothing happens if nothing attached? - _trialController.StartNewTrial(); + if (_waitingForInstructionsContinueInput && Input.GetKeyDown(KeyCode.Space)) + { + _waitingForInstructionsContinueInput = false; + Level1Events.IntroComplete(); + } else if (_waitingForReadyInput && Input.GetKeyDown(KeyCode.DownArrow)) + { + _waitingForReadyInput = false; + Level1Events.LevelStarted(); + _trialController.StartNewTrial(); + } + else if (_changeStagePaused && Input.GetKeyDown(KeyCode.DownArrow)) + { + _changeStagePaused = false; + Level1Events.StageInputReceived(); + _trialController.StartNewTrial(); + } } + + // Processing data public void ProcessTrialResult(double reactionTime) { if (!_isLevelActive) return; @@ -111,6 +146,7 @@ public void ProcessTrialResult(double reactionTime) private void CheckStageProgress(TrialResult result) { if (result.TotalScore < _stageData.CurrentTargetScore || !_stageData.AdvanceStage()) return; + int newTargetScore = _stageData.CalculateTargetScore( _levelData.validTrialCount, gameConfig.MinScore @@ -118,6 +154,10 @@ private void CheckStageProgress(TrialResult result) Level1Events.StageChanged(_stageData.CurrentStage, newTargetScore); _stageData.CurrentTargetScore = newTargetScore; + + // Wait for input after stage change + _changeStagePaused = true; + Level1Events.WaitingForStageInput(); } private void UpdateLevelData(bool isValidTrial, double reactionTime) @@ -147,6 +187,7 @@ private TrialResult CreateTrialResult(double reactionTime, string responseType, }; } + // Ending level private bool ShouldEndLevel() { return _stageData.CurrentStage == _stageData.TotalStages && _levelData.currentScore >= _stageData.CurrentTargetScore; diff --git a/Assets/Scripts/Controllers/Level1/Level1IntroController.cs b/Assets/Scripts/Controllers/Level1/Level1IntroController.cs deleted file mode 100644 index 60194d7..0000000 --- a/Assets/Scripts/Controllers/Level1/Level1IntroController.cs +++ /dev/null @@ -1,24 +0,0 @@ -using UnityEngine; - -public class Level1IntroController : MonoBehaviour -{ - private void OnEnable() - { - Level1Events.OnIntroComplete += HandleIntroComplete; - } - - private void OnDisable() - { - Level1Events.OnIntroComplete -= HandleIntroComplete; - } - - public void StartIntro() - { - Level1Events.IntroStarted(); - } - - private void HandleIntroComplete() - { - Level1Controller.Instance.StartLevel(); - } -} \ No newline at end of file diff --git a/Assets/Scripts/Controllers/Level1/Level1IntroController.cs.meta b/Assets/Scripts/Controllers/Level1/Level1IntroController.cs.meta deleted file mode 100644 index 55821dc..0000000 --- a/Assets/Scripts/Controllers/Level1/Level1IntroController.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 2e2cab227d7c479091f20111cf8621a7 -timeCreated: 1732191996 \ No newline at end of file diff --git a/Assets/Scripts/Controllers/Level1/Level1UIController.cs b/Assets/Scripts/Controllers/Level1/Level1UIController.cs index 204dd2b..686ad11 100644 --- a/Assets/Scripts/Controllers/Level1/Level1UIController.cs +++ b/Assets/Scripts/Controllers/Level1/Level1UIController.cs @@ -2,9 +2,12 @@ using UnityEngine; using UnityEngine.Serialization; +// So this contains references to the views and UI, whilst level 1 controller doesn't. +// Really some of the handlers should feature in level1 controller public class Level1UIController : MonoBehaviour { - [FormerlySerializedAs("levelUI")] [SerializeField] private Level1ViewManager levelViewManager; + + [SerializeField] private Level1ViewManager levelViewManager; private int _currentHealthBarIndex = 0; private Level1IntroductionView _introView; @@ -15,38 +18,54 @@ private void Awake() private void OnEnable() { - // Intro events - Level1Events.OnIntroStarted += HandleIntroStarted; + // Setup events + Level1Events.OnLevel1Start += HandleLevel1Start; + Level1Events.OnIntroAnimationComplete += HandleIntroAnimationComplete; + Level1Events.OnIntroComplete += HandleIntroComplete; + Level1Events.OnLevelStarted += HandleLevelStarted; - // Existing gameplay events + // Level change events Level1Events.OnStageChanged += HandleStageChange; Level1Events.OnTrialCompleted += HandleTrialCompleted; - Level1Events.OnLevelStarted += HandleLevelStarted; Level1Events.OnScoreUpdated += HandleScoreUpdated; } private void OnDisable() { - // Intro events - Level1Events.OnIntroStarted -= HandleIntroStarted; - Level1Events.OnIntroComplete -= HandleIntroComplete; + // Setup events + Level1Events.OnLevel1Start -= HandleLevel1Start; + Level1Events.OnIntroAnimationComplete -= HandleIntroAnimationComplete; - // Existing gameplay events + Level1Events.OnIntroComplete -= HandleIntroComplete; + Level1Events.OnLevelStarted -= HandleLevelStarted; + + // Level change events Level1Events.OnStageChanged -= HandleStageChange; Level1Events.OnTrialCompleted -= HandleTrialCompleted; - Level1Events.OnLevelStarted -= HandleLevelStarted; Level1Events.OnScoreUpdated -= HandleScoreUpdated; } - - private void HandleIntroStarted() + + private void HandleLevel1Start() { - _introView.Initialize(); + _introView.PlayInstructionsSignAnimation(); } + private void HandleIntroAnimationComplete() + { + Level1Controller.Instance.AllowIntroContinue(); + } + private void HandleIntroComplete() { levelViewManager.SwitchToGameplayUI(); + Level1Controller.Instance.AllowReadyContinue(); + } + + private void HandleLevelStarted() + { + levelViewManager.ClearInstructions(); + _currentHealthBarIndex = 0; } // Existing handlers... @@ -67,11 +86,6 @@ private IEnumerator HandleStageTransition(int newStage, int targetScore) yield return StartCoroutine(levelViewManager.HandleLevelTransition(newStage)); } - private void HandleLevelStarted() - { - _currentHealthBarIndex = 0; - } - private void HandleTrialCompleted(TrialResult result) { levelViewManager.DisplayTrialResult(result); diff --git a/Assets/Scripts/Core/Events/Level1Events.cs b/Assets/Scripts/Core/Events/Level1Events.cs index 2de67c0..2aa9bfa 100644 --- a/Assets/Scripts/Core/Events/Level1Events.cs +++ b/Assets/Scripts/Core/Events/Level1Events.cs @@ -3,9 +3,21 @@ public static class Level1Events { + // Intro - public static event Action OnIntroStarted; + public static event Action OnLevel1Start; + public static void Level1Start() => OnLevel1Start?.Invoke(); // Start intro animation on load + + public static event Action OnIntroAnimationComplete; + public static void IntroAnimationComplete() => OnIntroAnimationComplete?.Invoke(); // On end intro animation allow spacebar to move the scene on + public static event Action OnIntroComplete; + public static void IntroComplete() => OnIntroComplete?.Invoke(); // Allow for 'ready? press down arrow' continue + + // Pause + public static event Action OnWaitingForStartInput; + public static event Action OnWaitingForStageInput; + public static event Action OnStageInputReceived; // Trial events public static event Action OnNewTrialStarted; public static event Action OnTrialCompleted; @@ -18,16 +30,14 @@ public static class Level1Events // UI events public static event Action OnScoreUpdated; - public static event Action OnTrialStateChanged; public static event Action OnReactionTimeRecorded; public static event Action OnMedianRTUpdated; public static event Action OnInvalidResponse; - - - public static void IntroStarted() => OnIntroStarted?.Invoke(); - public static void IntroComplete() => OnIntroComplete?.Invoke(); + public static void LevelStarted() => OnLevelStarted?.Invoke(); + + public static void NewTrialStarted(double isi) => OnNewTrialStarted?.Invoke(isi); public static void StimulusShown(Dictionary stimSpec) => OnStimulusShown?.Invoke(stimSpec); public static void TrialCompleted(TrialResult result) => OnTrialCompleted?.Invoke(result); @@ -41,6 +51,10 @@ public static void StageChanged(int stage, int targetScore) => public static void ReactionTimeRecorded(double rt) => OnReactionTimeRecorded?.Invoke(rt); public static void MedianRTUpdated(float medianRT) => OnMedianRTUpdated?.Invoke(medianRT); public static void InvalidResponse(string reason) => OnInvalidResponse?.Invoke(reason); + + public static void WaitingForStartInput() => OnWaitingForStartInput?.Invoke(); + public static void WaitingForStageInput() => OnWaitingForStageInput?.Invoke(); + public static void StageInputReceived() => OnStageInputReceived?.Invoke(); } public enum TrialState diff --git a/Assets/Scripts/Views/Screens/Level1Screen/Level1IntroductionView.cs b/Assets/Scripts/Views/Screens/Level1Screen/Level1IntroductionView.cs index 03093b7..03fbd8e 100644 --- a/Assets/Scripts/Views/Screens/Level1Screen/Level1IntroductionView.cs +++ b/Assets/Scripts/Views/Screens/Level1Screen/Level1IntroductionView.cs @@ -17,10 +17,8 @@ public class Level1IntroductionView : MonoBehaviour [SerializeField] private float swingDuration = 4f; [SerializeField] private float swingFrequency = 3f; [SerializeField] private float swingDampening = 3f; - - private bool _viewingInstructions = true; // Toggle to stop double presses of spacebar - public void Initialize() + public void PlayInstructionsSignAnimation() { StartCoroutine(IntroSequence()); } @@ -33,19 +31,11 @@ private IEnumerator IntroSequence() yield return StartCoroutine(Swing(woodenSign.GetComponent())); yield return new WaitForSeconds(2); yield return StartCoroutine(UIAnimationController.Instance.FadeIn(continueText, 1f)); - } - - private void Update() - { - if (_viewingInstructions && Input.GetKeyDown(KeyCode.Space)) - { - CompleteIntro(); - } + Level1Events.IntroAnimationComplete(); // See level 1 intro controller } - private void CompleteIntro() + private void CompleteLevel1Intro() { - _viewingInstructions = false; woodenSign.SetActive(false); Level1Events.IntroComplete(); // Triggers OnIntroComplete Event loaded in L1IntroController, } diff --git a/Assets/Scripts/Views/Screens/Level1Screen/Level1ViewManager.cs b/Assets/Scripts/Views/Screens/Level1Screen/Level1ViewManager.cs index 3cc9bf6..c387046 100644 --- a/Assets/Scripts/Views/Screens/Level1Screen/Level1ViewManager.cs +++ b/Assets/Scripts/Views/Screens/Level1Screen/Level1ViewManager.cs @@ -42,6 +42,11 @@ public void DisplayTrialResult(TrialResult result) feedbackView.DisplayTrialResult(result); } + public void ClearInstructions() + { + feedbackView.SetPrompt(""); + } + public IEnumerator HandleLevelTransition(int newStage) { yield return StartCoroutine(feedbackView.HandleLevelChange(newStage));