From 84fda847a8f8d99a634cab8271881e263fea33c3 Mon Sep 17 00:00:00 2001 From: Maxwell Dodd Date: Mon, 27 Jan 2025 14:41:40 +0100 Subject: [PATCH] Fixed bug where highlighted room would not unhighlight and refactored room highlighting to be habdled by rooms. --- .../Assets/Scripts/GameManager.cs | 30 ++++-------------- PuzzleGameProject/Assets/Scripts/Room.cs | 31 +++++++++++++++---- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/PuzzleGameProject/Assets/Scripts/GameManager.cs b/PuzzleGameProject/Assets/Scripts/GameManager.cs index 9e91b06..d9a3c98 100644 --- a/PuzzleGameProject/Assets/Scripts/GameManager.cs +++ b/PuzzleGameProject/Assets/Scripts/GameManager.cs @@ -25,6 +25,8 @@ public class GameManager : MonoBehaviour private DicePair _dicePairOne = new(); private DicePair _dicePairTwo = new(); public static event Action StateChanged; + public static event Action DiceSelected; + public static event Action DiceUnselected; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() @@ -87,7 +89,7 @@ public class GameManager : MonoBehaviour if (_dicePairOne.AreBothDiceSelected()) { ChangeState(GameState.PickRoomOne); - HighLightValidRoomsWithNumber(_dicePairOne); + DiceSelected?.Invoke(_dicePairOne); } break; case GameState.PickDiceTwo: @@ -95,7 +97,7 @@ public class GameManager : MonoBehaviour if (_dicePairTwo.AreBothDiceSelected()) { ChangeState(GameState.PickRoomTwo); - HighLightValidRoomsWithNumber(_dicePairTwo); + DiceSelected?.Invoke(_dicePairTwo); } break; default: @@ -113,7 +115,7 @@ public class GameManager : MonoBehaviour ChangeState(GameState.PickDiceOne); } _dicePairOne.UnselectDie(die); - UnhighLightRoomsAsOptions(); + DiceUnselected?.Invoke(); break; case GameState.PickDiceTwo: case GameState.PickRoomTwo: @@ -122,7 +124,7 @@ public class GameManager : MonoBehaviour ChangeState(GameState.PickDiceTwo); } _dicePairTwo.UnselectDie(die); - UnhighLightRoomsAsOptions(); + DiceUnselected?.Invoke(); break; } } @@ -132,26 +134,6 @@ public class GameManager : MonoBehaviour StartNewTurn(); } - void HighLightValidRoomsWithNumber(DicePair pair) - { - foreach (Transform roomTransform in rooms.transform) - { - Room room = roomTransform.gameObject.GetComponent(); - if (room.TryUnlock(pair) && room.IsValidRoomToExplore()) - { - room.HighlightRoomAsOption(); - } - } - } - - void UnhighLightRoomsAsOptions() - { - foreach (Transform roomTransform in rooms.transform) - { - roomTransform.gameObject.GetComponent().UnhighlightRoomAsOption(); - } - } - private void DiceRolled(object sender, EventArgs e) { if (state == GameState.RollDice) diff --git a/PuzzleGameProject/Assets/Scripts/Room.cs b/PuzzleGameProject/Assets/Scripts/Room.cs index cc68ede..14472d5 100644 --- a/PuzzleGameProject/Assets/Scripts/Room.cs +++ b/PuzzleGameProject/Assets/Scripts/Room.cs @@ -3,6 +3,7 @@ using System.Collections; using TMPro; using UnityEngine; using System.Collections.Generic; +using Unity.VisualScripting; enum KeyType { @@ -23,10 +24,20 @@ public class Room : MonoBehaviour private bool _isClickable = true; private Color _roomNumberOriginalColor; + private void OnEnable() { + GameManager.StateChanged += HandleStateChange; + GameManager.DiceSelected += HandleDiceSelected; + GameManager.DiceUnselected += HandDiceUnselected; + } + + private void OnDisable() { + GameManager.StateChanged -= HandleStateChange; + GameManager.DiceSelected -= HandleDiceSelected; + GameManager.DiceUnselected -= HandDiceUnselected; + } + // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { - GameManager.StateChanged += HandleStateChange; - if (isEntrance) { SetPropertiesOfEntrance(); } @@ -44,10 +55,6 @@ public class Room : MonoBehaviour } } - private void OnEnable() { - - } - public void SetRoomExplored() { _isExplored = true; UnhighlightRoomAsOption(); @@ -131,6 +138,7 @@ public class Room : MonoBehaviour } private void HandleStateChange(GameState state) { + UnhighlightRoomAsOption(); switch (state) { case GameState.PickDiceOne: @@ -144,4 +152,15 @@ public class Room : MonoBehaviour break; } } + + private void HandleDiceSelected(DicePair pair) { + if (TryUnlock(pair) && IsValidRoomToExplore()) + { + HighlightRoomAsOption(); + } + } + + private void HandDiceUnselected() { + UnhighlightRoomAsOption(); + } }