From 40ac185dc7a017d95771fe580c77eab20e663908 Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Tue, 08 Apr 2025 17:36:08 +0000
Subject: [PATCH] #46 added interactables
---
Assets/Scripts/Interact/Interactable.cs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 60 insertions(+), 0 deletions(-)
diff --git a/Assets/Scripts/Interact/Interactable.cs b/Assets/Scripts/Interact/Interactable.cs
new file mode 100644
index 0000000..be2f63e
--- /dev/null
+++ b/Assets/Scripts/Interact/Interactable.cs
@@ -0,0 +1,60 @@
+// Interactable.cs - Base abstract class for all interactable objects
+using UnityEngine;
+using UnityEngine.Events;
+
+public abstract class Interactable : MonoBehaviour
+{
+ [SerializeField] private string promptMessage = "Press E to interact";
+ [SerializeField] private float interactionDistance = 3f;
+
+ // Event that will be triggered when the player interacts with this object
+ public UnityEvent OnInteract = new UnityEvent();
+
+ // Each derived class must implement how it processes interaction
+ public abstract void Interact();
+
+ // Returns the prompt message for UI display
+ public string GetPromptMessage()
+ {
+ return promptMessage;
+ }
+
+ // Check if player is within interaction distance
+ public bool IsInRange(Transform playerTransform)
+ {
+ float distance = Vector3.Distance(transform.position, playerTransform.position);
+ return distance <= interactionDistance;
+ }
+
+ // Optional: Visualize interaction range in editor
+ private void OnDrawGizmosSelected()
+ {
+ Gizmos.color = Color.yellow;
+ Gizmos.DrawWireSphere(transform.position, interactionDistance);
+ }
+ private void OnTriggerEnter2D(Collider2D other)
+ {
+ if (other.gameObject.layer == LayerMask.NameToLayer("Player"))
+ {
+ // Player entered trigger zone
+ PlayerInteraction playerInteraction = other.GetComponent<PlayerInteraction>();
+ if (playerInteraction != null)
+ {
+ playerInteraction.SetCurrentInteractable(this);
+ }
+ }
+ }
+
+ protected void OnTriggerExit2D(Collider2D other)
+ {
+ if (other.gameObject.layer == LayerMask.NameToLayer("Player"))
+ {
+ // Player exited trigger zone
+ PlayerInteraction playerInteraction = other.GetComponent<PlayerInteraction>();
+ if (playerInteraction != null)
+ {
+ playerInteraction.SetCurrentInteractable(null);
+ }
+ }
+ }
+}
--
Gitblit v1.10.0