Files
Click-PointRPG/Assets/Scripts/Interfaces/InteractiveObjectBase.cs

95 lines
2.7 KiB
C#

using UnityEngine;
/// <summary>
/// Base class for all interactive objects that implement IInteractiveObject.
/// Consolidates common properties and methods to reduce code duplication.
/// </summary>
public abstract class InteractiveObjectBase : MonoBehaviour, IInteractiveObject
{
[Header("Interactive Object Settings")]
[SerializeField]
protected string objectDisplayName = "Interactive Object";
[SerializeField]
protected Transform customInteractionPoint; // Optional: use instead of collider center
[SerializeField]
protected bool enableDebugLogs = false;
/// <summary>
/// Get the interaction point - uses custom point if assigned, otherwise collider center
/// </summary>
public virtual Vector3 GetInteractionPoint()
{
if (customInteractionPoint != null)
{
Log("GetInteractionPoint: Using custom interaction point");
return customInteractionPoint.position;
}
Collider collider = GetComponent<Collider>();
if (collider != null)
{
Log($"GetInteractionPoint: Using collider center at {collider.bounds.center}");
return collider.bounds.center;
}
Log("GetInteractionPoint: No collider found, using transform position");
return transform.position;
}
/// <summary>
/// Get the display name for this interactive object
/// </summary>
public virtual string GetDisplayName()
{
Log($"GetDisplayName: Returning '{objectDisplayName}'");
return objectDisplayName;
}
/// <summary>
/// Check if this object can be interacted with
/// Override in derived classes to add custom logic
/// </summary>
public virtual bool CanInteract()
{
Log("CanInteract: Default implementation returns true");
return true;
}
/// <summary>
/// Perform the interaction
/// Must be implemented by derived classes
/// </summary>
public abstract void Interact(GameObject player);
/// <summary>
/// Protected logging utility with optional debug toggle
/// </summary>
protected void Log(string message)
{
if (enableDebugLogs)
{
DebugManager.Log(GetType().Name, message, gameObject);
}
}
/// <summary>
/// Set the display name at runtime
/// </summary>
public void SetDisplayName(string newName)
{
objectDisplayName = newName;
Log($"Display name changed to '{newName}'");
}
/// <summary>
/// Set custom interaction point Transform
/// </summary>
public void SetCustomInteractionPoint(Transform point)
{
customInteractionPoint = point;
Log($"Custom interaction point set to {point.name}");
}
}