NpcInteract.cs 2.7 KB
Newer Older
秃头给你一拳's avatar
秃头给你一拳 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


/// <summary>
/// NPC交互,目前只有对话功能。所以只做对话功能。
/// </summary>
[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()
    {
秃头给你一拳's avatar
Beta  
秃头给你一拳 已提交
27 28 29 30 31 32
        if (npc_SO == null)
        {
            Destroy(gameObject);
            return;
        }

秃头给你一拳's avatar
秃头给你一拳 已提交
33 34 35 36 37
        //读取SO。设置Npc实体的各项属性
        dialog_SO = npc_SO.dialog_SO;

        //播放指定动画
        anim = GetComponentInChildren<Animator>();
秃头给你一拳's avatar
Beta  
秃头给你一拳 已提交
38 39
        if (anim)
            anim.CrossFade(npc_SO.PlayAnimName, .2f);
秃头给你一拳's avatar
秃头给你一拳 已提交
40 41 42 43

        if (pivotTrans!=null)
        {
            Text textUI = GetComponentInChildren<Text>();
秃头给你一拳's avatar
Beta  
秃头给你一拳 已提交
44 45
            if (textUI!=null)
                textUI.text = npc_SO.NpcName;
秃头给你一拳's avatar
秃头给你一拳 已提交
46 47 48 49 50
        }
    }

    private void OnEnable()
    {
秃头给你一拳's avatar
Beta  
秃头给你一拳 已提交
51 52
        if (anim)
            anim.CrossFade(npc_SO.PlayAnimName,.2f);
秃头给你一拳's avatar
秃头给你一拳 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    }

    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
秃头给你一拳's avatar
Beta  
秃头给你一拳 已提交
70 71 72
            //TODO: 比较临时工的行为
            EventManager.GetInstance().EventTrigger("交互_"+npc_SO.NpcName);
            
秃头给你一拳's avatar
秃头给你一拳 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        }
        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<Animator>();
        anim.Play(defaultAnim);
    }
    public override void Interact(PlayerManager playerManager)
    {
        base.Interact(playerManager);

        Debug.Log("交互");
    }
}
*/