Skip to content

Commit

Permalink
Some simplifications routing through event system
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-Lovell committed Nov 25, 2024
1 parent db3e3db commit a02250a
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 85 deletions.
3 changes: 2 additions & 1 deletion .idea/.idea.Doggo-Nogo/.idea/projectSettingsUpdater.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions .idea/.idea.Doggo-Nogo/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 48 additions & 7 deletions Assets/Scripts/Controllers/Level1/Level1Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -39,6 +41,7 @@ private void Awake() // initialised this controller and related processes
}

InitializeComponents();
PlayIntroAnimation();
}

private bool ValidateControllers()
Expand All @@ -60,19 +63,51 @@ private void InitializeComponents()
_trialController = GetComponent<Level1TrialController>();
_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;
Expand Down Expand Up @@ -111,13 +146,18 @@ 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
);

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)
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 0 additions & 24 deletions Assets/Scripts/Controllers/Level1/Level1IntroController.cs

This file was deleted.

This file was deleted.

50 changes: 32 additions & 18 deletions Assets/Scripts/Controllers/Level1/Level1UIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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...
Expand All @@ -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);
Expand Down
26 changes: 20 additions & 6 deletions Assets/Scripts/Core/Events/Level1Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> OnNewTrialStarted;
public static event Action<TrialResult> OnTrialCompleted;
Expand All @@ -18,16 +30,14 @@ public static class Level1Events

// UI events
public static event Action<int> OnScoreUpdated;

public static event Action<TrialState> OnTrialStateChanged;
public static event Action<double> OnReactionTimeRecorded;
public static event Action<float> OnMedianRTUpdated;
public static event Action<string> 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<string, float> stimSpec) => OnStimulusShown?.Invoke(stimSpec);
public static void TrialCompleted(TrialResult result) => OnTrialCompleted?.Invoke(result);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -33,19 +31,11 @@ private IEnumerator IntroSequence()
yield return StartCoroutine(Swing(woodenSign.GetComponent<RectTransform>()));
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,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit a02250a

Please sign in to comment.