mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-27 03:44:54 +00:00
90 lines
2.5 KiB
C#
90 lines
2.5 KiB
C#
|
using HPJ.Presentation.Agents;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace HPJ.Presentation
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Displays a line of the Navigation Agents Remaining Path
|
||
|
/// </summary>
|
||
|
[RequireComponent(typeof(LineRenderer))]
|
||
|
[RequireComponent(typeof(NavigationAgent))]
|
||
|
public class AgentLine : MonoBehaviour
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// How Often the line is updated
|
||
|
/// </summary>
|
||
|
[SerializeField]
|
||
|
private float _updateInterval = 0.1f;
|
||
|
private float _updateTime = 0;
|
||
|
|
||
|
private LineRenderer _line;
|
||
|
private NavigationAgent _agent;
|
||
|
private List<Vector3> _linePoints;
|
||
|
|
||
|
private void Awake()
|
||
|
{
|
||
|
_agent = GetComponent<NavigationAgent>();
|
||
|
_line = GetComponent<LineRenderer>();
|
||
|
_linePoints = new List<Vector3>();
|
||
|
}
|
||
|
|
||
|
// Update is called once per frame
|
||
|
void Update()
|
||
|
{
|
||
|
_updateTime += Time.deltaTime;
|
||
|
if (_updateTime > _updateInterval)
|
||
|
{
|
||
|
_updateTime = 0;
|
||
|
UpdateLine();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Updates the Line Renderer to the navigation agent remaining path
|
||
|
/// </summary>
|
||
|
public void UpdateLine()
|
||
|
{
|
||
|
_linePoints.Clear();
|
||
|
|
||
|
if (_agent.Path.Count <= 0)
|
||
|
{
|
||
|
_line.positionCount = 0;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (_agent.CurrentPathingIndex + 1 >= _agent.Path.Count)
|
||
|
{
|
||
|
Vector3 EndPoint = _agent.Path[_agent.Path.Count - 1];
|
||
|
_linePoints.Add(EndPoint);
|
||
|
|
||
|
EndPoint.x = transform.position.x;
|
||
|
EndPoint.z = transform.position.z;
|
||
|
_linePoints.Add(EndPoint);
|
||
|
|
||
|
_line.positionCount = 2;
|
||
|
_line.SetPositions(_linePoints.ToArray());
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Vector3 StartPoint = _agent.Path[_agent.CurrentPathingIndex + 1];
|
||
|
StartPoint.x = transform.position.x;
|
||
|
StartPoint.z = transform.position.z;
|
||
|
_linePoints.Add(StartPoint);
|
||
|
int PathCount = 1;
|
||
|
|
||
|
|
||
|
for(int i = _agent.CurrentPathingIndex + 1; i < _agent.Path.Count; i++)
|
||
|
{
|
||
|
PathCount++;
|
||
|
Vector3 NextPoint = _agent.Path[i];
|
||
|
_linePoints.Add(NextPoint);
|
||
|
}
|
||
|
|
||
|
_line.positionCount = PathCount;
|
||
|
_line.SetPositions(_linePoints.ToArray());
|
||
|
}
|
||
|
}
|
||
|
}
|