using System; using UnityEngine; using Object = UnityEngine.Object; using System.Collections.Generic; using Abilities; using TMPro; using Unity.VisualScripting; public enum GameState { RollDice, PickRoomOne, PickRoomTwo, PickDiceOne, PickDiceTwo, } public class GameManager : MonoBehaviour { [SerializeField] public GameState state; [SerializeField] private GameObject rooms; [SerializeField] private DiceRoller diceRoller; [SerializeField] private PassManager passManager; [SerializeField] private Player player; 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() { diceRoller.diceRolled += DiceRolled; passManager.PassRequested += PassRequested; foreach (Transform roomTransform in rooms.transform) { roomTransform.gameObject.GetComponent().ValidRoomClicked += ValidRoomClicked; } foreach (Transform diceTransform in diceRoller.dice.transform) { diceTransform.gameObject.GetComponent().DieSelected += HandleDieSelected; diceTransform.gameObject.GetComponent().DieUnselected += HandleDieUnselected; } StartNewTurn(); } private void ChangeState(GameState stateToChangeTo) { if (state != stateToChangeTo) { state = stateToChangeTo; StateChanged?.Invoke(state); } } private void StartNewTurn() { ChangeState(GameState.RollDice); diceRoller.Enable(); diceRoller.ResetDice(); _dicePairOne = new DicePair(); _dicePairTwo = new DicePair(); } void ValidRoomClicked(object sender, Room room) { switch (state) { case GameState.PickRoomOne when !room.TryUnlock(_dicePairOne): case GameState.PickRoomTwo when !room.TryUnlock(_dicePairTwo): return; } room.SetRoomExplored(); if (state == GameState.PickRoomOne) { ChangeState(GameState.PickDiceTwo); } else { StartNewTurn(); } } private void HandleDieSelected(Die die) { switch (state) { case GameState.PickDiceOne: _dicePairOne.SelectDie(die); if (_dicePairOne.AreBothDiceSelected()) { ChangeState(GameState.PickRoomOne); DiceSelected?.Invoke(_dicePairOne); } break; case GameState.PickDiceTwo: _dicePairTwo.SelectDie(die); if (_dicePairTwo.AreBothDiceSelected()) { ChangeState(GameState.PickRoomTwo); DiceSelected?.Invoke(_dicePairTwo); } break; default: return; } } private void HandleDieUnselected(Die die) { switch (state) { case GameState.PickDiceOne: case GameState.PickRoomOne: if (state == GameState.PickRoomOne) { ChangeState(GameState.PickDiceOne); } _dicePairOne.UnselectDie(die); DiceUnselected?.Invoke(); break; case GameState.PickDiceTwo: case GameState.PickRoomTwo: if (state == GameState.PickRoomTwo) { ChangeState(GameState.PickDiceTwo); } _dicePairTwo.UnselectDie(die); DiceUnselected?.Invoke(); break; } } private void PassTurn() { player.TakeDamage(1); StartNewTurn(); } private void DiceRolled(object sender, EventArgs e) { if (state == GameState.RollDice) { diceRoller.Disable(); ChangeState(GameState.PickDiceOne); } } private void PassRequested(object sender, EventArgs e) { if (state is GameState.PickDiceOne or GameState.PickDiceTwo) { PassTurn(); } } }