Implemented the functionality for monster rooms.
This commit is contained in:
parent
6a309804ad
commit
aa49815fb9
16 changed files with 1182 additions and 71 deletions
|
|
@ -1,166 +0,0 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using Unity.VisualScripting;
|
||||
|
||||
internal enum KeyType
|
||||
{
|
||||
Number,
|
||||
MatchingDice
|
||||
}
|
||||
|
||||
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 bool _isClickable = true;
|
||||
private Color _roomNumberOriginalColor;
|
||||
|
||||
private void OnEnable() {
|
||||
GameManager.StateChanged += HandleStateChange;
|
||||
GameManager.DiceSelected += HandleDiceSelected;
|
||||
GameManager.DiceUnselected += HandDiceUnselected;
|
||||
}
|
||||
|
||||
private void OnDisable() {
|
||||
GameManager.StateChanged -= HandleStateChange;
|
||||
GameManager.DiceSelected -= HandleDiceSelected;
|
||||
GameManager.DiceUnselected -= HandDiceUnselected;
|
||||
}
|
||||
|
||||
// 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>();
|
||||
_roomNumberOriginalColor = gameObject.GetComponentInChildren<TextMeshProUGUI>().color;
|
||||
|
||||
if (keyType == KeyType.Number)
|
||||
{
|
||||
numberText.SetText(number.ToString());
|
||||
}
|
||||
else if (keyType == KeyType.MatchingDice)
|
||||
{
|
||||
numberText.SetText("=");
|
||||
}
|
||||
}
|
||||
|
||||
public void SetRoomExplored() {
|
||||
_isExplored = true;
|
||||
UnhighlightRoomAsOption();
|
||||
gameObject.GetComponent<SpriteRenderer>().color =
|
||||
ColorHelper.AddColorTint(gameObject.GetComponent<SpriteRenderer>().color, Color.grey, 0.5f);
|
||||
}
|
||||
|
||||
private void HighlightRoomAsOption()
|
||||
{
|
||||
gameObject.GetComponentInChildren<TextMeshProUGUI>().color = Color.blue;
|
||||
}
|
||||
|
||||
private void UnhighlightRoomAsOption()
|
||||
{
|
||||
gameObject.GetComponentInChildren<TextMeshProUGUI>().color = _roomNumberOriginalColor;
|
||||
}
|
||||
|
||||
void SetPropertiesOfEntrance() {
|
||||
gameObject.GetComponent<SpriteRenderer>().color = Color.green;
|
||||
isEntrance = true;
|
||||
}
|
||||
|
||||
void OnMouseDown() {
|
||||
if (!_isClickable) return;
|
||||
if (IsValidRoomToExplore()) {
|
||||
OnValidRoomClicked();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnValidRoomClicked() {
|
||||
ValidRoomClicked?.Invoke(this, this);
|
||||
}
|
||||
|
||||
// Check if the room is valid to be explored. If so trigger the event.
|
||||
private 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.MatchingDice:
|
||||
if (pair.DoResultsMatch())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void HandleStateChange(GameState state) {
|
||||
UnhighlightRoomAsOption();
|
||||
switch (state)
|
||||
{
|
||||
case GameState.PickDiceOne:
|
||||
case GameState.PickDiceTwo:
|
||||
case GameState.RollDice:
|
||||
_isClickable = false;
|
||||
break;
|
||||
case GameState.PickRoomTwo:
|
||||
case GameState.PickRoomOne:
|
||||
_isClickable = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleDiceSelected(DicePair pair) {
|
||||
if (TryUnlock(pair) && IsValidRoomToExplore())
|
||||
{
|
||||
HighlightRoomAsOption();
|
||||
}
|
||||
}
|
||||
|
||||
private void HandDiceUnselected() {
|
||||
UnhighlightRoomAsOption();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue