135 lines
3.6 KiB
C#
135 lines
3.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using Object = UnityEngine.Object;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
|
|
public enum GameState
|
|
{
|
|
RollDice,
|
|
PickRoomOne,
|
|
PickRoomTwo,
|
|
PickDiceOne,
|
|
PickDiceTwo,
|
|
}
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
[SerializeField] private GameState state;
|
|
[SerializeField] private GameObject rooms;
|
|
[SerializeField] private DiceRoller diceRoller;
|
|
|
|
private DicePair _dicePairOne = new DicePair();
|
|
private DicePair _dicePairTwo = new DicePair();
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
diceRoller.diceRolled += DiceRolled;
|
|
foreach (Transform roomTransform in rooms.transform)
|
|
{
|
|
roomTransform.gameObject.GetComponent<Room>().ValidRoomClicked += ValidRoomClicked;
|
|
}
|
|
foreach (Transform diceTransform in diceRoller.dice.transform)
|
|
{
|
|
diceTransform.gameObject.GetComponent<Die>().DieClicked += DieClicked;
|
|
}
|
|
StartNewTurn();
|
|
}
|
|
|
|
private void StartNewTurn()
|
|
{
|
|
state = GameState.RollDice;
|
|
diceRoller.Enable();
|
|
diceRoller.ResetDice();
|
|
_dicePairOne = new DicePair();
|
|
_dicePairTwo = new DicePair();
|
|
}
|
|
|
|
void ValidRoomClicked(object sender, Room room) {
|
|
if (state != GameState.PickRoomOne && state != GameState.PickRoomTwo)
|
|
{
|
|
return;
|
|
}
|
|
|
|
switch (state)
|
|
{
|
|
case GameState.PickRoomOne when room.number != _dicePairOne.Sum():
|
|
case GameState.PickRoomTwo when room.number != _dicePairTwo.Sum():
|
|
return;
|
|
}
|
|
|
|
room.SetRoomExplored();
|
|
if (state == GameState.PickRoomOne)
|
|
{
|
|
state = GameState.PickDiceTwo;
|
|
}
|
|
else
|
|
{
|
|
StartNewTurn();
|
|
}
|
|
|
|
}
|
|
|
|
void DieClicked(object sender, Die die)
|
|
{
|
|
if (state != GameState.PickDiceOne && state != GameState.PickDiceTwo)
|
|
{
|
|
return;
|
|
}
|
|
|
|
switch (state)
|
|
{
|
|
case GameState.PickDiceOne:
|
|
{
|
|
if (!_dicePairOne.IsDieInPair(die))
|
|
{
|
|
_dicePairOne.SelectDie(die);
|
|
|
|
if (_dicePairOne.AreBothDiceSelected())
|
|
{
|
|
state = GameState.PickRoomOne;
|
|
HighLightValidRoomsWithNumber(_dicePairOne.Sum());
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
case GameState.PickDiceTwo:
|
|
{
|
|
if (!_dicePairTwo.IsDieInPair(die))
|
|
{
|
|
_dicePairTwo.SelectDie(die);
|
|
|
|
if (_dicePairTwo.AreBothDiceSelected())
|
|
{
|
|
state = GameState.PickRoomTwo;
|
|
HighLightValidRoomsWithNumber(_dicePairTwo.Sum());
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void HighLightValidRoomsWithNumber(int number)
|
|
{
|
|
foreach (Transform roomTransform in rooms.transform)
|
|
{
|
|
Room room = roomTransform.gameObject.GetComponent<Room>();
|
|
if (room.number == number && room.IsValidRoomToExplore())
|
|
{
|
|
room.HighlightRoomAsOption();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DiceRolled(object sender, EventArgs e)
|
|
{
|
|
if (state == GameState.RollDice)
|
|
{
|
|
diceRoller.Disable();
|
|
state = GameState.PickDiceOne;
|
|
}
|
|
}
|
|
}
|