Refactored locks for rooms to be more versatile by giving them their own type.
This commit is contained in:
parent
7bec7c539d
commit
d79d6216f0
13 changed files with 85 additions and 53 deletions
|
|
@ -70,7 +70,7 @@ public class DicePair
|
|||
return _pair.Item1.GetResult() + _pair.Item2.GetResult();
|
||||
}
|
||||
|
||||
public bool DoResultsMatch()
|
||||
public bool CheckIfResultsMatch()
|
||||
{
|
||||
if (_pair.Item1.GetResult() == _pair.Item2.GetResult())
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,16 +4,15 @@ using UnityEngine;
|
|||
public class EmptyRoom : Room
|
||||
{
|
||||
[SerializeField] private GameObject numberTextObject;
|
||||
[SerializeField] private int number;
|
||||
|
||||
protected override void InitializeRoom() {
|
||||
base.InitializeRoom();
|
||||
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("=");
|
||||
}
|
||||
|
|
@ -21,24 +20,7 @@ public class EmptyRoom : Room
|
|||
|
||||
public override 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;
|
||||
return _locks[0].CheckIfKeyFits(pair);
|
||||
}
|
||||
|
||||
public override void SetRoomExplored() {
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 65db8ffa325fb48e89716418fa310bca
|
||||
8
PuzzleGameProject/Assets/Scripts/Rooms/Locks.meta
Normal file
8
PuzzleGameProject/Assets/Scripts/Rooms/Locks.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a3af0e06c321aa8458a492be0ed8db63
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
23
PuzzleGameProject/Assets/Scripts/Rooms/Locks/Lock.cs
Normal file
23
PuzzleGameProject/Assets/Scripts/Rooms/Locks/Lock.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b665fd3798293ac4e9eb870c53c878a0
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b40cbe88356086a49b6bc7dc8ff549c0
|
||||
21
PuzzleGameProject/Assets/Scripts/Rooms/Locks/NumberLock.cs
Normal file
21
PuzzleGameProject/Assets/Scripts/Rooms/Locks/NumberLock.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0f021f4903d50cb4b8bea2c40701ad58
|
||||
|
|
@ -5,7 +5,6 @@ using TMPro;
|
|||
|
||||
public class MonsterRoom : Room
|
||||
{
|
||||
[SerializeField] private List<int> locks = new();
|
||||
[SerializeField] private GameObject numberTextObject;
|
||||
[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();
|
||||
|
||||
// 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;
|
||||
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.GetComponent<TextMeshProUGUI>().text = locks[i].ToString();
|
||||
lockToDuplicate.GetComponent<TextMeshProUGUI>().text = ((NumberLock)_locks[i]).GetNumber().ToString();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,21 +5,16 @@ using UnityEngine;
|
|||
using System.Collections.Generic;
|
||||
using Unity.VisualScripting;
|
||||
|
||||
public enum KeyType
|
||||
{
|
||||
Number,
|
||||
MatchingDice
|
||||
}
|
||||
|
||||
public abstract class Room : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private List<GameObject> adjacentRooms;
|
||||
[SerializeField] private bool isEntrance;
|
||||
[SerializeField] protected KeyType keyType;
|
||||
public event EventHandler<Room> ValidRoomClicked;
|
||||
private Color _roomNumberOriginalColor;
|
||||
|
||||
protected bool _isExplored = false;
|
||||
protected Lock[] _locks;
|
||||
private bool _isClickable = true;
|
||||
|
||||
private void OnEnable() {
|
||||
|
|
@ -46,6 +41,7 @@ public abstract class Room : MonoBehaviour
|
|||
if (isEntrance) {
|
||||
SetPropertiesOfEntrance();
|
||||
}
|
||||
_locks = gameObject.GetComponents<Lock>();
|
||||
}
|
||||
|
||||
protected void HighlightRoomAsOption()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue