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
{
///
/// 播放指定 NPC 的动画。
///
/// 动画名称
/// 目标 NPC 的名字或 Story ID。如果不填,则依次使用当前发言角色和对话发起人
[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);
}
///
/// 停止指定 NPC 的动画,使其恢复到默认动作。
///
/// 目标 NPC 的名字或 Story ID。
[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();
}
///
/// 内部工具函数:根据参数智能匹配活跃 NPC 实体。
///
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;
}
}
}