-signed Macha-latte

## Overview
- The dialogue system is a single `DialogueBox` scene that gets created when `start_dialogue` node is called and destroyed when the dialogue ends.
  
- The dialogue system shows conversations written in Yarn Spinner. Any script in the game starts one with a single call
  
- It is made of three parts:
	- **Yarn Spinner** (the addon) reads the `.yarn` files and decides which line or choice comes next.
	- **DialogueManager** (an autoload) is the only way to open dialogue. It makes sure only one dialogue box is open at a time.
	- **DialogueBox** (a scene) is created fresh for every conversation and deletes itself when the conversation ends. It holds the Yarn runner, the presenters and the visuals.

## Requirements

- [x]  Open and close
- [x]  Typewriter text
- [x]  Advance with Z
- [x]  Show questions
- [x]  Pick answers with the keyboard only
- [x]  No mouse input for questions
- [x]  Delete the box when dialogue ends
- [x]  Only one box at a time
- [x]  Reopen fresh every time
- [x]  Start from a function call with a Yarn node name

## Setup

- The addon is in `addons/yarn_spinner/`.
- Compiling `.yarn` files needs the .NET SDK and the `YarnSpinner.Console` tool (`ysc`) installed globally. Restart Godot after installing them.
- All `.yarn` files are collected by dialogue/Project.yarnproject. A `.yarn` file that isn't included in the project won't compile.
- `DialogueManager` must be registered as an autoload in **Project Settings → Globals → Autoload**, with the name `DialogueManager`.

## Files

| File                                           | What it is                                                               |
| ---------------------------------------------- | ------------------------------------------------------------------------ |
| `dialogue/dialogue_manager.gd`                 | The `DialogueManager` autoload. Opens dialogue boxes.                    |
| `dialogue/dialogue_box.tscn` / `.gd`           | The dialogue box. One is created per conversation.                       |
| `dialogue/dialogue_option_button.tscn` / `.gd` | The button used for each choice. Keyboard only.                          |
| `dialogue/Project.yarnproject`                 | The Yarn project. Lists which `.yarn` files get compiled.                |
| `dialogue/test_talk.yarn`                      | Example dialogue used for testing.                                       |
| `dialogue/dialogue_test.tscn` / `.gd`          | Test scene. Starts dialogue when it loads, press T to start it again.    |
| `dialogue/portrait_object/`                    | See **OBJ PortraitObject**.                                              |
| `dialogue/characters/`                         | One file per character (name and portraits). See **OBJ PortraitObject**. |

## Structure

```
DialogueBox (CanvasLayer)
|--YarnDialogueRunner
|  |-YarnLinePresenter
|  |_YarnOptionsPresenter
|
|__PortraitObject
```

| Node               | What it does                                                                                                                                      |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| DialogueBox        | This is the root, A `CanvasLayer` on layer 10, so it draws on top of the game. Its script starts the dialogue and deletes the box at the end      |
| YarnDialogueRunner | Runs the Yarn file. It decides which line or choice comes next and sends it to the presenters. Its `yarn_project` is set to `Project.yarnproject` |
| YarnLinePresenter  | Shows a line. Types the text out, sets the speaker name, show the continue indicator and waits for Z                                              |
| PortraitObject     | Everthing visual the player sees. The labels that the presetner writs into live in here                                                           |

The presenters wont draw anything themselves, they are pointed at nodes inside PortraitObject

| Presenter setting | Points at |
| --- | --- |
| Line presenter → `text_label` | `PortraitObject/Box/TextLabel` |
| Line presenter → `character_label` | `PortraitObject/NameTag/CharacterLabel` |
| Line presenter → `character_container` | `PortraitObject/NameTag` |
| Line presenter → `continue_indicator` | `PortraitObject/Box/ContinueIndicator` |
| Options presenter → `options_container` | `PortraitObject/Box/OptionsContainer` |
**Renaming any of these nodes WILL break the box. Update the paths to the presenters before renaming**

