// Copyright (c) Pixel Crushers. All rights reserved.
using UnityEngine;
using UnityEngine.Events;
using System;
using UnityEngine.Serialization;
namespace PixelCrushers.DialogueSystem
{
public delegate void UsableDelegate(Usable usable);
///
/// This component indicates that the game object is usable. This component works in
/// conjunction with the Selector component. If you leave overrideName blank but there
/// is an OverrideActorName component on the same object, this component will use
/// the name specified in OverrideActorName.
///
[AddComponentMenu("")] // Use wrapper.
public class Usable : MonoBehaviour
{
///
/// (Optional) Overrides the name shown by the Selector.
///
[SerializeField]
[FormerlySerializedAs("overrideName")]
private string m_overrideName;
public virtual string overrideName { get => m_overrideName; set => m_overrideName = value; }
///
/// (Optional) Overrides the use message shown by the Selector.
///
[SerializeField]
[FormerlySerializedAs("overrideUseMessage")]
private string m_overrideUseMessage;
public virtual string overrideUseMessage { get => m_overrideUseMessage; set => m_overrideUseMessage = value; }
///
/// The max distance at which the object can be used.
///
public float maxUseDistance = 5f;
[Serializable]
public class UsableEvents
{
///
/// Invoked when a Selector or ProximitySelector selects this Usable.
///
public UnityEvent onSelect = new UnityEvent();
///
/// Invoked when a Selector or ProximitySelector deselects this Usable.
///
public UnityEvent onDeselect = new UnityEvent();
///
/// Invoked when a Selector or ProximitySelector uses this Usable.
///
public UnityEvent onUse = new UnityEvent();
}
public UsableEvents events;
public event UsableDelegate disabled = delegate { };
protected virtual void OnDisable()
{
disabled(this);
}
public virtual void Start()
{
}
///
/// Gets the name of the override, including parsing if it contains a [lua]
/// or [var] tag.
///
/// The override name.
public virtual string GetName()
{
if (string.IsNullOrEmpty(overrideName))
{
return DialogueActor.GetActorName(transform);
}
else if (overrideName.Contains("[lua") || overrideName.Contains("[var"))
{
return DialogueManager.GetLocalizedText(FormattedText.Parse(overrideName, DialogueManager.masterDatabase.emphasisSettings).text);
}
else
{
return DialogueManager.GetLocalizedText(overrideName);
}
}
public virtual void OnSelectUsable()
{
if (events != null && events.onSelect != null) events.onSelect.Invoke();
}
public virtual void OnDeselectUsable()
{
if (events != null && events.onDeselect != null) events.onDeselect.Invoke();
}
public virtual void OnUseUsable()
{
if (events != null && events.onUse != null) events.onUse.Invoke();
}
}
}