_xiaofang/xiaofang/Assets/Script/npc/RecuseNpc.cs
2024-12-12 14:50:53 +08:00

147 lines
3.2 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.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public enum Npcstate
{
idle,
dead,
run
}
public class RecuseNpc : MonoBehaviour
{
public static RecuseNpc instance;
private Button recusebtn;
private bool statebool = false;
private Animator anim;
public Npcstate nstate = Npcstate.dead;
private bool movebool = false;
public Transform target;
public Vector3 currentTarget;
// 存储目标点的List
public List<Vector3> targetPoints = new List<Vector3>();
private void Awake()
{
instance = this;
recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>();
anim = this.GetComponent<Animator>();
}
private void OnTriggerEnter(Collider other)//通过触发器得到对应的救援组人员标签是否显现出UI
{
if(other.tag == "Player")
recusebtn.gameObject.SetActive(true);
if (statebool) return;
other.GetComponent<CharacterInturn>().cha = this.gameObject ;
}
private void OnTriggerExit(Collider other)
{
recusebtn.gameObject.SetActive(false);
}
//设置NPC的状态
public void Setnpcstate()//点击救援按钮执行完动作后对按钮进行隐藏
{
movebool = true;
nstate = Npcstate.run;
recusebtn.gameObject.SetActive(false);
Debug.Log("Setnpcstate调用");
}
//设置NPC的目标点
public void SetNpcDes(Vector3 tar)
{
//target.position = tar;
targetPoints.Add(tar); // 将目标点添加到列表中
}
private void Update()
{
if (targetPoints.Count > 0) // 确保列表中有目标点
{
currentTarget = targetPoints[0]; // 获取当前的目标点
if (Vector3.Distance(transform.position, currentTarget) < 1.3f && movebool && currentTarget != null) // 判断人物被救援后移动到目标位置
{
Debug.Log("到达目标点");
// 达到目标后,从列表中移除该目标点
if(targetPoints.Count > 1)
{
targetPoints = RemoveDes();
}
else
{
targetPoints = null;
}
// 设置NPC状态
nstate = Npcstate.idle;
movebool = false;
}
}
// 继续处理NPC的状态和动画
switch (nstate)
{
case Npcstate.idle:
SetAni(2);
break;
case Npcstate.run:
movebool = true;
Run(currentTarget);
break;
case Npcstate.dead:
SetAni(0);
break;
}
}
//删除目标点
public List<Vector3> RemoveDes()
{
List<Vector3> list = new List<Vector3>();
for(int i= 1;i<targetPoints.Count;i++)
{
list[i-1] = targetPoints[i];
}
return list;
}
//跑步逻辑
public void Run(Vector3 target)
{
if (movebool)
{
SetAni(1);
transform.LookAt(target);
transform.position = Vector3.Lerp(transform.position, target, 0.3f * Time.deltaTime);
}
}
public void SetAni(int x)
{
anim.SetInteger("state", x);
}
}