using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// NPC交互,目前只有对话功能。所以只做对话功能。
///
[RequireComponent(typeof(Collider))]
public class NpcInteract : InteractableObj
{
[SerializeField]
protected Npc_SO npc_SO;//根据读取的NpcSO来自动生成Npc
[SerializeField]
protected Transform pivotTrans;//名字支点Trans,需手动赋值
protected Animator anim;
public Npc_SO Npc_SO { get { return npc_SO; } }
//Npc属性文件
protected Dialog_SO dialog_SO;
public void Awake()
{
if (npc_SO == null)
{
Destroy(gameObject);
return;
}
//读取SO。设置Npc实体的各项属性
dialog_SO = npc_SO.dialog_SO;
//播放指定动画
anim = GetComponentInChildren();
if (anim)
anim.CrossFade(npc_SO.PlayAnimName, .2f);
if (pivotTrans!=null)
{
Text textUI = GetComponentInChildren();
if (textUI!=null)
textUI.text = npc_SO.NpcName;
}
}
private void OnEnable()
{
if (anim)
anim.CrossFade(npc_SO.PlayAnimName,.2f);
}
protected void Update()
{
HandleNamePivot();
}
//覆盖父类的方法,UI调用接口的交互方法后调用
protected override void Interact(PlayerManager playerManager)
{
base.Interact(playerManager);
if (dialog_SO!=null)
{
UIManager.GetInstance().dialogPanel.StartADialog(dialog_SO, 0);
//事件中心:交互NPC
//TODO: 比较临时工的行为
EventManager.GetInstance().EventTrigger("交互_"+npc_SO.NpcName);
}
else
{
Debug.Log("该NPC未配置对话文件");
}
}
//处理显示名字的Pivot
protected void HandleNamePivot()
{
if (pivotTrans == null) return;
//检测与相机的距离
//TODO:常量设置
if (Vector3.Distance(Camera.main.transform.position, transform.position) < 15)
{
pivotTrans.gameObject.SetActive(true);
pivotTrans.LookAt(Camera.main.transform.position);
}
else
{
pivotTrans.gameObject.SetActive(false);
}
}
}
/*
public class NpcInteract : InteractableObj
{
[HideInInspector]
public Animator anim;
public string defaultAnim;//NPC默认初始播放的动画
private void Start()
{
//播放指定动画
anim = GetComponentInChildren();
anim.Play(defaultAnim);
}
public override void Interact(PlayerManager playerManager)
{
base.Interact(playerManager);
Debug.Log("交互");
}
}
*/