PuzzleGame/PuzzleGameProject/Assets/Scripts/Room.cs
2025-01-20 14:59:33 +01:00

78 lines
2.1 KiB
C#

using System;
using System.Collections;
using TMPro;
using UnityEngine;
using System.Collections.Generic;
public class Room : MonoBehaviour
{
public int number;
public GameObject numberTextObject;
public List<GameObject> adjacentRooms;
public bool isEntrance;
public event EventHandler<Room> ValidRoomClicked;
bool _isExplored = false;
// 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>();
numberText.SetText(number.ToString());
}
public void SetRoomExplored() {
_isExplored = true;
gameObject.GetComponent<SpriteRenderer>().color =
ColorHelper.AddColorTint(gameObject.GetComponent<SpriteRenderer>().color, Color.grey, 0.5f);
}
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.
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;
}
}