Refactored clicking on dice and rooms so that logic is moved out of the game manager

This commit is contained in:
Max Dodd 2025-01-25 17:18:01 +01:00
parent ede5ef1533
commit e7ea8302ad
8 changed files with 150 additions and 67 deletions

View file

@ -1293,6 +1293,7 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
passButton: {fileID: 895359725} passButton: {fileID: 895359725}
gameManager: {fileID: 126339304}
--- !u!114 &895359725 --- !u!114 &895359725
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1332,7 +1333,7 @@ MonoBehaviour:
m_PressedTrigger: Pressed m_PressedTrigger: Pressed
m_SelectedTrigger: Selected m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled m_DisabledTrigger: Disabled
m_Interactable: 1 m_Interactable: 0
m_TargetGraphic: {fileID: 895359726} m_TargetGraphic: {fileID: 895359726}
m_OnClick: m_OnClick:
m_PersistentCalls: m_PersistentCalls:
@ -1648,6 +1649,10 @@ PrefabInstance:
serializedVersion: 3 serializedVersion: 3
m_TransformParent: {fileID: 1820284206} m_TransformParent: {fileID: 1820284206}
m_Modifications: m_Modifications:
- target: {fileID: 1770509300873928573, guid: 92d87e25cc40b3e448e62e8ba0328315, type: 3}
propertyPath: m_Enabled
value: 1
objectReference: {fileID: 0}
- target: {fileID: 2435349004046080434, guid: 92d87e25cc40b3e448e62e8ba0328315, type: 3} - target: {fileID: 2435349004046080434, guid: 92d87e25cc40b3e448e62e8ba0328315, type: 3}
propertyPath: m_Name propertyPath: m_Name
value: Room (1) value: Room (1)
@ -1660,6 +1665,10 @@ PrefabInstance:
propertyPath: keyType propertyPath: keyType
value: 1 value: 1
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 2727030095425591755, guid: 92d87e25cc40b3e448e62e8ba0328315, type: 3}
propertyPath: m_Enabled
value: 1
objectReference: {fileID: 0}
- target: {fileID: 2727030095425591755, guid: 92d87e25cc40b3e448e62e8ba0328315, type: 3} - target: {fileID: 2727030095425591755, guid: 92d87e25cc40b3e448e62e8ba0328315, type: 3}
propertyPath: isEntrance propertyPath: isEntrance
value: 1 value: 1
@ -1684,6 +1693,14 @@ PrefabInstance:
propertyPath: 'unexploredAdjacentRooms.Array.data[0]' propertyPath: 'unexploredAdjacentRooms.Array.data[0]'
value: value:
objectReference: {fileID: 794442986} objectReference: {fileID: 794442986}
- target: {fileID: 4019891345885281529, guid: 92d87e25cc40b3e448e62e8ba0328315, type: 3}
propertyPath: m_Enabled
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4019891345885281529, guid: 92d87e25cc40b3e448e62e8ba0328315, type: 3}
propertyPath: m_IsTrigger
value: 1
objectReference: {fileID: 0}
- target: {fileID: 9187907134033443523, guid: 92d87e25cc40b3e448e62e8ba0328315, type: 3} - target: {fileID: 9187907134033443523, guid: 92d87e25cc40b3e448e62e8ba0328315, type: 3}
propertyPath: m_LocalPosition.x propertyPath: m_LocalPosition.x
value: -3.647156 value: -3.647156

View file

