using UnityEngine; using UnityEngine.Events; namespace EasyTalk.Controller { /// /// Extends the standard DialogueController by adding functionality to either play dialogue automatically when a collider (such as a player) enters the trigger collider /// on the AreaDialogueController's GameObject, or sends a notification via the onPrompt UnityEvent to allow something else to determine whether the dialogue should play. /// public class AreaDialogueController2D : DialogueController { /// /// The collider which activates the controller when colliding with this GameObject. /// [Tooltip("This is the collider of the object which, when it collides with this GameObject, will trigger the Dialogue to start, or trigger the prompt, depending" + "on the activation mode.")] [SerializeField] private Collider2D activator; /// /// The ID of the entry point where the Dialogue is entered when the controller is activated. /// [Tooltip("The ID of the Entry node where the Dialogue should enter. Can leave blank if you only have one entry node in the Dialogue.")] [SerializeField] private string entryPoint; /// /// The activation mode of the controller. /// [Tooltip("If set to PLAY_ON_ENTER, the dialogue will automatically start playing whenever the activator collider enters the trigger " + "collider of this GameObject. If set to PROMPT_ON_ENTER, the Prompt() method will be called, which by default invokes all registered" + " callbacks with the onPrompt Unity Event.")] [SerializeField] private DialogueActivationMode activationMode = DialogueActivationMode.PLAY_ON_ENTER; /// /// The deactivation mode of the controller. /// [Tooltip("If set to FINISH_PLAYING, the Dialogue will continue playing even after the activator collider leaves the trigger area of this GameObject. If set" + "to EXIT_ON_LEAVE_AREA, the Dialogue will exit immediately when the activator leaves the trigger area.")] [SerializeField] private DialogueDeactivationMode deactivationMode = DialogueDeactivationMode.FINISH_PLAYING; /// /// An event which is triggered when the activator enters the trigger area of this GameObject and the activation mode is set to PROMPT_ON_ENTER. /// [Tooltip("This event is triggered whenever the activator enters the trigger collider of this GameObject and the activation mode is set to PROMPT_ON_ENTER.")] [SerializeField] public UnityEvent onPrompt = new UnityEvent(); /// /// An event which is triggered whenever the activator collider enters the trigger of this GameObject. /// [Tooltip("This event is triggered whenever the activator enters the trigger collider of this GameObject.")] [SerializeField] public UnityEvent onAreaEntered = new UnityEvent(); /// /// An event which is triggered whenever the activator collider leaves the trigger of this GameObject. /// [Tooltip("This event is triggered whenever the activator exits the trigger collider of this GameObject.")] [SerializeField] public UnityEvent onAreaExited = new UnityEvent(); /// /// Triggers the onPrompt event. /// public virtual void Prompt() { if (onPrompt != null) { onPrompt.Invoke(); } } /// /// Plays the Dialogue and triggers the onPlay event. /// public virtual void Play() { if (entryPoint == null || entryPoint.Length == 0) { PlayDialogue(); } else { PlayDialogue(entryPoint); } } /// /// Exits the Dialogue. /// public virtual void Exit() { ExitDialogue(); } /// /// If the 'other' collider is the activator then the Dialogue will be played or the onPrompt event will be triggered, depending on the activation mode. This method will /// also trigger the onAreaEntered event. /// /// The collider which entered this trigger. private void OnTriggerEnter2D(Collider2D other) { if (other == activator) { if (onAreaEntered != null) { onAreaEntered.Invoke(); } if (activationMode == DialogueActivationMode.PLAY_ON_ENTER) { Play(); } else { Prompt(); } } } /// /// If the 'other' collider is the activator then the Dialogue will be exited if the deactivation mode is EXIT_ON_LEAVE_AREA. This method also triggers the /// onAreaExited event. /// /// The collider which entered this trigger. private void OnTriggerExit2D(Collider2D other) { if (other == activator) { if (onAreaExited != null) { onAreaExited.Invoke(); } if (deactivationMode == DialogueDeactivationMode.EXIT_ON_LEAVE_AREA && isRunning) { Exit(); } } } /// /// The dialogue activation mode for an area controller. /// public enum DialogueActivationMode { PLAY_ON_ENTER, PROMPT_ON_ENTER } /// /// The dialogue deactivation mode for an area controller. /// public enum DialogueDeactivationMode { EXIT_ON_LEAVE_AREA, FINISH_PLAYING } } }