Fixed bug where highlighted room would not unhighlight and refactored room highlighting to be habdled by rooms.

This commit is contained in:
Maxwell Dodd 2025-01-27 14:41:40 +01:00
parent bcfd42147b
commit 6a309804ad
2 changed files with 38 additions and 37 deletions

View file

@ -25,6 +25,8 @@ public class GameManager : MonoBehaviour
private DicePair _dicePairOne = new(); private DicePair _dicePairOne = new();
private DicePair _dicePairTwo = new(); private DicePair _dicePairTwo = new();
public static event Action<GameState> StateChanged; public static event Action<GameState> StateChanged;
public static event Action<DicePair> DiceSelected;
public static event Action DiceUnselected;
// Start is called once before the first execution of Update after the MonoBehaviour is created // Start is called once before the first execution of Update after the MonoBehaviour is created
void Start() void Start()
@ -87,7 +89,7 @@ public class GameManager : MonoBehaviour
if (_dicePairOne.AreBothDiceSelected()) if (_dicePairOne.AreBothDiceSelected())
{ {
ChangeState(GameState.PickRoomOne); ChangeState(GameState.PickRoomOne);
HighLightValidRoomsWithNumber(_dicePairOne); DiceSelected?.Invoke(_dicePairOne);
} }
break; break;
case GameState.PickDiceTwo: case GameState.PickDiceTwo:
@ -95,7 +97,7 @@ public class GameManager : MonoBehaviour
if (_dicePairTwo.AreBothDiceSelected()) if (_dicePairTwo.AreBothDiceSelected())
{ {
ChangeState(GameState.PickRoomTwo); ChangeState(GameState.PickRoomTwo);
HighLightValidRoomsWithNumber(_dicePairTwo); DiceSelected?.Invoke(_dicePairTwo);
} }
break; break;
default: default:
@ -113,7 +115,7 @@ public class GameManager : MonoBehaviour
ChangeState(GameState.PickDiceOne); ChangeState(GameState.PickDiceOne);
} }
_dicePairOne.UnselectDie(die); _dicePairOne.UnselectDie(die);
UnhighLightRoomsAsOptions(); DiceUnselected?.Invoke();
break; break;
case GameState.PickDiceTwo: case GameState.PickDiceTwo:
case GameState.PickRoomTwo: case GameState.PickRoomTwo:
@ -122,7 +124,7 @@ public class GameManager : MonoBehaviour
ChangeState(GameState.PickDiceTwo); ChangeState(GameState.PickDiceTwo);
} }
_dicePairTwo.UnselectDie(die); _dicePairTwo.UnselectDie(die);
UnhighLightRoomsAsOptions(); DiceUnselected?.Invoke();
break; break;
} }
} }
@ -132,26 +134,6 @@ public class GameManager : MonoBehaviour
StartNewTurn(); StartNewTurn();
} }
void HighLightValidRoomsWithNumber(DicePair pair)
{
foreach (Transform roomTransform in rooms.transform)
{
Room room = roomTransform.gameObject.GetComponent<Room>();
if (room.TryUnlock(pair) && room.IsValidRoomToExplore())
{
room.HighlightRoomAsOption();
}
}
}
void UnhighLightRoomsAsOptions()
{
foreach (Transform roomTransform in rooms.transform)
{
roomTransform.gameObject.GetComponent<Room>().UnhighlightRoomAsOption();
}
}
private void DiceRolled(object sender, EventArgs e) private void DiceRolled(object sender, EventArgs e)
{ {
if (state == GameState.RollDice) if (state == GameState.RollDice)

View file

@ -3,11 +3,12 @@ using System.Collections;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using System.Collections.Generic; using System.Collections.Generic;
using Unity.VisualScripting;
enum KeyType internal enum KeyType
{ {
Number, Number,
MatchinDice MatchingDice
} }
public class Room : MonoBehaviour public class Room : MonoBehaviour
@ -23,10 +24,20 @@ public class Room : MonoBehaviour
private bool _isClickable = true; private bool _isClickable = true;
private Color _roomNumberOriginalColor; 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 // Start is called once before the first execution of Update after the MonoBehaviour is created
void Start() { void Start() {
GameManager.StateChanged += HandleStateChange;
if (isEntrance) { if (isEntrance) {
SetPropertiesOfEntrance(); SetPropertiesOfEntrance();
} }
@ -38,16 +49,12 @@ public class Room : MonoBehaviour
{ {
numberText.SetText(number.ToString()); numberText.SetText(number.ToString());
} }
else if (keyType == KeyType.MatchinDice) else if (keyType == KeyType.MatchingDice)
{ {
numberText.SetText("="); numberText.SetText("=");
} }
} }
private void OnEnable() {
}
public void SetRoomExplored() { public void SetRoomExplored() {
_isExplored = true; _isExplored = true;
UnhighlightRoomAsOption(); UnhighlightRoomAsOption();
@ -55,12 +62,12 @@ public class Room : MonoBehaviour
ColorHelper.AddColorTint(gameObject.GetComponent<SpriteRenderer>().color, Color.grey, 0.5f); ColorHelper.AddColorTint(gameObject.GetComponent<SpriteRenderer>().color, Color.grey, 0.5f);
} }
public void HighlightRoomAsOption() private void HighlightRoomAsOption()
{ {
gameObject.GetComponentInChildren<TextMeshProUGUI>().color = Color.blue; gameObject.GetComponentInChildren<TextMeshProUGUI>().color = Color.blue;
} }
public void UnhighlightRoomAsOption() private void UnhighlightRoomAsOption()
{ {
gameObject.GetComponentInChildren<TextMeshProUGUI>().color = _roomNumberOriginalColor; gameObject.GetComponentInChildren<TextMeshProUGUI>().color = _roomNumberOriginalColor;
} }
@ -82,7 +89,7 @@ public class Room : MonoBehaviour
} }
// Check if the room is valid to be explored. If so trigger the event. // Check if the room is valid to be explored. If so trigger the event.
public bool IsValidRoomToExplore() { private bool IsValidRoomToExplore() {
if (_isExplored) { if (_isExplored) {
return false; return false;
} }
@ -118,7 +125,7 @@ public class Room : MonoBehaviour
return true; return true;
} }
return false; return false;
case KeyType.MatchinDice: case KeyType.MatchingDice:
if (pair.DoResultsMatch()) if (pair.DoResultsMatch())
{ {
return true; return true;
@ -131,6 +138,7 @@ public class Room : MonoBehaviour
} }
private void HandleStateChange(GameState state) { private void HandleStateChange(GameState state) {
UnhighlightRoomAsOption();
switch (state) switch (state)
{ {
case GameState.PickDiceOne: case GameState.PickDiceOne:
@ -144,4 +152,15 @@ public class Room : MonoBehaviour
break; break;
} }
} }
private void HandleDiceSelected(DicePair pair) {
if (TryUnlock(pair) && IsValidRoomToExplore())
{
HighlightRoomAsOption();
}
}
private void HandDiceUnselected() {
UnhighlightRoomAsOption();
}
} }