Files
real-shooter/Assets/Scripts/Character/MovementController.cs
2022-05-16 17:59:47 +07:00

86 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AI;
using Random = UnityEngine.Random;
[RequireComponent(typeof(NavMeshAgent))]
public class MovementController : MonoBehaviour
{
private Transform _firePointTransform;
public int PointStartID { get; set; }
public int PointEndID { get; private set; }
public float FlagDistance { get; private set; }
private const float UpdateFlagPositionDelay = 5;
private const float UpdateReachedDestinationDelay = 5;
[SerializeField] private NavMeshAgent navMeshAgent;
[SerializeField] private GameObject flag;
public float DistanceToGo { get; private set; }
public float RemainingDistance => navMeshAgent.remainingDistance;
public Vector3 Velocity => navMeshAgent.velocity;
private Dictionary<int, NavPoint> _idNavPointDict;
private void Awake()
{
navMeshAgent.speed = SettingsReader.Instance.GetSettings.MovementSpeed;
_idNavPointDict = MapManager.Instance.IDToNavPoint;
_firePointTransform = transform.GetChild(0);
InvokeRepeating(nameof(UpdateFlagPosition), 0, UpdateFlagPositionDelay);
InvokeRepeating(nameof(ReachedDestination), 0, UpdateReachedDestinationDelay);
}
private void OnDestroy()
{
CancelInvoke(nameof(UpdateFlagPosition));
CancelInvoke(nameof(ReachedDestination));
}
private void UpdateFlagPosition()
{
FlagDistance = (flag.transform.position - gameObject.transform.position).magnitude;
}
public void MoveToRandomPoint()
{
GoToNextNavPoint(MapManager.Instance.NavPoints[Random.Range(0, MapManager.Instance.NavPoints.Count)]);
}
public List<NavPoint> GetPointsCandidate()
{
return MapManager.Instance.NavPoints
.Where(point =>
(transform.position - point.Position).magnitude < SettingsReader.Instance.GetSettings.MovementDistance)
.ToList();
}
public void GoToNextNavPoint(NavPoint destination)
{
if (navMeshAgent.isStopped == true) navMeshAgent.isStopped = false;
PointStartID = PointEndID;
PointEndID = destination.PointId;
navMeshAgent.SetDestination(destination.Position);
DistanceToGo = navMeshAgent.remainingDistance;
}
public void ReturnToStartPoint()
{
if (navMeshAgent.isStopped == true) navMeshAgent.isStopped = false;
navMeshAgent.SetDestination(_idNavPointDict[PointStartID].Position);
(PointEndID, PointStartID) = (PointStartID, PointEndID);
}
public void StopOnPath()
{
navMeshAgent.isStopped = true;
}
public void ReachedDestination()
{
if ((navMeshAgent.isStopped == false) && (navMeshAgent.velocity.magnitude < 0.1))
PointStartID = PointEndID;
}
}