Refactored clicking on dice and rooms so that logic is moved out of the game manager
This commit is contained in:
parent
ede5ef1533
commit
e7ea8302ad
8 changed files with 150 additions and 67 deletions
|
|
@ -3,6 +3,7 @@ using UnityEngine;
|
|||
using Object = UnityEngine.Object;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using Unity.VisualScripting;
|
||||
|
||||
public enum GameState
|
||||
{
|
||||
|
|
@ -14,6 +15,8 @@ public enum GameState
|
|||
}
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
public static GameManager Instance { get; private set; }
|
||||
|
||||
[SerializeField] public GameState state;
|
||||
[SerializeField] private GameObject rooms;
|
||||
[SerializeField] private DiceRoller diceRoller;
|
||||
|
|
@ -22,8 +25,16 @@ public class GameManager : MonoBehaviour
|
|||
|
||||
private DicePair _dicePairOne = 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
|
||||
void Start()
|
||||
{
|
||||
|
|
@ -35,14 +46,18 @@ public class GameManager : MonoBehaviour
|
|||
}
|
||||
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();
|
||||
}
|
||||
|
||||
private void ChangeState(GameState stateToChangeTo) {
|
||||
state = stateToChangeTo;
|
||||
StateChanged?.Invoke(this, state);
|
||||
if (state != stateToChangeTo)
|
||||
{
|
||||
state = stateToChangeTo;
|
||||
StateChanged?.Invoke(state);
|
||||
}
|
||||
}
|
||||
|
||||
private void StartNewTurn()
|
||||
|
|
@ -55,11 +70,6 @@ public class GameManager : MonoBehaviour
|
|||
}
|
||||
|
||||
void ValidRoomClicked(object sender, Room room) {
|
||||
if (state != GameState.PickRoomOne && state != GameState.PickRoomTwo)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch (state)
|
||||
{
|
||||
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)
|
||||
{
|
||||
case GameState.PickDiceOne:
|
||||
{
|
||||
if (!_dicePairOne.ContainsDie(die))
|
||||
_dicePairOne.SelectDie(die);
|
||||
if (_dicePairOne.AreBothDiceSelected())
|
||||
{
|
||||
_dicePairOne.SelectDie(die);
|
||||
|
||||
if (_dicePairOne.AreBothDiceSelected())
|
||||
{
|
||||
ChangeState(GameState.PickRoomOne);
|
||||
HighLightValidRoomsWithNumber(_dicePairOne);
|
||||
}
|
||||
ChangeState(GameState.PickRoomOne);
|
||||
HighLightValidRoomsWithNumber(_dicePairOne);
|
||||
}
|
||||
else
|
||||
{
|
||||
_dicePairOne.UnselectDie(die);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case GameState.PickDiceTwo:
|
||||
{
|
||||
if (!_dicePairTwo.ContainsDie(die) && !_dicePairOne.ContainsDie(die))
|
||||
_dicePairTwo.SelectDie(die);
|
||||
if (_dicePairTwo.AreBothDiceSelected())
|
||||
{
|
||||
_dicePairTwo.SelectDie(die);
|
||||
|
||||
if (_dicePairTwo.AreBothDiceSelected())
|
||||
{
|
||||
ChangeState(GameState.PickRoomTwo);
|
||||
HighLightValidRoomsWithNumber(_dicePairTwo);
|
||||
}
|
||||
ChangeState(GameState.PickRoomTwo);
|
||||
HighLightValidRoomsWithNumber(_dicePairTwo);
|
||||
}
|
||||
else if (_dicePairTwo.ContainsDie(die))
|
||||
{
|
||||
_dicePairTwo.UnselectDie(die);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleDieUnselected(Die die) {
|
||||
switch (state)
|
||||
{
|
||||
case GameState.PickDiceOne:
|
||||
case GameState.PickRoomOne:
|
||||
if (_dicePairOne.ContainsDie(die))
|
||||
if (state == GameState.PickRoomOne)
|
||||
{
|
||||
_dicePairOne.UnselectDie(die);
|
||||
UnhighLightRoomsAsOptions();
|
||||
ChangeState(GameState.PickDiceOne);
|
||||
}
|
||||
_dicePairOne.UnselectDie(die);
|
||||
UnhighLightRoomsAsOptions();
|
||||
break;
|
||||
case GameState.PickDiceTwo:
|
||||
case GameState.PickRoomTwo:
|
||||
if (_dicePairTwo.ContainsDie(die))
|
||||
if (state == GameState.PickRoomTwo)
|
||||
{
|
||||
_dicePairTwo.UnselectDie(die);
|
||||
UnhighLightRoomsAsOptions();
|
||||
ChangeState(GameState.PickDiceTwo);
|
||||
}
|
||||
_dicePairTwo.UnselectDie(die);
|
||||
UnhighLightRoomsAsOptions();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue