Files
Cielonos/Assets/Scripts/MainGame/Narrative/CustomFunctions/NPC_CFs.cs
SoulliesOfficial 7bc1e1722c 爆更
2026-06-05 04:21:00 -04:00

73 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using Cielonos.MainGame;
using Cielonos.MainGame.Interactions;
using Cielonos.MainGame.Narrative;
using SLSUtilities.Narrative.UI;
using UnityEngine;
using Yarn.Unity;
namespace Cielonos.Narrative
{
public static partial class CustomFunctions
{
/// <summary>
/// 播放指定 NPC 的动画。
/// </summary>
/// <param name="animationName">动画名称</param>
/// <param name="npcName">目标 NPC 的名字或 Story ID。如果不填则依次使用当前发言角色和对话发起人</param>
[YarnCommand("play_animation")]
public static void PlayAnimation(string animationName, string npcName)
{
NpcBase npc = ResolveNpc(npcName, "play_animation");
Debug.Log($"[play_animation] 解析结果:目标 NPC Key = '{npcName}', 解析到的 NPC 实例 = {(npc != null ? npc.gameObject.name : "null")}");
if (npc == null) return;
if (npc.funcAnimSm == null)
{
Debug.LogError($"[play_animation] NPC '{npc.gameObject.name}' 的 functional animation 子模块 (funcAnimSm) 未初始化!");
return;
}
npc.funcAnimSm.Play(animationName);
}
/// <summary>
/// 停止指定 NPC 的动画,使其恢复到默认动作。
/// </summary>
/// <param name="npcName">目标 NPC 的名字或 Story ID。</param>
[YarnCommand("stop_animation")]
public static void StopAnimation(string npcName)
{
NpcBase npc = ResolveNpc(npcName, "stop_animation");
if (npc == null) return;
if (npc.funcAnimSm == null)
{
Debug.LogError($"[stop_animation] NPC '{npc.gameObject.name}' 的 functional animation 子模块 (funcAnimSm) 未初始化!");
return;
}
npc.funcAnimSm.Stop();
}
/// <summary>
/// 内部工具函数:根据参数智能匹配活跃 NPC 实体。
/// </summary>
private static NpcBase ResolveNpc(string npcName, string commandName)
{
if (string.IsNullOrEmpty(npcName))
{
Debug.LogWarning($"[{commandName}] 无法解析目标 NPC未指定 npcName。");
return null;
}
if (StoryDirector.ActiveNpcs.TryGetValue(npcName, out NpcBase npc) && npc != null)
{
return npc;
}
Debug.LogWarning($"[{commandName}] 无法定位 NPC在 ActiveNpcs 中未找到 Key 为 '{npcName}' 的活跃 NPC。");
return null;
}
}
}