## Starting dialogue

`DialogueManager` has two functions:

|Function|What it does|
|---|---|
|`start_dialogue(node_id: String, project: YarnProjectResource = null)`|Opens a box and starts the Yarn node called `node_id`. Pass a `project` only to use a different Yarn project than the default.|
|`is_dialogue_active() -> bool`|`true` while a box is open.|

Example: starting dialogue when the player talks to someone:

``` gdscript
func interact() -> void:
	DialogueManager.start_dialogue("Start")
```

Example, stopping the player from moving during dialogue:

``` gdscript
func _physics_process(delta: float) -> void:	
	if DialogueManager.is_dialogue_active():
		return	
	# movement code
```

`start_dialogue` does nothing if:

- a box is already open, or
- a box closed less than **200 ms** ago (see Flow).
## Flow

What happens from the call to the box disappearing:

1. Something calls `DialogueManager.start_dialogue("Start")`.

2. The manager checks that no box is open and that the last one closed more than 200 ms ago. If either check fails, it stops here.
   
3. The manager creates a new `DialogueBox`, adds it as its own child and calls `start()` on it.
   
4. The box tells `YarnDialogueRunner` to start the node.
   
5. For each line, the runner sends the line to `YarnLinePresenter`, which types it out at 15 words per second. Pressing Z while it types finishes the line. Pressing Z again goes to the next line.
   
6. At the same time, the box hears the line presenter's `line_started` signal and tells PortraitObject who is talking, so it can show the right portrait and name. (See **OBJ PortraitObject**.)
   
7. For choices, the runner sends them to `YarnOptionsPresenter`, which creates one option button per choice. The player moves with up/down and picks with Z.
   
8. When the Yarn node ends, the runner fires `dialogue_completed` (or `dialogue_cancelled` on an error). The box deletes itself with `queue_free()`.
   
9. The manager notices the box left the tree, clears its reference and remembers the time.

**Why are we waiting for 200 ms:** the same Z press that closes the last line could also count as "talk to the NPC" and open the dialogue again straight away. The short wait stops that.

**Why a new box every time:** every conversation starts clean, with no leftover text or buttons from the last one.

## Input

| Action            | Key     | Used for                                          |
| ----------------- | ------- | ------------------------------------------------- |
| `dialogue_accept` | Z       | Finish typing, go to the next line, pick a choice |
| `move_up`         | Up , W  | Previous choice                                   |
| `move_down`       | Down, S | Next choice                                       |

- `dialogue_accept` is set as the line presenter's `continue_action` and is used by the option button script.
- It's a separate action from `ui_accept` because other menus use `ui_accept` (Enter/Space), and dialogue shouldn't react to those.
- The mouse is ignored: the line presenter has `click_anywhere_to_continue` off, and the option buttons have their mouse filter set to Ignore.
- The option button (`dialogue_option_button.gd`) reads the input itself: Z presses the focused button, and up/down move focus to the button above or below.

## Writing dialogue

Dialogue lives in `.yarn` files inside `dialogue/`. for example referring to `test_talk.yarn`:

```
title: Start
---
Dyno: Wazzap?!
Dyno: Do you have some kind of explanation.
-> I opened an exe file.    
	<<jump Beginning_Truth>>
-> I walked right on in.    
	<<jump Beginning_Lie>>
===
```

- `title:` is the node name. This is what you pass to `start_dialogue`.
- `Name: text` is one line. The name before the colon is the speaker. It must match a character's `yarn_name` for the portrait and name tag to show (see **OBJ PortraitObject**).
- `->` is a choice. The indented lines under it run when it's picked.
- `<<jump NodeName>>` goes to another node.
- `===` ends the node.

For everything else, see the [Yarn Spinner docs](https://docs.yarnspinner.dev/).

## Testing

1. Open dialogue/dialogue_test.tscn and run it with.
2. Dialogue starts on its own from the node set in the test node's `start_node`.
3. When it ends, press **T** to start it again.
