using HPJ.Presentation.Agents; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace HPJ.Presentation { /// /// Displays a line of the Navigation Agents Remaining Path /// [RequireComponent(typeof(LineRenderer))] [RequireComponent(typeof(NavigationAgent))] public class AgentLine : MonoBehaviour { /// /// How Often the line is updated /// [SerializeField] private float _updateInterval = 0.1f; private float _updateTime = 0; private LineRenderer _line; private NavigationAgent _agent; private List _linePoints; private void Awake() { _agent = GetComponent(); _line = GetComponent(); _linePoints = new List(); } // Update is called once per frame void Update() { _updateTime += Time.deltaTime; if (_updateTime > _updateInterval) { _updateTime = 0; UpdateLine(); } } /// /// Updates the Line Renderer to the navigation agent remaining path /// 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()); } } }