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

129 lines
3.3 KiB
C#

using System;
using System.Collections;
using TMPro;
using UnityEngine;
using System.Collections.Generic;
enum KeyType
{
Number,
MatchinDice
}
public class Room : MonoBehaviour
{
[SerializeField] private GameObject numberTextObject;
[SerializeField] private List<GameObject> adjacentRooms;
[SerializeField] private bool isEntrance;
[SerializeField] private KeyType keyType;
[SerializeField] private int number;
public event EventHandler<Room> ValidRoomClicked;
bool _isExplored = false;
private Color _roomNumberOriginalColor;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start() {
if (isEntrance) {
SetPropertiesOfEntrance();
}
TextMeshProUGUI numberText = numberTextObject.GetComponent<TextMeshProUGUI>();
if (keyType == KeyType.Number)
{
numberText.SetText(number.ToString());
}
else if (keyType == KeyType.MatchinDice)
{
numberText.SetText("=");
}
}
public void SetRoomExplored() {
_isExplored = true;
UnhighlightRoomAsOption();
gameObject.GetComponent<SpriteRenderer>().color =
ColorHelper.AddColorTint(gameObject.GetComponent<SpriteRenderer>().color, Color.grey, 0.5f);
}
public void HighlightRoomAsOption()
{
_roomNumberOriginalColor = gameObject.GetComponentInChildren<TextMeshProUGUI>().color;
gameObject.GetComponentInChildren<TextMeshProUGUI>().color = Color.blue;
}
public void UnhighlightRoomAsOption()
{
gameObject.GetComponentInChildren<TextMeshProUGUI>().color = _roomNumberOriginalColor;
}
void SetPropertiesOfEntrance() {
gameObject.GetComponent<SpriteRenderer>().color = Color.green;
isEntrance = true;
}
IEnumerator OnMouseDown()
{
if (IsValidRoomToExplore()) {
OnValidRoomClicked();
}
yield return null;
}
protected virtual void OnValidRoomClicked() {
ValidRoomClicked?.Invoke(this, this);
}
// Check if the room is valid to be explored. If so trigger the event.
public bool IsValidRoomToExplore() {
if (_isExplored) {
return false;
}
if (isEntrance) {
// All entrance rooms are valid to be explored.
return true;
}
else if (HasExploredAdjacentRooms()) {
// Otherwise the room must have an adjacent room explored.
return true;
}
return false;
}
bool HasExploredAdjacentRooms() {
foreach (GameObject adjacentRoom in adjacentRooms) {
if (adjacentRoom.GetComponent<Room>()._isExplored)
{
return true;
}
}
return false;
}
public bool TryUnlock(DicePair pair)
{
switch (keyType)
{
case KeyType.Number:
if (number == pair.Sum())
{
return true;
}
return false;
case KeyType.MatchinDice:
if (pair.DoResultsMatch())
{
return true;
}
return false;
}
return false;
}
}