From 4bd34a13356f78db7830115014daf451bb4e6fd9 Mon Sep 17 00:00:00 2001 From: Max Dodd Date: Fri, 17 Jan 2025 20:03:15 +0100 Subject: [PATCH] Implemented basic room exploration logic. --- .../.idea.PuzzleGameProject/.idea/.gitignore | 13 ++++ .../.idea/indexLayout.xml | 8 +++ .../.idea.PuzzleGameProject/.idea/vcs.xml | 6 ++ PuzzleGameProject/Assets/Prefabs/Room.prefab | 64 ++++++++++++++++++ PuzzleGameProject/Assets/Scenes/Level_0.unity | 19 +----- .../Assets/Scripts/ColorUtility.cs | 18 +++++ .../Assets/Scripts/ColorUtility.cs.meta | 2 + .../Assets/Scripts/GameManager.cs | 25 +++++-- PuzzleGameProject/Assets/Scripts/Room.cs | 66 +++++++++++++++---- .../Assets/Scripts/RoomManager.cs | 29 -------- .../Assets/Scripts/RoomManager.cs.meta | 2 - .../Assets/Scripts/TurnManager.cs | 24 ------- .../Assets/Scripts/TurnManager.cs.meta | 2 - 13 files changed, 186 insertions(+), 92 deletions(-) create mode 100644 PuzzleGameProject/.idea/.idea.PuzzleGameProject/.idea/.gitignore create mode 100644 PuzzleGameProject/.idea/.idea.PuzzleGameProject/.idea/indexLayout.xml create mode 100644 PuzzleGameProject/.idea/.idea.PuzzleGameProject/.idea/vcs.xml create mode 100644 PuzzleGameProject/Assets/Scripts/ColorUtility.cs create mode 100644 PuzzleGameProject/Assets/Scripts/ColorUtility.cs.meta delete mode 100644 PuzzleGameProject/Assets/Scripts/RoomManager.cs delete mode 100644 PuzzleGameProject/Assets/Scripts/RoomManager.cs.meta delete mode 100644 PuzzleGameProject/Assets/Scripts/TurnManager.cs delete mode 100644 PuzzleGameProject/Assets/Scripts/TurnManager.cs.meta diff --git a/PuzzleGameProject/.idea/.idea.PuzzleGameProject/.idea/.gitignore b/PuzzleGameProject/.idea/.idea.PuzzleGameProject/.idea/.gitignore new file mode 100644 index 0000000..d5d8d71 --- /dev/null +++ b/PuzzleGameProject/.idea/.idea.PuzzleGameProject/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/.idea.PuzzleGameProject.iml +/contentModel.xml +/projectSettingsUpdater.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/PuzzleGameProject/.idea/.idea.PuzzleGameProject/.idea/indexLayout.xml b/PuzzleGameProject/.idea/.idea.PuzzleGameProject/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/PuzzleGameProject/.idea/.idea.PuzzleGameProject/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/PuzzleGameProject/.idea/.idea.PuzzleGameProject/.idea/vcs.xml b/PuzzleGameProject/.idea/.idea.PuzzleGameProject/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/PuzzleGameProject/.idea/.idea.PuzzleGameProject/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/PuzzleGameProject/Assets/Prefabs/Room.prefab b/PuzzleGameProject/Assets/Prefabs/Room.prefab index 11f2d60..5909457 100644 --- a/PuzzleGameProject/Assets/Prefabs/Room.prefab +++ b/PuzzleGameProject/Assets/Prefabs/Room.prefab @@ -11,6 +11,7 @@ GameObject: - component: {fileID: 9187907134033443523} - component: {fileID: 1770509300873928573} - component: {fileID: 2727030095425591755} + - component: {fileID: 4019891345885281529} m_Layer: 0 m_Name: Room m_TagString: Untagged @@ -103,6 +104,69 @@ MonoBehaviour: m_EditorClassIdentifier: number: 0 numberTextObject: {fileID: 8119019481281764985} + adjacentRooms: [] + isEntrance: 0 + validRoomClickedEvent: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 11500000, guid: 85fe57d438ef1ec4fa8b36b9c17c11a4, type: 3} + m_TargetAssemblyTypeName: UnityEngine.Object, UnityEngine + m_MethodName: set_name + m_Mode: 5 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: ValidRoomClicked + m_BoolArgument: 0 + m_CallState: 2 +--- !u!61 &4019891345885281529 +BoxCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2435349004046080434} + m_Enabled: 1 + serializedVersion: 3 + m_Density: 1 + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_ForceSendLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_ForceReceiveLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_ContactCaptureLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_CallbackLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_IsTrigger: 1 + m_UsedByEffector: 0 + m_CompositeOperation: 0 + m_CompositeOrder: 0 + m_Offset: {x: 0, y: 0} + m_SpriteTilingProperty: + border: {x: 0, y: 0, z: 0, w: 0} + pivot: {x: 0.5, y: 0.5} + oldSize: {x: 1, y: 1} + newSize: {x: 1, y: 1} + adaptiveTilingThreshold: 0.5 + drawMode: 0 + adaptiveTiling: 0 + m_AutoTiling: 0 + m_Size: {x: 1, y: 1} + m_EdgeRadius: 0 --- !u!1 &7488793311042651820 GameObject: m_ObjectHideFlags: 0 diff --git a/PuzzleGameProject/Assets/Scenes/Level_0.unity b/PuzzleGameProject/Assets/Scenes/Level_0.unity index 3119f12..b652471 100644 --- a/PuzzleGameProject/Assets/Scenes/Level_0.unity +++ b/PuzzleGameProject/Assets/Scenes/Level_0.unity @@ -339,7 +339,6 @@ GameObject: m_Component: - component: {fileID: 126339302} - component: {fileID: 126339304} - - component: {fileID: 126339303} m_Layer: 0 m_Name: GameManager m_TagString: Untagged @@ -362,19 +361,6 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &126339303 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 126339301} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: f0c75429168c5fb418e656b295433971, type: 3} - m_Name: - m_EditorClassIdentifier: - rooms: {fileID: 1820284205} --- !u!114 &126339304 MonoBehaviour: m_ObjectHideFlags: 0 @@ -384,10 +370,11 @@ MonoBehaviour: m_GameObject: {fileID: 126339301} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 6d900f1b1caebac429626fd7fe820582, type: 3} + m_Script: {fileID: 11500000, guid: 85fe57d438ef1ec4fa8b36b9c17c11a4, type: 3} m_Name: m_EditorClassIdentifier: - roomManager: {fileID: 0} + state: 0 + rooms: {fileID: 1820284205} --- !u!1001 &408992220 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/PuzzleGameProject/Assets/Scripts/ColorUtility.cs b/PuzzleGameProject/Assets/Scripts/ColorUtility.cs new file mode 100644 index 0000000..72810d2 --- /dev/null +++ b/PuzzleGameProject/Assets/Scripts/ColorUtility.cs @@ -0,0 +1,18 @@ +using UnityEngine; + +public static class ColorUtility +{ + public static Color AddGreyShade(Color originalColor, float greyIntensity) + { + // Clamp the greyIntensity between 0 and 1 + greyIntensity = Mathf.Clamp01(greyIntensity); + + // Define the grey color (e.g., medium grey) + Color grey = new Color(0.5f, 0.5f, 0.5f); // RGB (128, 128, 128) + + // Blend the original color with the grey color + Color blendedColor = Color.Lerp(originalColor, grey, greyIntensity); + + return blendedColor; + } +} diff --git a/PuzzleGameProject/Assets/Scripts/ColorUtility.cs.meta b/PuzzleGameProject/Assets/Scripts/ColorUtility.cs.meta new file mode 100644 index 0000000..8f71d3e --- /dev/null +++ b/PuzzleGameProject/Assets/Scripts/ColorUtility.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: b672f01694df748be8bec0b7866a5972 \ No newline at end of file diff --git a/PuzzleGameProject/Assets/Scripts/GameManager.cs b/PuzzleGameProject/Assets/Scripts/GameManager.cs index f8716d9..57af67a 100644 --- a/PuzzleGameProject/Assets/Scripts/GameManager.cs +++ b/PuzzleGameProject/Assets/Scripts/GameManager.cs @@ -1,19 +1,30 @@ +using System; using UnityEngine; +using Object = UnityEngine.Object; +public enum GameState +{ + PickRoom +} public class GameManager : MonoBehaviour { - public TurnManager turnManager; - public RoomManager roomManager; + public GameState state; + public GameObject rooms; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { - roomManager.InnitializeRooms(); + state = GameState.PickRoom; + foreach (Transform roomTransform in rooms.transform) + { + roomTransform.gameObject.GetComponent().ValidRoomClicked += ValidRoomClicked; + } } - // Update is called once per frame - void Update() - { - + void ValidRoomClicked(object sender, Room room) { + if (state == GameState.PickRoom) + { + room.SetRoomExplored(); + } } } diff --git a/PuzzleGameProject/Assets/Scripts/Room.cs b/PuzzleGameProject/Assets/Scripts/Room.cs index 473b43b..8b244e5 100644 --- a/PuzzleGameProject/Assets/Scripts/Room.cs +++ b/PuzzleGameProject/Assets/Scripts/Room.cs @@ -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 adjacentRooms; public bool isEntrance; - private bool isExplored = false; + public event EventHandler 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().color = + ColorUtility.AddGreyShade(gameObject.GetComponent().color, 0.5f); } - private void SetPropertiesOfEntrance() + void SetPropertiesOfEntrance() { + gameObject.GetComponent().color = Color.green; + isEntrance = true; + } + + IEnumerator OnMouseDown() { - this.gameObject.GetComponent().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()._isExplored) + { + return true; + } + } + return false; } } diff --git a/PuzzleGameProject/Assets/Scripts/RoomManager.cs b/PuzzleGameProject/Assets/Scripts/RoomManager.cs deleted file mode 100644 index 9eb8545..0000000 --- a/PuzzleGameProject/Assets/Scripts/RoomManager.cs +++ /dev/null @@ -1,29 +0,0 @@ -using UnityEngine; -using System.Collections.Generic; - -public class RoomManager : MonoBehaviour -{ - public GameObject rooms; - private List _entranceRooms = new List(); - - public void InnitializeRooms() - { - AddEntranceRoomsToList(rooms); - } - - private void AddEntranceRoomsToList(GameObject rooms) - { - foreach (Transform roomTransform in rooms.transform) - { - if (roomTransform.gameObject.GetComponent().isEntrance) - { - _entranceRooms.Add(roomTransform.gameObject); - } - } - } - - public List GetEntranceRooms() - { - return _entranceRooms; - } -} diff --git a/PuzzleGameProject/Assets/Scripts/RoomManager.cs.meta b/PuzzleGameProject/Assets/Scripts/RoomManager.cs.meta deleted file mode 100644 index 916a2a1..0000000 --- a/PuzzleGameProject/Assets/Scripts/RoomManager.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: f0c75429168c5fb418e656b295433971 \ No newline at end of file diff --git a/PuzzleGameProject/Assets/Scripts/TurnManager.cs b/PuzzleGameProject/Assets/Scripts/TurnManager.cs deleted file mode 100644 index 7589585..0000000 --- a/PuzzleGameProject/Assets/Scripts/TurnManager.cs +++ /dev/null @@ -1,24 +0,0 @@ -using UnityEngine; - -public class TurnManager : MonoBehaviour -{ - public RoomManager roomManager; - private int _turn = 0; - - // 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() - { - - } - - private void takeTurn() - { - - } -} diff --git a/PuzzleGameProject/Assets/Scripts/TurnManager.cs.meta b/PuzzleGameProject/Assets/Scripts/TurnManager.cs.meta deleted file mode 100644 index 26fe8dc..0000000 --- a/PuzzleGameProject/Assets/Scripts/TurnManager.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: 6d900f1b1caebac429626fd7fe820582 \ No newline at end of file