69 lines
2.2 KiB
C#
69 lines
2.2 KiB
C#
![]() |
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using BITKit;
|
||
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
using NodeCanvas.Framework;
|
||
|
using Project.B.CharacterController;
|
||
|
using Project.B.Entities;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.AI;
|
||
|
|
||
|
namespace Net.Project.B.Quest
|
||
|
{
|
||
|
public class GuidePlayerToPosition : ActionTask<Transform>
|
||
|
{
|
||
|
public BBParameter<LineRenderer> LineRenderer;
|
||
|
|
||
|
private ICharacterController _characterController;
|
||
|
private NavMeshAgent _agent;
|
||
|
private readonly NavMeshPath _navMeshPath = new();
|
||
|
|
||
|
protected override string info =>
|
||
|
_characterController is null ? "未初始化" : $"状态:{_navMeshPath.status.ToString()}";
|
||
|
|
||
|
protected override string OnInit()
|
||
|
{
|
||
|
LineRenderer.value.enabled = false;
|
||
|
return base.OnInit();
|
||
|
}
|
||
|
|
||
|
protected override void OnExecute()
|
||
|
{
|
||
|
LineRenderer.value.enabled = true;
|
||
|
var playerFactory = BITApp.ServiceProvider.GetService<IPlayerFactory>();
|
||
|
var player = playerFactory.Entities.First().Value;
|
||
|
_characterController=player.ServiceProvider.GetRequiredService<ICharacterController>();
|
||
|
}
|
||
|
|
||
|
protected override void OnUpdate()
|
||
|
{
|
||
|
if (NavMesh.CalculatePath(_characterController.Position, agent.position, NavMesh.AllAreas, _navMeshPath))
|
||
|
{
|
||
|
DrawPath(_navMeshPath);
|
||
|
}
|
||
|
if(Vector3.Distance(_characterController.Position,agent.position)<1f)
|
||
|
{
|
||
|
EndAction(true);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected override void OnStop(bool interrupted)
|
||
|
{
|
||
|
base.OnStop(interrupted);
|
||
|
LineRenderer.value.enabled = false;
|
||
|
}
|
||
|
|
||
|
private void DrawPath(NavMeshPath path)
|
||
|
{
|
||
|
LineRenderer.value.positionCount = path.corners.Length;
|
||
|
var newPath = path.corners;
|
||
|
for (var i = 0; i < path.corners.Length; i++)
|
||
|
{
|
||
|
newPath[i] = new Vector3(path.corners[i].x, path.corners[i].y + 0.1f, path.corners[i].z);
|
||
|
}
|
||
|
LineRenderer.value.SetPositions(newPath);
|
||
|
}
|
||
|
}
|
||
|
}
|