From d228d1cd34aa30f8e2c5dbf298d1a06d6269e054 Mon Sep 17 00:00:00 2001 From: rezazfn Date: Sat, 8 Feb 2025 22:11:47 +0800 Subject: [PATCH] game: info collecting mechanic, can build off to add as data in web --- .../Prefabs/Reza Assets/Needle_Reza.prefab | 2 +- Game/Assets/Scenes/House.unity | 155 +++++++++++++++++- Game/Assets/Scripts/GameManager.cs | 41 ++++- Game/Assets/Scripts/InfoCollector.cs | 54 ++++-- Game/ProjectSettings/TagManager.asset | 1 + 5 files changed, 229 insertions(+), 24 deletions(-) diff --git a/Game/Assets/Prefabs/Reza Assets/Needle_Reza.prefab b/Game/Assets/Prefabs/Reza Assets/Needle_Reza.prefab index 7e0b9bc..c151434 100644 --- a/Game/Assets/Prefabs/Reza Assets/Needle_Reza.prefab +++ b/Game/Assets/Prefabs/Reza Assets/Needle_Reza.prefab @@ -19,7 +19,7 @@ GameObject: - component: {fileID: 5684876363966156978} m_Layer: 0 m_Name: Needle_Reza - m_TagString: Untagged + m_TagString: InfoObject m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 diff --git a/Game/Assets/Scenes/House.unity b/Game/Assets/Scenes/House.unity index cd9dbc1..8395efc 100644 --- a/Game/Assets/Scenes/House.unity +++ b/Game/Assets/Scenes/House.unity @@ -695,7 +695,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5} - m_AnchoredPosition: {x: 367, y: 191} + m_AnchoredPosition: {x: 370, y: 181} m_SizeDelta: {x: 200, y: 50} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &9836844 @@ -745,8 +745,8 @@ MonoBehaviour: m_faceColor: serializedVersion: 2 rgba: 4294967295 - m_fontSize: 30 - m_fontSizeBase: 30 + m_fontSize: 16 + m_fontSizeBase: 16 m_fontWeight: 400 m_enableAutoSizing: 0 m_fontSizeMin: 18 @@ -781,7 +781,7 @@ MonoBehaviour: m_VertexBufferAutoSizeReduction: 0 m_useMaxVisibleDescender: 1 m_pageToDisplay: 1 - m_margin: {x: -231.23126, y: 0, z: 45.508484, w: 0} + m_margin: {x: -231.23126, y: 0, z: 116.85144, w: -79.70676} m_isUsingLegacyAnimationComponent: 0 m_isVolumetricText: 0 m_hasFontAssetChanged: 0 @@ -62387,7 +62387,7 @@ MeshCollider: serializedVersion: 5 m_Convex: 0 m_CookingOptions: 30 - m_Mesh: {fileID: 1837428087} + m_Mesh: {fileID: 2012346953} --- !u!33 &907749283 MeshFilter: m_ObjectHideFlags: 10 @@ -73219,11 +73219,11 @@ PrefabInstance: objectReference: {fileID: 2105633704} - target: {fileID: 2618367384921537347, guid: 115e0f30c5ec185489b02437aec67077, type: 3} propertyPath: m_VersionIndex - value: 3215 + value: 3221 objectReference: {fileID: 0} - target: {fileID: 3510408986590439666, guid: 115e0f30c5ec185489b02437aec67077, type: 3} propertyPath: m_Name - value: Needle_Reza (1) + value: Needles objectReference: {fileID: 0} - target: {fileID: 4226959381646210589, guid: 115e0f30c5ec185489b02437aec67077, type: 3} propertyPath: m_Mesh @@ -80808,6 +80808,140 @@ MonoBehaviour: m_SelectedFaces: m_SelectedEdges: [] m_SelectedVertices: +--- !u!1 &1177838948 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1177838949} + - component: {fileID: 1177838951} + - component: {fileID: 1177838950} + m_Layer: 5 + m_Name: Crosshair + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1177838949 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1177838948} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 1776114652} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 200, y: 50} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &1177838950 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1177838948} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_text: + + m_isRightToLeft: 0 + m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} + m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} + m_fontSharedMaterials: [] + m_fontMaterial: {fileID: 0} + m_fontMaterials: [] + m_fontColor32: + serializedVersion: 2 + rgba: 4294967295 + m_fontColor: {r: 1, g: 1, b: 1, a: 1} + m_enableVertexGradient: 0 + m_colorMode: 3 + m_fontColorGradient: + topLeft: {r: 1, g: 1, b: 1, a: 1} + topRight: {r: 1, g: 1, b: 1, a: 1} + bottomLeft: {r: 1, g: 1, b: 1, a: 1} + bottomRight: {r: 1, g: 1, b: 1, a: 1} + m_fontColorGradientPreset: {fileID: 0} + m_spriteAsset: {fileID: 0} + m_tintAllSprites: 0 + m_StyleSheet: {fileID: 0} + m_TextStyleHashCode: -1183493901 + m_overrideHtmlColors: 0 + m_faceColor: + serializedVersion: 2 + rgba: 4294967295 + m_fontSize: 18 + m_fontSizeBase: 18 + m_fontWeight: 400 + m_enableAutoSizing: 0 + m_fontSizeMin: 18 + m_fontSizeMax: 72 + m_fontStyle: 0 + m_HorizontalAlignment: 2 + m_VerticalAlignment: 512 + m_textAlignment: 65535 + m_characterSpacing: 0 + m_wordSpacing: 0 + m_lineSpacing: 0 + m_lineSpacingMax: 0 + m_paragraphSpacing: 0 + m_charWidthMaxAdj: 0 + m_enableWordWrapping: 1 + m_wordWrappingRatios: 0.4 + m_overflowMode: 0 + m_linkedTextComponent: {fileID: 0} + parentLinkedComponent: {fileID: 0} + m_enableKerning: 1 + m_enableExtraPadding: 0 + checkPaddingRequired: 0 + m_isRichText: 1 + m_parseCtrlCharacters: 1 + m_isOrthographic: 1 + m_isCullingEnabled: 0 + m_horizontalMapping: 0 + m_verticalMapping: 0 + m_uvLineOffset: 0 + m_geometrySortingOrder: 0 + m_IsTextObjectScaleStatic: 0 + m_VertexBufferAutoSizeReduction: 0 + m_useMaxVisibleDescender: 1 + m_pageToDisplay: 1 + m_margin: {x: 0, y: 0, z: 0, w: 0} + m_isUsingLegacyAnimationComponent: 0 + m_isVolumetricText: 0 + m_hasFontAssetChanged: 0 + m_baseMaterial: {fileID: 0} + m_maskOffset: {x: 0, y: 0, z: 0, w: 0} +--- !u!222 &1177838951 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1177838948} + m_CullTransparentMesh: 1 --- !u!1 &1184053919 GameObject: m_ObjectHideFlags: 0 @@ -114248,6 +114382,7 @@ RectTransform: m_Children: - {fileID: 707184550} - {fileID: 5551323} + - {fileID: 1177838949} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} @@ -122441,6 +122576,8 @@ MonoBehaviour: gazeTimeRequired: 2 displayTime: 5 infoText: {fileID: 9836844} + audioSource: {fileID: 1898819871} + scribbleSound: {fileID: 8300000, guid: 28757b19d93f6db4bb4b45260f18bc87, type: 3} --- !u!1 &1853702029 GameObject: m_ObjectHideFlags: 0 @@ -148129,11 +148266,11 @@ PrefabInstance: objectReference: {fileID: 1425780483} - target: {fileID: 2618367384921537347, guid: 115e0f30c5ec185489b02437aec67077, type: 3} propertyPath: m_VersionIndex - value: 3215 + value: 3221 objectReference: {fileID: 0} - target: {fileID: 3510408986590439666, guid: 115e0f30c5ec185489b02437aec67077, type: 3} propertyPath: m_Name - value: Needle_Reza + value: Needles objectReference: {fileID: 0} - target: {fileID: 4226959381646210589, guid: 115e0f30c5ec185489b02437aec67077, type: 3} propertyPath: m_Mesh diff --git a/Game/Assets/Scripts/GameManager.cs b/Game/Assets/Scripts/GameManager.cs index 6d35e80..9515e8a 100644 --- a/Game/Assets/Scripts/GameManager.cs +++ b/Game/Assets/Scripts/GameManager.cs @@ -1,7 +1,7 @@ /* Author: Reza Date: 3/2/25 -Description: To keep track of tasks and game mechanics +Description: To keep track of tasks, which level the player is at, and game mechanics */ using System.Collections; @@ -10,6 +10,45 @@ using UnityEngine; public class GameManager : MonoBehaviour { + /* Game Story Flow: + - Player cleans room while waiting for parents to leave + - The door unlocks after room is cleaned and explored and player either sweeps floor or brushes teeth + - + */ + + /// + /// singleton pattern: define instance field for accessing the singleton elsewhere + /// + public static GameManager Instance; + + /// + /// enforces singleton behaviour; sets doesn't destroy on load and checks for multiple instances + /// + private void Awake() + { + // check if instance hasn't been set yet + if (Instance == null) + { + Debug.Log("awake as singleton instance, setting self as the forever-alive instance"); + Instance = this; + DontDestroyOnLoad(gameObject); + } + // check if instance is already set and it's not this instance + else if (Instance != null && Instance != this) + { + Debug.Log("awake as non-singleton instance, destroying self"); + Destroy(gameObject); + } + } + + /// + /// trackable game tasks (Reza) + /// + private bool bedroomCleaned = false; + private bool teethBrushed = false; + private bool floorSweeped = false; + + // Start is called before the first frame update void Start() { diff --git a/Game/Assets/Scripts/InfoCollector.cs b/Game/Assets/Scripts/InfoCollector.cs index 2a12952..0ef7a95 100644 --- a/Game/Assets/Scripts/InfoCollector.cs +++ b/Game/Assets/Scripts/InfoCollector.cs @@ -15,10 +15,10 @@ public class InfoCollector : MonoBehaviour public float detectionRange = 2f; // Defines how long the player must look at the object for to collect information - public float gazeTimeRequired = 2f; + public float gazeTimeRequired = 1.5f; // Defines how long the information UI will remain on screen - public float displayTime = 5f; + public float displayTime = 7f; // Defines the UI text to display public TMP_Text infoText; @@ -34,6 +34,13 @@ public class InfoCollector : MonoBehaviour // Tracks how long the player has been looking at the object private float gazeTimer = 0f; + + // Defines Audio References + public AudioSource audioSource; + public AudioClip scribbleSound; + + // Store objects that have already been collected + private HashSet collectedObjects = new HashSet(); void Start() { @@ -56,23 +63,29 @@ public class InfoCollector : MonoBehaviour // Ensures that relevant info objects are detected if (hit.collider.CompareTag("InfoObject")) { - // If the player is still looking at the same object, the gaze time - if (currentObject == hit.collider.gameObject) + GameObject targetObject = hit.collider.gameObject; + + // **Fix: Stop gaze timer if object has been collected** + if (collectedObjects.Contains(targetObject)) + { + return; // Exit without increasing gaze time + } + + // If the player is still looking at the same object, increase gaze time + if (currentObject == targetObject) { - // Increases gaze time gazeTimer += Time.deltaTime; - // If gaze time reaches required time and is not displaying yet, display the information + // If gaze time reaches required time and info is not displayed yet if (gazeTimer >= gazeTimeRequired && !isDisplaying) { - // Display object information - CollectInfo(currentObject); + CollectInfo(targetObject); } } else { - // Reset timer if looking at a new object - currentObject = hit.collider.gameObject; + // Reset timer when looking at a new object + currentObject = targetObject; gazeTimer = 0f; } } @@ -91,9 +104,20 @@ public class InfoCollector : MonoBehaviour // Prevents spamming of display isDisplaying = true; + // **Fix: Mark object as collected** + collectedObjects.Add(obj); + // Displays information - infoText.text = "Info Collected: " + obj.name + "\n" + GetObjectInfo(obj); + infoText.text = "Info Collected:\n\n" + + "" + obj.name + "\n" + + "" + GetObjectInfo(obj) + ""; Debug.Log("Collected information from: " + obj.name); + + // Play sound only if no other sound is currently playing + if (!audioSource.isPlaying) + { + audioSource.PlayOneShot(scribbleSound); + } // Clears text after displayed time Invoke(nameof(ClearText), displayTime); @@ -112,9 +136,13 @@ public class InfoCollector : MonoBehaviour string GetObjectInfo(GameObject obj) { // Check if the object's name is the same - if (obj.CompareTag("Needles")) + if (obj.name == "Needles") // Returns predefined information - return "Test"; + return "A used needle. If my parents finds out they would murder me."; + + if (obj.name == "Bottles") + // Returns predefined information + return "An empty bottle. It numbs the pain... but not for long enough"; // Default information if there is no specific case return "An unknown object with mysterious origins."; diff --git a/Game/ProjectSettings/TagManager.asset b/Game/ProjectSettings/TagManager.asset index cd1dd5d..64368c9 100644 --- a/Game/ProjectSettings/TagManager.asset +++ b/Game/ProjectSettings/TagManager.asset @@ -11,6 +11,7 @@ TagManager: - InfoObject - Bottle - Dirt + - Needles layers: - Default - TransparentFX