Implemented basic room exploration logic.
This commit is contained in:
parent
cb7aff20dd
commit
4bd34a1335
13 changed files with 186 additions and 92 deletions
|
|
@ -1,3 +1,5 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
|
@ -8,13 +10,15 @@ public class Room : MonoBehaviour
|
|||
public GameObject numberTextObject;
|
||||
public List<GameObject> adjacentRooms;
|
||||
public bool isEntrance;
|
||||
private bool isExplored = false;
|
||||
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)
|
||||
{
|
||||
void Start() {
|
||||
|
||||
|
||||
if (isEntrance) {
|
||||
SetPropertiesOfEntrance();
|
||||
}
|
||||
|
||||
|
|
@ -22,15 +26,53 @@ public class Room : MonoBehaviour
|
|||
numberText.SetText(number.ToString());
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
public void SetRoomExplored() {
|
||||
_isExplored = true;
|
||||
gameObject.GetComponent<SpriteRenderer>().color =
|
||||
ColorUtility.AddGreyShade(gameObject.GetComponent<SpriteRenderer>().color, 0.5f);
|
||||
}
|
||||
|
||||
private void SetPropertiesOfEntrance()
|
||||
void SetPropertiesOfEntrance() {
|
||||
gameObject.GetComponent<SpriteRenderer>().color = Color.green;
|
||||
isEntrance = true;
|
||||
}
|
||||
|
||||
IEnumerator OnMouseDown()
|
||||
{
|
||||
this.gameObject.GetComponent<SpriteRenderer>().color = Color.green;
|
||||
this.isEntrance = true;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue