replace the NPC scaffold with data-driven prefabs, and retire the builders
NPCStandIn was written to be dropped on a sphere and carried its identity in per-instance public fields. The cost of that showed up in the scene itself: of the two spheres, one was the Bartender and the other was named "Bartender (1)" in the hierarchy while being configured as the Chair — data changed, name never was. Nothing outside the scene could see what existed, and two copies could disagree. Identity now lives in an InteractableDefinition asset and the scene only places a prefab. The migration matched the old objects by yarn node rather than by name, which is the only reason the chair ended up as the chair. InteractableDefinition rather than the planned "character definition": it covers a character and a talkable object equally, so Bartender.prefab sits under Prefabs/Characters and Chair.prefab under Prefabs/Interactables. Three components collapse into one. DialogueInteractable was in no scene at all — a dead trigger-zone variant of the same idea. ClickInteractableBridge existed only to adapt NPCStandIn without modifying it, a constraint that died with NPCStandIn; DialogueInteractor implements IClickInteractable itself. The parts worth keeping were lifted out rather than discarded: WorldSpaceLabel and InteractionPrompt are now normal components on prefab children, so a label can be seen and positioned in the Editor instead of existing only at runtime. InputDeviceTracker moved into NightclubArcadia.Core and gained a namespace, leaving no global-namespace runtime types. Characters get the same pipeline as Skills and Candidates: Assets/Characters/characters.json is the source of truth, CharacterSetup regenerates the definition assets on script reload and removes orphans. Adding an NPC is a JSON entry plus a prefab — no code, and no Unity for the data half. Phase 5.4 deletes the one-shot builders rather than disabling them. UILayerSetup, PlayerControlSetup, UILayerSetupAutoRun and YarnDemoSetup assumed the single-scene layout, had already destroyed a level scene and turned Systems binary once each, and after NPCStandIn went they no longer compiled. The prefabs they used to generate are authored and committed; the wiring knowledge is in those prefabs and in git history. The asset generators — Skill, Candidate, Character — are untouched and still work. EditMode 42/42, PlayMode 7/7, YarnCheck 9 files / 35 nodes. All three scenes still text. Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace NightclubArcadia.Interaction
|
||||
{
|
||||
/// <summary>
|
||||
/// Adapts NPCStandIn to the click-to-interact pipeline without modifying
|
||||
/// the existing proximity + keypress path.
|
||||
/// </summary>
|
||||
public sealed class ClickInteractableBridge : MonoBehaviour, IClickInteractable
|
||||
{
|
||||
[SerializeField] NPCStandIn npc;
|
||||
|
||||
public Transform ApproachPoint => transform;
|
||||
public float ApproachRadius => npc != null ? Mathf.Max(0.15f, npc.interactionRange - 0.5f) : 0.15f;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (npc == null)
|
||||
{
|
||||
npc = GetComponent<NPCStandIn>();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnApproached()
|
||||
{
|
||||
npc?.Interact();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5257f07d188de44eb9a04ba4d3a65fec
|
||||
@@ -0,0 +1,170 @@
|
||||
using NightclubArcadia.Core;
|
||||
using NightclubArcadia.Dialogue;
|
||||
using NightclubArcadia.UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using Yarn.Unity;
|
||||
|
||||
namespace NightclubArcadia.Interaction
|
||||
{
|
||||
/// <summary>
|
||||
/// Anything the player can walk up to and talk to: a character, or an object with
|
||||
/// something to say.
|
||||
///
|
||||
/// Replaces NPCStandIn, which was written to be dropped on a sphere primitive and
|
||||
/// carried its identity in per-instance public fields. Two objects could claim the
|
||||
/// same character and disagree, and in practice did — the scene's second "Bartender"
|
||||
/// was the chair, renamed in the data but not in the hierarchy.
|
||||
///
|
||||
/// Identity now comes from an InteractableDefinition asset, so a prefab is the thing
|
||||
/// and the scene only places it. Also replaces DialogueInteractable, a trigger-zone
|
||||
/// variant of the same idea that was in no scene at all.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(SphereCollider))]
|
||||
public sealed class DialogueInteractor : MonoBehaviour, IClickInteractable
|
||||
{
|
||||
[Header("Identity")]
|
||||
[SerializeField] InteractableDefinition definition;
|
||||
|
||||
[Header("Presentation (optional)")]
|
||||
[SerializeField] WorldSpaceLabel label;
|
||||
[SerializeField] InteractionPrompt prompt;
|
||||
|
||||
[Header("Overrides")]
|
||||
[Tooltip("Leave empty to use the Canvas under the DialogueRunner.")]
|
||||
[SerializeField] GameObject dialogueUI;
|
||||
|
||||
DialogueRunner runner;
|
||||
Transform player;
|
||||
InputAction interact;
|
||||
bool playerInRange;
|
||||
|
||||
public Transform ApproachPoint => transform;
|
||||
|
||||
public float ApproachRadius =>
|
||||
definition != null ? Mathf.Max(0.15f, definition.InteractionRange - 0.5f) : 0.15f;
|
||||
|
||||
/// <summary>
|
||||
/// The dialogue runner, resolved on demand and cached only on success.
|
||||
///
|
||||
/// Never resolved once in Awake: the runner lives in the Systems scene and this
|
||||
/// object lives in a level, so it can wake up before the runner exists. Caching a
|
||||
/// failure there left NPCs permanently mute for the session. See SceneOrderTests.
|
||||
/// </summary>
|
||||
public DialogueRunner ResolvedRunner =>
|
||||
runner != null ? runner : runner = SceneServices.TryResolve<DialogueRunner>(null);
|
||||
|
||||
Transform Player =>
|
||||
player != null ? player : player = SceneServices.Player;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (definition == null)
|
||||
{
|
||||
Debug.LogError($"[Interactor] {name} has no InteractableDefinition; it will do nothing.", this);
|
||||
enabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
GetComponent<SphereCollider>().isTrigger = false;
|
||||
|
||||
if (label != null)
|
||||
{
|
||||
label.gameObject.SetActive(definition.ShowLabel);
|
||||
label.SetText(definition.DisplayName);
|
||||
}
|
||||
|
||||
prompt?.SetShown(false);
|
||||
|
||||
interact = new InputAction("Interact");
|
||||
interact.AddBinding("<Keyboard>/f");
|
||||
interact.AddBinding("<Gamepad>/buttonSouth");
|
||||
interact.performed += _ =>
|
||||
{
|
||||
if (playerInRange)
|
||||
{
|
||||
Interact();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void OnEnable() => interact?.Enable();
|
||||
|
||||
void OnDisable() => interact?.Disable();
|
||||
|
||||
void Update()
|
||||
{
|
||||
var target = Player;
|
||||
if (target == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var inRange = Vector3.Distance(transform.position, target.position) <= definition.InteractionRange;
|
||||
if (inRange == playerInRange)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
playerInRange = inRange;
|
||||
prompt?.SetShown(inRange);
|
||||
}
|
||||
|
||||
/// <summary>Entry point for both the proximity keypress and click-to-move arrival.</summary>
|
||||
public void Interact()
|
||||
{
|
||||
var dialogue = ResolvedRunner;
|
||||
if (dialogue == null)
|
||||
{
|
||||
Debug.LogError(
|
||||
$"[Interactor] {definition.DisplayName}: no DialogueRunner in any loaded scene. " +
|
||||
"It lives in Systems.unity — open Bootstrap, or Tools → Nightclub Arcadia → " +
|
||||
"Open Game Scenes.", this);
|
||||
return;
|
||||
}
|
||||
|
||||
if (UIStateController.Instance != null && UIStateController.Instance.IsMenuOpen)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!dialogue.IsDialogueRunning)
|
||||
{
|
||||
StartDialogueAsync(dialogue);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnApproached() => Interact();
|
||||
|
||||
async void StartDialogueAsync(DialogueRunner dialogue)
|
||||
{
|
||||
// The dialogue UI is hidden when the player leaves a conversation via
|
||||
// <<enter_environment>>. Make it visible again before the first line renders.
|
||||
if (DialogueUIVisibility.Show(ResolveDialogueUI(dialogue)))
|
||||
{
|
||||
// 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 dialogue.StartDialogue(definition.YarnNode);
|
||||
}
|
||||
|
||||
GameObject ResolveDialogueUI(DialogueRunner dialogue)
|
||||
{
|
||||
if (dialogueUI != null)
|
||||
{
|
||||
return dialogueUI;
|
||||
}
|
||||
|
||||
var canvas = dialogue.GetComponentInChildren<Canvas>(true);
|
||||
if (canvas == null)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"[Interactor] No Canvas under {dialogue.name} for {definition.DisplayName}", this);
|
||||
return null;
|
||||
}
|
||||
return canvas.gameObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba3ce042c3b3457ba101f1c6643b983f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,46 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace NightclubArcadia.Interaction
|
||||
{
|
||||
/// <summary>
|
||||
/// What a thing in the world is called and which conversation it starts.
|
||||
///
|
||||
/// This replaces a set of public fields edited per scene object. The scaffold it
|
||||
/// grew out of had the name, colour, yarn node, ranges and font sizes all set on
|
||||
/// each sphere by hand, which meant two objects claiming to be the same character
|
||||
/// could disagree, and nothing outside the scene could see what existed.
|
||||
///
|
||||
/// Generated from Assets/Characters/characters.json — edit the JSON, not the asset.
|
||||
/// Hand edits are overwritten, the same contract as Skills and Candidates.
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "Nightclub Arcadia/Interactable Definition", order = 30)]
|
||||
public sealed class InteractableDefinition : ScriptableObject
|
||||
{
|
||||
[SerializeField] string id;
|
||||
[SerializeField] string displayName;
|
||||
[SerializeField] string yarnNode;
|
||||
[SerializeField] float interactionRange = 3f;
|
||||
|
||||
[Tooltip("Shown as a floating label. Off for scenery that should not announce itself.")]
|
||||
[SerializeField] bool showLabel = true;
|
||||
|
||||
public string Id => id;
|
||||
public string DisplayName => displayName;
|
||||
public string YarnNode => yarnNode;
|
||||
public float InteractionRange => interactionRange;
|
||||
public bool ShowLabel => showLabel;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
/// <summary>Editor-only, for the generator. Not part of the runtime surface.</summary>
|
||||
public void EditorSet(string newId, string newDisplayName, string newYarnNode,
|
||||
float newRange, bool newShowLabel)
|
||||
{
|
||||
id = newId;
|
||||
displayName = newDisplayName;
|
||||
yarnNode = newYarnNode;
|
||||
interactionRange = newRange;
|
||||
showLabel = newShowLabel;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8f4df63859ea4e39910d900aa7a5510a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,54 @@
|
||||
using NightclubArcadia.Core;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NightclubArcadia.Interaction
|
||||
{
|
||||
/// <summary>
|
||||
/// The "[F]" that appears when the player is close enough to interact, showing the
|
||||
/// glyph for whichever input device was last used.
|
||||
///
|
||||
/// Separated from the interactor so the prompt can be positioned and styled in a
|
||||
/// prefab rather than constructed in code, and so anything else that needs a
|
||||
/// proximity prompt can reuse it.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(TextMeshPro))]
|
||||
public sealed class InteractionPrompt : MonoBehaviour
|
||||
{
|
||||
TextMeshPro text;
|
||||
bool shown;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
text = GetComponent<TextMeshPro>();
|
||||
SetShown(false);
|
||||
}
|
||||
|
||||
public void SetShown(bool value)
|
||||
{
|
||||
shown = value;
|
||||
if (text != null)
|
||||
{
|
||||
text.enabled = value;
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (shown && text != null)
|
||||
{
|
||||
text.text = GlyphFor(InputDeviceTracker.Current);
|
||||
}
|
||||
}
|
||||
|
||||
static string GlyphFor(InputDeviceTracker.DeviceType type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case InputDeviceTracker.DeviceType.Xbox: return "(X)";
|
||||
case InputDeviceTracker.DeviceType.PlayStation: return "(✕)";
|
||||
default: return "[F]";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10b4e7c1069944b49acca9287528f275
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,54 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NightclubArcadia.Interaction
|
||||
{
|
||||
/// <summary>
|
||||
/// A floating text label that turns to face the camera.
|
||||
///
|
||||
/// Lifted out of NPCStandIn, which created its label in Awake and billboarded it in
|
||||
/// LateUpdate. That worked, but it meant the text only existed at runtime and could
|
||||
/// not be seen or positioned in the Editor. This is a normal component on a normal
|
||||
/// child object, so a prefab can show what it will look like.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(TextMeshPro))]
|
||||
public sealed class WorldSpaceLabel : MonoBehaviour
|
||||
{
|
||||
[SerializeField] bool billboard = true;
|
||||
|
||||
TextMeshPro text;
|
||||
Camera view;
|
||||
|
||||
void Awake() => text = GetComponent<TextMeshPro>();
|
||||
|
||||
public void SetText(string value)
|
||||
{
|
||||
if (text == null)
|
||||
{
|
||||
text = GetComponent<TextMeshPro>();
|
||||
}
|
||||
text.text = value;
|
||||
}
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
if (!billboard)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Camera.main is a tagged lookup; cache it, but re-acquire if the camera was
|
||||
// destroyed with a scene, which happens when the Systems scene reloads.
|
||||
if (view == null)
|
||||
{
|
||||
view = Camera.main;
|
||||
if (view == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
transform.rotation = Quaternion.LookRotation(transform.position - view.transform.position);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5d24ca2949394cc78a1fdde5d83e08c2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user