Files
nightclub-arcadia/NightclubArcadia/Assets/Scripts/Dialogue/DialogueInteractable.cs
T

55 lines
1.7 KiB
C#

using UnityEngine;
using Yarn.Unity;
namespace NightclubArcadia.Dialogue
{
/// <summary>
/// Trigger-zone interactable: player walks in range, presses E, and this
/// starts the given Yarn node. The reverse of DialogueTransitionController's
/// <<enter_environment>> — this is the "step into dialogue" half of the loop.
/// </summary>
public class DialogueInteractable : MonoBehaviour
{
[SerializeField] private DialogueRunner dialogueRunner;
[SerializeField] private GameObject dialogueUI;
[SerializeField] private string yarnNodeName = "Bartender_Chat";
private bool playerInRange = false;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
playerInRange = true;
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
playerInRange = false;
}
void Update()
{
if (playerInRange && Input.GetKeyDown(KeyCode.E))
{
StartConversation();
}
}
async void StartConversation()
{
// Player controls are locked by DialogueTransitionController off the
// runner's onDialogueStart event — nothing to do for that here.
if (DialogueUIVisibility.Show(dialogueUI))
{
// Only reached if the UI was found deactivated; give TMP a
// frame to re-cache its Canvas in OnEnable before any text is
// generated.
await Awaitable.NextFrameAsync();
}
await dialogueRunner.StartDialogue(yarnNodeName);
}
}
}