PuzzleGame/PuzzleGameProject/Assets/Scripts/GameManager.cs

162 lines
4.5 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.TryUnlock(_dicePairOne):
case GameState.PickRoomTwo when !room.TryUnlock(_dicePairTwo):
return;
}
room.SetRoomExplored();
if (state == GameState.PickRoomOne)
{
state = GameState.PickDiceTwo;
}
else
{
StartNewTurn();
}
}
void DieClicked(object sender, Die die)
{
switch (state)
{
case GameState.PickDiceOne:
{
if (!_dicePairOne.ContainsDie(die))
{
_dicePairOne.SelectDie(die);
if (_dicePairOne.AreBothDiceSelected())
{
state = 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())
{
state = GameState.PickRoomTwo;
HighLightValidRoomsWithNumber(_dicePairTwo);
}
}
else if (_dicePairTwo.ContainsDie(die))
{
_dicePairTwo.UnselectDie(die);
}
break;
}
case GameState.PickRoomOne:
if (_dicePairOne.ContainsDie(die))
{
_dicePairOne.UnselectDie(die);
UnhighLightRoomsAsOptions();
state = GameState.PickDiceOne;
}
break;
case GameState.PickRoomTwo:
if (_dicePairTwo.ContainsDie(die))
{
_dicePairTwo.UnselectDie(die);
UnhighLightRoomsAsOptions();
state = GameState.PickDiceTwo;
}
break;
}
}
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();
state = GameState.PickDiceOne;
}
}
}