Refactored locks for rooms to be more versatile by giving them their own type.

This commit is contained in:
Max 2025-01-29 12:02:00 +01:00
parent 7bec7c539d
commit d79d6216f0
13 changed files with 85 additions and 53 deletions

View file

@ -70,7 +70,7 @@ public class DicePair
return _pair.Item1.GetResult() + _pair.Item2.GetResult(); return _pair.Item1.GetResult() + _pair.Item2.GetResult();
} }
public bool DoResultsMatch() public bool CheckIfResultsMatch()
{ {
if (_pair.Item1.GetResult() == _pair.Item2.GetResult()) if (_pair.Item1.GetResult() == _pair.Item2.GetResult())
{ {

View file

@ -4,16 +4,15 @@ using UnityEngine;
public class EmptyRoom : Room public class EmptyRoom : Room
{ {
[SerializeField] private GameObject numberTextObject; [SerializeField] private GameObject numberTextObject;
[SerializeField] private int number;
protected override void InitializeRoom() { protected override void InitializeRoom() {
base.InitializeRoom(); base.InitializeRoom();
TextMeshProUGUI numberText = numberTextObject.GetComponent<TextMeshProUGUI>(); TextMeshProUGUI numberText = numberTextObject.GetComponent<TextMeshProUGUI>();
if (keyType == KeyType.Number) if (_locks[0] is NumberLock )
{ {
numberText.SetText(number.ToString()); numberText.SetText(((NumberLock)_locks[0]).GetNumber().ToString());
} }
else if (keyType == KeyType.MatchingDice) else if (_locks[0] is MatchingDiceLock)
{ {
numberText.SetText("="); numberText.SetText("=");
} }
@ -21,24 +20,7 @@ public class EmptyRoom : Room
public override bool TryUnlock(DicePair pair) public override bool TryUnlock(DicePair pair)
{ {
switch (keyType) return _locks[0].CheckIfKeyFits(pair);
{
case KeyType.Number:
if (number == pair.Sum())
{
return true;
}
return false;
case KeyType.MatchingDice:
if (pair.DoResultsMatch())
{
return true;
}
return false;
}
return false;
} }
public override void SetRoomExplored() { public override void SetRoomExplored() {

View file

@ -1,16 +0,0 @@
using UnityEngine;
public class Key : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}

View file

@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 65db8ffa325fb48e89716418fa310bca

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a3af0e06c321aa8458a492be0ed8db63
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,23 @@
using System;
using Unity.VisualScripting;
using UnityEngine;
public abstract class Lock : MonoBehaviour
{
public event Action Unlocked;
public abstract bool CheckIfKeyFits(DicePair dicePair);
public virtual void Unlock(DicePair dicePair)
{
if (CheckIfKeyFits(dicePair))
{
OnUnlock();
}
}
protected virtual void OnUnlock()
{
Unlocked?.Invoke();
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b665fd3798293ac4e9eb870c53c878a0

View file

@ -0,0 +1,15 @@
using Unity.VisualScripting;
using UnityEngine;
public class MatchingDiceLock : Lock
{
public override bool CheckIfKeyFits(DicePair dicePair)
{
if (dicePair.CheckIfResultsMatch())
{
return true;
}
return false;
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b40cbe88356086a49b6bc7dc8ff549c0

View file

@ -0,0 +1,21 @@
using UnityEngine;
public class NumberLock : Lock
{
[SerializeField] private int number;
public override bool CheckIfKeyFits(DicePair dicePair)
{
if (dicePair.Sum() == number)
{
return true;
}
return false;
}
public int GetNumber()
{
return number;
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 0f021f4903d50cb4b8bea2c40701ad58

View file

@ -5,7 +5,6 @@ using TMPro;
public class MonsterRoom : Room public class MonsterRoom : Room
{ {
[SerializeField] private List<int> locks = new();
[SerializeField] private GameObject numberTextObject; [SerializeField] private GameObject numberTextObject;
[SerializeField] private int _health; // Number of times the room needs to be unlocked before becoming explored. [SerializeField] private int _health; // Number of times the room needs to be unlocked before becoming explored.
@ -13,19 +12,19 @@ public class MonsterRoom : Room
base.InitializeRoom(); base.InitializeRoom();
// Create the lock numbers on the room. // Create the lock numbers on the room.
numberTextObject.GetComponent<TextMeshProUGUI>().text = locks[0].ToString(); numberTextObject.GetComponent<TextMeshProUGUI>().text = ((NumberLock)_locks[0]).GetNumber().ToString();
GameObject lockToDuplicate = numberTextObject; GameObject lockToDuplicate = numberTextObject;
for (int i = 1; i < locks.Count; i++) for (int i = 1; i < _locks.Length; i++)
{ {
lockToDuplicate = DuplicateToTheLeft(lockToDuplicate, ((RectTransform)lockToDuplicate.transform).rect.width); lockToDuplicate = DuplicateToTheLeft(lockToDuplicate, ((RectTransform)lockToDuplicate.transform).rect.width);
lockToDuplicate.GetComponent<TextMeshProUGUI>().text = locks[i].ToString(); lockToDuplicate.GetComponent<TextMeshProUGUI>().text = ((NumberLock)_locks[i]).GetNumber().ToString();
} }
} }
public override bool TryUnlock(DicePair pair) { public override bool TryUnlock(DicePair pair) {
foreach (int lockNumber in locks) foreach (NumberLock numberLock in _locks)
{ {
if (pair.Sum() == lockNumber) if (pair.Sum() == numberLock.GetNumber())
{ {
return true; return true;
} }

View file

@ -5,21 +5,16 @@ using UnityEngine;
using System.Collections.Generic; using System.Collections.Generic;
using Unity.VisualScripting; using Unity.VisualScripting;
public enum KeyType
{
Number,
MatchingDice
}
public abstract class Room : MonoBehaviour public abstract class Room : MonoBehaviour
{ {
[SerializeField] private List<GameObject> adjacentRooms; [SerializeField] private List<GameObject> adjacentRooms;
[SerializeField] private bool isEntrance; [SerializeField] private bool isEntrance;
[SerializeField] protected KeyType keyType;
public event EventHandler<Room> ValidRoomClicked; public event EventHandler<Room> ValidRoomClicked;
private Color _roomNumberOriginalColor; private Color _roomNumberOriginalColor;
protected bool _isExplored = false; protected bool _isExplored = false;
protected Lock[] _locks;
private bool _isClickable = true; private bool _isClickable = true;
private void OnEnable() { private void OnEnable() {
@ -46,6 +41,7 @@ public abstract class Room : MonoBehaviour
if (isEntrance) { if (isEntrance) {
SetPropertiesOfEntrance(); SetPropertiesOfEntrance();
} }
_locks = gameObject.GetComponents<Lock>();
} }
protected void HighlightRoomAsOption() protected void HighlightRoomAsOption()