@ -6,6 +6,8 @@ public class DicePair
public void SelectDie(Die die) public void SelectDie(Die die)
{ {
if (ContainsDie(die)) return;
if (_pair.Item1 == null && _pair.Item2 == null) if (_pair.Item1 == null && _pair.Item2 == null)
{ {
_pair.Item1 = die; _pair.Item1 = die;

View file

@ -15,11 +15,14 @@ public class Die : MonoBehaviour
private int _result = 0; private int _result = 0;
[SerializeField] private Button dieButton; // assign in the editor [SerializeField] private Button dieButton; // assign in the editor
private Color originalColor; private Color originalColor;
public event EventHandler<Die> DieClicked; private bool _isClickable = false;
private bool _selected = false;
public event Action<Die> DieSelected;
public event Action<Die> DieUnselected;
private void Start() private void Start() {
{ GameManager.Instance.StateChanged += HandleStateChange;
dieButton.onClick.AddListener(() => { DiePressed();}); dieButton.onClick.AddListener(DiePressed);
originalColor = gameObject.GetComponent<Image>().color; originalColor = gameObject.GetComponent<Image>().color;
} }
@ -34,8 +37,8 @@ public class Die : MonoBehaviour
return _result; return _result;
} }
public void ResetDie() public void ResetDie() {
{ _selected = false;
gameObject.GetComponent<Image>().color = originalColor; gameObject.GetComponent<Image>().color = originalColor;
gameObject.GetComponent<Outline>().enabled = false; gameObject.GetComponent<Outline>().enabled = false;
} }
@ -56,18 +59,46 @@ public class Die : MonoBehaviour
private void DiePressed() private void DiePressed()
{ {
DieClicked?.Invoke(this, this); if (!_isClickable) return;
if (_selected)
{
DieUnselected?.Invoke(this);
}
else
{
DieSelected?.Invoke(this);
}
} }
private void DieSelectedButPairNotComplete() private void DieSelectedButPairNotComplete()
{ {
_selected = true;
gameObject.GetComponent<Outline>().enabled = true; gameObject.GetComponent<Outline>().enabled = true;
} }
private void DieSelectedAndPairComplete() private void DieSelectedAndPairComplete() {
{ _selected = true;
gameObject.GetComponent<Outline>().enabled = false; gameObject.GetComponent<Outline>().enabled = false;
gameObject.GetComponent<Image>().color = gameObject.GetComponent<Image>().color =
ColorHelper.AddColorTint(gameObject.GetComponent<Image>().color, ColorHelper.OkayGreen, 0.5f); ColorHelper.AddColorTint(gameObject.GetComponent<Image>().color, ColorHelper.OkayGreen, 0.5f);
} }
private void HandleStateChange(GameState state) {
switch (state)
{
case GameState.PickDiceOne:
case GameState.PickDiceTwo:
case GameState.PickRoomOne:
case GameState.PickRoomTwo:
_isClickable = true;
break;
case GameState.RollDice:
_isClickable = false;
break;
default:
_isClickable = false;
break;
}
}
} }

View file

@ -3,6 +3,7 @@ using UnityEngine;
using Object = UnityEngine.Object; using Object = UnityEngine.Object;
using System.Collections.Generic; using System.Collections.Generic;
using TMPro; using TMPro;
using Unity.VisualScripting;
public enum GameState public enum GameState
{ {
@ -14,6 +15,8 @@ public enum GameState
} }
public class GameManager : MonoBehaviour public class GameManager : MonoBehaviour
{ {
public static GameManager Instance { get; private set; }
[SerializeField] public GameState state; [SerializeField] public GameState state;
[SerializeField] private GameObject rooms; [SerializeField] private GameObject rooms;
[SerializeField] private DiceRoller diceRoller; [SerializeField] private DiceRoller diceRoller;
@ -22,7 +25,15 @@ public class GameManager : MonoBehaviour
private DicePair _dicePairOne = new(); private DicePair _dicePairOne = new();
private DicePair _dicePairTwo = new(); private DicePair _dicePairTwo = new();
public event EventHandler<GameState> StateChanged; public event Action<GameState> StateChanged;
private void Awake() {
if (Instance != null && Instance != this) {
Destroy(gameObject);
return;
}
Instance = this;
}
// 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()
@ -35,14 +46,18 @@ public class GameManager : MonoBehaviour
} }
foreach (Transform diceTransform in diceRoller.dice.transform) foreach (Transform diceTransform in diceRoller.dice.transform)
{ {
diceTransform.gameObject.GetComponent<Die>().DieClicked += DieClicked; diceTransform.gameObject.GetComponent<Die>().DieSelected += HandleDieSelected;
diceTransform.gameObject.GetComponent<Die>().DieUnselected += HandleDieUnselected;
} }
StartNewTurn(); StartNewTurn();
} }
private void ChangeState(GameState stateToChangeTo) { private void ChangeState(GameState stateToChangeTo) {
if (state != stateToChangeTo)
{
state = stateToChangeTo; state = stateToChangeTo;
StateChanged?.Invoke(this, state); StateChanged?.Invoke(state);
}
} }
private void StartNewTurn() private void StartNewTurn()
@ -55,11 +70,6 @@ public class GameManager : MonoBehaviour
} }
void ValidRoomClicked(object sender, Room room) { void ValidRoomClicked(object sender, Room room) {
if (state != GameState.PickRoomOne && state != GameState.PickRoomTwo)
{
return;
}
switch (state) switch (state)
{ {
case GameState.PickRoomOne when !room.TryUnlock(_dicePairOne): case GameState.PickRoomOne when !room.TryUnlock(_dicePairOne):
@ -78,63 +88,50 @@ public class GameManager : MonoBehaviour
} }
} }
void DieClicked(object sender, Die die) private void HandleDieSelected(Die die) {
{
switch (state) switch (state)
{ {
case GameState.PickDiceOne: case GameState.PickDiceOne:
{
if (!_dicePairOne.ContainsDie(die))
{
_dicePairOne.SelectDie(die); _dicePairOne.SelectDie(die);
if (_dicePairOne.AreBothDiceSelected()) if (_dicePairOne.AreBothDiceSelected())
{ {
ChangeState(GameState.PickRoomOne); ChangeState(GameState.PickRoomOne);
HighLightValidRoomsWithNumber(_dicePairOne); HighLightValidRoomsWithNumber(_dicePairOne);
} }
}
else
{
_dicePairOne.UnselectDie(die);
}
break; break;
}
case GameState.PickDiceTwo: case GameState.PickDiceTwo:
{
if (!_dicePairTwo.ContainsDie(die) && !_dicePairOne.ContainsDie(die))
{
_dicePairTwo.SelectDie(die); _dicePairTwo.SelectDie(die);
if (_dicePairTwo.AreBothDiceSelected()) if (_dicePairTwo.AreBothDiceSelected())
{ {
ChangeState(GameState.PickRoomTwo); ChangeState(GameState.PickRoomTwo);
HighLightValidRoomsWithNumber(_dicePairTwo); HighLightValidRoomsWithNumber(_dicePairTwo);
} }
break;
default:
return;
} }
else if (_dicePairTwo.ContainsDie(die))
{
_dicePairTwo.UnselectDie(die);
} }
break; private void HandleDieUnselected(Die die) {
} switch (state)
case GameState.PickRoomOne: {
if (_dicePairOne.ContainsDie(die)) case GameState.PickDiceOne:
case GameState.PickRoomOne:
if (state == GameState.PickRoomOne)
{ {
_dicePairOne.UnselectDie(die);
UnhighLightRoomsAsOptions();
ChangeState(GameState.PickDiceOne); ChangeState(GameState.PickDiceOne);
} }
break; _dicePairOne.UnselectDie(die);
case GameState.PickRoomTwo:
if (_dicePairTwo.ContainsDie(die))
{
_dicePairTwo.UnselectDie(die);
UnhighLightRoomsAsOptions(); UnhighLightRoomsAsOptions();
break;
case GameState.PickDiceTwo:
case GameState.PickRoomTwo:
if (state == GameState.PickRoomTwo)
{
ChangeState(GameState.PickDiceTwo); ChangeState(GameState.PickDiceTwo);
} }
_dicePairTwo.UnselectDie(die);
UnhighLightRoomsAsOptions();
break; break;
} }
} }

