Npc移动逻辑

This commit is contained in:
huyulong 2024-12-12 14:50:53 +08:00
parent 7217b04390
commit 0fc572148d
4 changed files with 79 additions and 19 deletions

View File

@ -79,6 +79,13 @@ public class TaskItem : MonoBehaviour
triggers.Add(triggerType);
}
// 任务完成时调用的函数
public void OnTaskCompleted()
{
TaskPanel.instance.RemoveTask(taskId);
}
// Update is called once per frame
void Update()
{

View File

@ -9,7 +9,7 @@ using UnityEditor;
[InitializeOnLoad]
public class EncryptionDongleClient : MonoBehaviour
{
public static string licenseID = "Ïû·Àid";
public static string licenseID = "xiaofangid";
private static string serverURL = "http://shu.sheziwanglo.cn:5001/validate";
static EncryptionDongleClient()

View File

@ -12,7 +12,7 @@ public class TaskPanel : Base
public static TaskPanel instance;
public List<int> taskId = new List<int>();
public List<int> taskIds = new List<int>();
public Transform contentTrans;
@ -73,12 +73,12 @@ public class TaskPanel : Base
public async void InitTask()
{
await DestroyTaskAsync();
for(int i = 0; i < taskId.Count;i++)
for(int i = 0; i < taskIds.Count;i++)
{
GameObject go = GameObject.Instantiate(taskPrefab, contentTrans);
go.transform.name = "Task_" + i;
TaskItem item = go.GetComponent<TaskItem>();
item.SetInfo(taskId[i], JSONReader);
item.SetInfo(taskIds[i], JSONReader);
Button button = go.GetComponent<Button>();
button.onClick.AddListener(() => OnButtonClicked(button));
buttons.Add(button);
@ -113,7 +113,7 @@ public class TaskPanel : Base
await Task.Delay(10);
}
//这里是在加入新任务时需要更新显示前的删除
public void DeleteAllChildObjects()
{
// ±éÀúËùÓÐ×ÓÎïÌ岢ɾ³ý
@ -129,13 +129,25 @@ public class TaskPanel : Base
{
taskId.Add(id);
taskIds.Add(id);
InitTask();
}
public void RemoveTask(int taskId)
{
// 通过 taskId 找到对应的任务按钮并移除
Button taskButton = buttons.Find(button => button.name == "Task_" + taskId);
if (taskButton != null)
{
buttons.Remove(taskButton);
Destroy(taskButton.gameObject); // 删除该任务按钮
// 如果任务完成后有其他特定处理,比如从任务 ID 列表中移除任务,也可以在这里做
taskIds.Remove(taskId);
}
}

View File

@ -24,6 +24,12 @@ public class RecuseNpc : MonoBehaviour
private bool movebool = false;
public Transform target;
public Vector3 currentTarget;
// 存储目标点的List
public List<Vector3> targetPoints = new List<Vector3>();
private void Awake()
{
instance = this;
@ -45,6 +51,7 @@ public class RecuseNpc : MonoBehaviour
recusebtn.gameObject.SetActive(false);
}
//设置NPC的状态
public void Setnpcstate()//点击救援按钮执行完动作后对按钮进行隐藏
{
@ -54,29 +61,49 @@ public class RecuseNpc : MonoBehaviour
Debug.Log("Setnpcstate调用");
}
//设置NPC的目标点
public void SetNpcDes(Vector3 tar)
{
target.position = tar;
//target.position = tar;
targetPoints.Add(tar); // 将目标点添加到列表中
}
private void Update()
{
if (Vector3.Distance(transform.position,target.position) < 1.3f && movebool && target.transform.position != null)//判断人物被救援后移动到目标位置
if (targetPoints.Count > 0) // 确保列表中有目标点
{
Debug.Log("到达目标点");
nstate = Npcstate.idle;
movebool = false;
}
currentTarget = targetPoints[0]; // 获取当前的目标点
switch (nstate)//通过枚举状态实现人物是否被救援,以及动作的改变
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();
Run(currentTarget);
break;
case Npcstate.dead:
SetAni(0);
@ -84,14 +111,28 @@ public class RecuseNpc : MonoBehaviour
}
}
//删除目标点
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()
public void Run(Vector3 target)
{
if (movebool)
{
SetAni(1);
transform.LookAt(target);
transform.position = Vector3.Lerp(transform.position, target.position, 0.3f * Time.deltaTime);
transform.position = Vector3.Lerp(transform.position, target, 0.3f * Time.deltaTime);
}
}