_xiaofang/xiaofang/Assets/Script/npc/RecuseNpc.cs

154 lines
3.3 KiB
C#
Raw Normal View History

2024-10-21 18:09:07 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2024-10-22 11:57:50 +08:00
2024-10-22 17:37:42 +08:00
public enum Npcstate
{
idle,
dead,
2024-11-29 15:13:32 +08:00
run
2024-10-22 11:57:50 +08:00
}
2024-10-21 18:09:07 +08:00
public class RecuseNpc : MonoBehaviour
{
2024-12-12 18:51:08 +08:00
public string npcId;
2024-12-09 09:46:52 +08:00
public static RecuseNpc instance;
2024-10-21 18:09:07 +08:00
private Button recusebtn;
2024-10-22 11:57:50 +08:00
2024-10-22 17:37:42 +08:00
private bool statebool = false;
private Animator anim;
2024-10-22 11:57:50 +08:00
2024-11-29 15:13:32 +08:00
public Npcstate nstate = Npcstate.dead;
2024-10-22 11:57:50 +08:00
2024-10-22 17:37:42 +08:00
private bool movebool = false;
public Transform target;
2024-12-12 14:50:53 +08:00
public Vector3 currentTarget;
// <20>洢Ŀ<E6B4A2><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>List
public List<Vector3> targetPoints = new List<Vector3>();
2024-10-22 11:57:50 +08:00
private void Awake()
2024-10-21 18:09:07 +08:00
{
2024-12-09 09:46:52 +08:00
instance = this;
2024-10-21 18:09:07 +08:00
recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>();
2024-10-22 17:37:42 +08:00
anim = this.GetComponent<Animator>();
2024-10-21 18:09:07 +08:00
}
2024-10-22 17:37:42 +08:00
private void OnTriggerEnter(Collider other)//ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>Ӧ<EFBFBD>ľ<EFBFBD>Ԯ<EFBFBD><D4AE><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1>ǩ<EFBFBD><C7A9><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ֳ<EFBFBD>UI
2024-10-21 18:09:07 +08:00
{
2024-10-24 18:01:55 +08:00
if(other.tag == "Player")
2024-10-21 18:09:07 +08:00
recusebtn.gameObject.SetActive(true);
2024-10-22 11:57:50 +08:00
if (statebool) return;
2024-10-24 12:46:20 +08:00
other.GetComponent<CharacterInturn>().cha = this.gameObject ;
2024-10-22 11:57:50 +08:00
}
2024-10-21 18:09:07 +08:00
2024-10-23 11:31:35 +08:00
private void OnTriggerExit(Collider other)
{
recusebtn.gameObject.SetActive(false);
}
2024-12-12 18:51:08 +08:00
public void SetNPCInfo(string id)
{
npcId = id;
}
2024-12-12 14:50:53 +08:00
//<2F><><EFBFBD><EFBFBD>NPC<50><43>״̬
2024-10-22 17:37:42 +08:00
public void Setnpcstate()//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԯ<EFBFBD><D4AE>ťִ<C5A5><D6B4><EFBFBD><EFBFBD><EAB6AF><EFBFBD><EFBFBD><EFBFBD>԰<EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
2024-11-29 15:13:32 +08:00
2024-10-22 17:37:42 +08:00
movebool = true;
2024-11-29 15:13:32 +08:00
nstate = Npcstate.run;
2024-10-22 17:37:42 +08:00
recusebtn.gameObject.SetActive(false);
2024-12-09 09:46:52 +08:00
Debug.Log("Setnpcstate<74><65><EFBFBD><EFBFBD>");
}
2024-12-12 14:50:53 +08:00
//<2F><><EFBFBD><EFBFBD>NPC<50><43>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>
2024-12-09 11:30:15 +08:00
public void SetNpcDes(Vector3 tar)
2024-12-09 09:46:52 +08:00
{
2024-12-12 14:50:53 +08:00
//target.position = tar;
targetPoints.Add(tar); // <20><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5>б<EFBFBD><D0B1><EFBFBD>
2024-10-22 17:37:42 +08:00
}
private void Update()
2024-10-22 11:57:50 +08:00
{
2024-12-12 14:50:53 +08:00
if (targetPoints.Count > 0) // ȷ<><C8B7><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>
2024-10-22 17:37:42 +08:00
{
2024-12-12 14:50:53 +08:00
currentTarget = targetPoints[0]; // <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>
if (Vector3.Distance(transform.position, currentTarget) < 1.3f && movebool && currentTarget != null) // <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFB1BB>Ԯ<EFBFBD><D4AE><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB>
{
Debug.Log("<22><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>");
// <20>ﵽĿ<EFB5BD><C4BF><EFBFBD>󣬴<EFBFBD><F3A3ACB4>б<EFBFBD><D0B1><EFBFBD><EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>
if(targetPoints.Count > 1)
{
targetPoints = RemoveDes();
}
else
{
targetPoints = null;
}
// <20><><EFBFBD><EFBFBD>NPC״̬
nstate = Npcstate.idle;
movebool = false;
}
2024-10-22 17:37:42 +08:00
}
2024-12-12 14:50:53 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>NPC<50><43>״̬<D7B4>Ͷ<EFBFBD><CDB6><EFBFBD>
switch (nstate)
2024-10-22 17:37:42 +08:00
{
case Npcstate.idle:
2024-11-29 15:13:32 +08:00
SetAni(2);
2024-10-22 17:37:42 +08:00
break;
2024-11-29 15:13:32 +08:00
case Npcstate.run:
movebool = true;
2024-12-12 14:50:53 +08:00
Run(currentTarget);
2024-10-22 17:37:42 +08:00
break;
case Npcstate.dead:
2024-11-29 15:13:32 +08:00
SetAni(0);
2024-10-22 17:37:42 +08:00
break;
}
2024-11-29 15:13:32 +08:00
}
2024-12-12 14:50:53 +08:00
//ɾ<><C9BE>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>
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;
}
2024-11-29 15:13:32 +08:00
//<2F>ܲ<EFBFBD><DCB2>߼<EFBFBD>
2024-12-12 14:50:53 +08:00
public void Run(Vector3 target)
2024-11-29 15:13:32 +08:00
{
if (movebool)
{
SetAni(1);
transform.LookAt(target);
2024-12-12 14:50:53 +08:00
transform.position = Vector3.Lerp(transform.position, target, 0.3f * Time.deltaTime);
2024-11-29 15:13:32 +08:00
}
}
2024-10-22 17:37:42 +08:00
2024-11-29 15:13:32 +08:00
public void SetAni(int x)
{
anim.SetInteger("state", x);
2024-10-22 11:57:50 +08:00
}
2024-10-21 18:09:07 +08:00
2024-10-22 17:37:42 +08:00
2024-10-21 18:09:07 +08:00
}