View file

@ -1,2 +1,11 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 85fe57d438ef1ec4fa8b36b9c17c11a4 guid: 85fe57d438ef1ec4fa8b36b9c17c11a4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 1
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -15,10 +15,10 @@ public class PassManager : MonoBehaviour
void Start() void Start()
{ {
passButton.onClick.AddListener(OnPassClicked); passButton.onClick.AddListener(OnPassClicked);
gameManager.StateChanged += OnGameStateChange; gameManager.StateChanged += HandleStateChange;
} }
private void OnGameStateChange(object sender, GameState state) { private void HandleStateChange(GameState state) {
if (state == GameState.RollDice) if (state == GameState.RollDice)
{ {
passButton.interactable = false; passButton.interactable = false;

View file

@ -1,2 +1,11 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 95487118287b4514fb9b9fb7018ae717 guid: 95487118287b4514fb9b9fb7018ae717
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -20,12 +20,12 @@ public class Room : MonoBehaviour
public event EventHandler<Room> ValidRoomClicked; public event EventHandler<Room> ValidRoomClicked;
bool _isExplored = false; bool _isExplored = false;
private bool _isClickable = true;
private Color _roomNumberOriginalColor; private Color _roomNumberOriginalColor;
// 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.Instance.StateChanged += HandleStateChange;
if (isEntrance) { if (isEntrance) {
SetPropertiesOfEntrance(); SetPropertiesOfEntrance();
@ -44,6 +44,10 @@ public class Room : MonoBehaviour
} }
} }
private void OnEnable() {
}
public void SetRoomExplored() { public void SetRoomExplored() {
_isExplored = true; _isExplored = true;
UnhighlightRoomAsOption(); UnhighlightRoomAsOption();
@ -66,12 +70,11 @@ public class Room : MonoBehaviour
isEntrance = true; isEntrance = true;
} }
IEnumerator OnMouseDown() void OnMouseDown() {
{ if (!_isClickable) return;
if (IsValidRoomToExplore()) { if (IsValidRoomToExplore()) {
OnValidRoomClicked(); OnValidRoomClicked();
} }
yield return null;
} }
protected virtual void OnValidRoomClicked() { protected virtual void OnValidRoomClicked() {
@ -126,4 +129,19 @@ public class Room : MonoBehaviour
return false; return false;
} }
private void HandleStateChange(GameState state) {
switch (state)
{
case GameState.PickDiceOne:
case GameState.PickDiceTwo:
case GameState.RollDice:
_isClickable = false;
break;
case GameState.PickRoomTwo:
case GameState.PickRoomOne:
_isClickable = true;
break;
}
}
} }