170 lines
4.8 KiB
C#
170 lines
4.8 KiB
C#
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<GameState> StateChanged;
|
|
|
|
// 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<Room>().ValidRoomClicked += ValidRoomClicked;
|
|
}
|
|
foreach (Transform diceTransform in diceRoller.dice.transform)
|
|
{
|
|
diceTransform.gameObject.GetComponent<Die>().DieSelected += HandleDieSelected;
|
|
diceTransform.gameObject.GetComponent<Die>().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);
|
|
HighLightValidRoomsWithNumber(_dicePairOne);
|
|
}
|
|
break;
|
|
case GameState.PickDiceTwo:
|
|
_dicePairTwo.SelectDie(die);
|
|
if (_dicePairTwo.AreBothDiceSelected())
|
|
{
|
|
ChangeState(GameState.PickRoomTwo);
|
|
HighLightValidRoomsWithNumber(_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);
|
|
UnhighLightRoomsAsOptions();
|
|
break;
|
|
case GameState.PickDiceTwo:
|
|
case GameState.PickRoomTwo:
|
|
if (state == GameState.PickRoomTwo)
|
|
{
|
|
ChangeState(GameState.PickDiceTwo);
|
|
}
|
|
_dicePairTwo.UnselectDie(die);
|
|
UnhighLightRoomsAsOptions();
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void PassTurn() {
|
|
player.TakeDamage(1);
|
|
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)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|