PuzzleGame/PuzzleGameProject/Assets/Scripts/GameManager.cs
2025-01-21 14:41:43 +01:00

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.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)
{
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);
}
}
break;
}
case GameState.PickDiceTwo:
{
if (!_dicePairTwo.IsDieInPair(die) && !_dicePairOne.IsDieInPair(die))
{
_dicePairTwo.SelectDie(die);
if (_dicePairTwo.AreBothDiceSelected())
{
state = GameState.PickRoomTwo;
HighLightValidRoomsWithNumber(_dicePairTwo);
}
}
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();
}
}
}
private void DiceRolled(object sender, EventArgs e)
{
if (state == GameState.RollDice)
{
diceRoller.Disable();
state = GameState.PickDiceOne;
}
}
}