PuzzleGame/PuzzleGameProject/Assets/Scripts/UI/UIManager.cs

167 lines
7.6 KiB
C#

using System;
using System.Collections.Generic;
using Abilities;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UIElements;
namespace UI
{
public class UIManager : MonoBehaviour
{
[SerializeField] private GameObject canvas;
[FormerlySerializedAs("hud")] [SerializeField] private InGameHUDController hudController;
[SerializeField] private GameObject chestPopUpControllerGO;
[SerializeField] private GameObject endOfGamePupUpGO;
[SerializeField] private ChestPopUpController chestPopUpController;
[SerializeField] private GameObject diceGO;
[SerializeField] private DiceRoller diceRoller;
[SerializeField] private PassManager passManager;
[SerializeField] private StartingBlackDieAbility startingBlackDieAbility;
[SerializeField] private BlackDieAbility blackDieAbility;
[SerializeField] private ChestRewardSelection chestRewardSelection;
[SerializeField] private TorchAbility torchAbility;
[SerializeField] private ArmorAbility armorAbility;
[SerializeField] private Player player;
private Dictionary<Die, Button> _dieToButtonMapping = new Dictionary<Die, Button>();
private void OnEnable()
{
chestPopUpController = chestPopUpControllerGO.GetComponent<ChestPopUpController>();
RoomRewards.ChestRewarded += HandleChestRewarded;
GameManager.EndOfGame += HandleEndOfGame;
chestPopUpController.BlackDieSelected += chestRewardSelection.HandleBlackDiceSelected;
chestPopUpController.KeySelected += chestRewardSelection.HandleTorchSelected;
chestPopUpController.ArmorAndDiamondSelected += chestRewardSelection.HandleDiamondAndLifeSelected;
hudController.RollClicked += diceRoller.RollDice;
hudController.PassClicked += passManager.OnPassClicked;
diceRoller.Enabled += () => hudController.SetDiceRollerEnabled(true);
diceRoller.Disabled += () => hudController.SetDiceRollerEnabled(false);
startingBlackDieAbility.MarkUseUsedEvent += hudController.MarkStartingBlackDieAbilityUsed;
startingBlackDieAbility.UnmarkUseUsedEvent += hudController.UnmarkStartingBlackDieAbilityUsed;
hudController.StartingBlackDieAbilityClicked += startingBlackDieAbility.UseClicked;
blackDieAbility.MarkUseUsedEvent += hudController.MarkBlackDieAbilityUsed;
blackDieAbility.UnmarkUseUsedEvent += hudController.UnmarkStartingBlackDieAbilityUsed;
blackDieAbility.AbilityGained += hudController.SetBlackDieAbilityEnabled;
hudController.BlackDieAbilityClicked += blackDieAbility.UseClicked;
chestRewardSelection.ArmorAndDiamondNoLongerAvailable += chestPopUpController.DisableArmorAndDiamond;
chestRewardSelection.TorchNoLongerAvailable += chestPopUpController.DisableKey;
chestRewardSelection.BlackDieNoLongerAvailable += chestPopUpController.DisableBlackDie;
torchAbility.AbilityGained += hudController.SetKeyAbilityEnabled;
TorchAbility.TorchAbilityUsed += hudController.MarkKeyAbilityUsed;
hudController.KeyAbilityClicked += torchAbility.UseClicked;
player.DiamondCountUpdated += hudController.SetDiamondCount;
player.HealthUpdated += hudController.SetHealth;
armorAbility.ArmorAbilityGained += hudController.SetArmorAbilityEnabled;
armorAbility.MarkAbilityUsed += hudController.MarkArmorAbilityUsed;
}
private void OnDisable()
{
RoomRewards.ChestRewarded -= HandleChestRewarded;
GameManager.EndOfGame -= HandleEndOfGame;
chestPopUpController.BlackDieSelected -= chestRewardSelection.HandleBlackDiceSelected;
chestPopUpController.KeySelected -= chestRewardSelection.HandleTorchSelected;
chestPopUpController.ArmorAndDiamondSelected -= chestRewardSelection.HandleDiamondAndLifeSelected;
hudController.RollClicked -= diceRoller.RollDice;
hudController.PassClicked -= passManager.OnPassClicked;
diceRoller.Enabled -= () => hudController.SetDiceRollerEnabled(true);
diceRoller.Disabled -= () => hudController.SetDiceRollerEnabled(false);
startingBlackDieAbility.MarkUseUsedEvent -= hudController.MarkStartingBlackDieAbilityUsed;
startingBlackDieAbility.UnmarkUseUsedEvent -= hudController.UnmarkStartingBlackDieAbilityUsed;
hudController.StartingBlackDieAbilityClicked -= startingBlackDieAbility.UseClicked;
blackDieAbility.MarkUseUsedEvent -= hudController.MarkBlackDieAbilityUsed;
blackDieAbility.UnmarkUseUsedEvent -= hudController.UnmarkStartingBlackDieAbilityUsed;
blackDieAbility.AbilityGained -= hudController.SetBlackDieAbilityEnabled;
hudController.BlackDieAbilityClicked -= blackDieAbility.UseClicked;
chestRewardSelection.ArmorAndDiamondNoLongerAvailable -= chestPopUpController.DisableArmorAndDiamond;
chestRewardSelection.TorchNoLongerAvailable -= chestPopUpController.DisableKey;
chestRewardSelection.BlackDieNoLongerAvailable -= chestPopUpController.DisableBlackDie;
torchAbility.AbilityGained -= hudController.SetKeyAbilityEnabled;
TorchAbility.TorchAbilityUsed -= hudController.MarkKeyAbilityUsed;
hudController.KeyAbilityClicked -= torchAbility.UseClicked;
player.DiamondCountUpdated -= hudController.SetDiamondCount;
player.HealthUpdated -= hudController.SetHealth;
armorAbility.ArmorAbilityGained -= hudController.SetArmorAbilityEnabled;
armorAbility.MarkAbilityUsed -= hudController.MarkArmorAbilityUsed;
}
private void Start()
{
int count = 0;
foreach (Transform dieTransform in diceGO.transform)
{
Die die = dieTransform.gameObject.GetComponent<Die>();
if (die.color == DiceColor.Black)
{
_dieToButtonMapping[die] = hudController.BlackDieButton;
}
else
{
_dieToButtonMapping[die] = hudController.WhiteDiceButtons[count];
count++;
}
}
foreach (var kvp in _dieToButtonMapping)
{
Button button = kvp.Value;
Die die = kvp.Key;
button.clicked += die.DiePressed;
die.DieRollResult += result => button.text = result.ToString();
die.DieSelectedButPairNotCompleteEvent += () => hudController.SetDieSelectedButNotComplete(button);
die.DieSelectedAndPairCompleteEvent += () => hudController.SetDieSelectedAndPairComplete(button);
die.DieReset += () => hudController.RestDie(button);
}
}
private void BlackDieAbilityGained()
{
hudController.SetBlackDieSelected(true);
}
private void TorchAbilityGained()
{
hudController.SetTorchSelected(true);
}
private void HealthAndDiamondGained()
{
hudController.SetHealthAndDiamondSelected(true);
}
private void HandleChestRewarded()
{
chestPopUpControllerGO.SetActive(true);
}
private void HandleEndOfGame(object sender, EndOfGameEventArgs args)
{
endOfGamePupUpGO.SetActive(true);
endOfGamePupUpGO.GetComponent<EndGameController>().PopulateEndOfGamePopUp(args);
}
}
}