interactable

This commit is contained in:
Janis
2023-03-04 15:53:47 +01:00
parent ae0b21f9e9
commit 5f08865a2b
17 changed files with 506 additions and 357 deletions

View File

@@ -0,0 +1,53 @@
// Base class for interactable object in the top-down game
using UnityEngine;
public abstract class Interactable : MonoBehaviour
{
[SerializeField]
[Range(0f, 10f)]
private float radius = 2f; // radius of interaction
[SerializeField]
private string interactText = "Interact"; // text to display when player is in range
[SerializeField]
private string interactTextOutOfRange = "Out of range"; // text to display when player is out of range
public string GetInteractText(GameObject interactor = null)
{
if (interactor != null && IsInRange(interactor))
{
return interactText;
}
else { return interactTextOutOfRange; }
}
public abstract void OnInteract();
public float GetRadius() { return radius; }
public void Interact(GameObject interactor)
{
// check if in range
if (!IsInRange(interactor))
{
Debug.Log("Out of range");
return;
}
Debug.Log("Interacting with " + transform.name);
OnInteract();
}
public bool IsInRange(GameObject interactor)
{
return Vector2.Distance(transform.position, interactor.transform.position) <= radius;
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, radius);
}
}