48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using System.Linq;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
using System.Threading.Tasks;
|
|
|
|
[RequireComponent(typeof(NavMeshAgent))]
|
|
public class MovementController : MonoBehaviour
|
|
{
|
|
public NavPoint CurrentNavPoint { get; set; }
|
|
public float FlagDistance { get; private set; }
|
|
private GameObject flag;
|
|
private const float updateFlagPositionDelay = 5;
|
|
[SerializeField] private NavMeshAgent navMeshAgent;
|
|
|
|
private void Start()
|
|
{
|
|
navMeshAgent.speed = SettingsReader.Instance.GetSettings.MovementSpeed;
|
|
InvokeRepeating(nameof(UpdateFlagPosition), 0, updateFlagPositionDelay);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
CancelInvoke(nameof(UpdateFlagPosition));
|
|
}
|
|
|
|
private void UpdateFlagPosition()
|
|
{
|
|
FlagDistance = (flag.transform.position - gameObject.transform.position).magnitude;
|
|
}
|
|
|
|
public void MoveToRandomPoint()
|
|
{
|
|
Debug.Log(MapManager.navPoints == null);
|
|
goToNextNavPoint(MapManager.navPoints[Random.Range(0, MapManager.navPoints.Count)]);
|
|
}
|
|
|
|
public List<NavPoint> getPointsCandidate()
|
|
{
|
|
return MapManager.navPoints
|
|
.Where(point => (CurrentNavPoint.position - point.position).magnitude < SettingsReader.Instance.GetSettings.MovementSpeed)
|
|
.ToList();
|
|
}
|
|
|
|
public void goToNextNavPoint(NavPoint destination) =>
|
|
navMeshAgent.SetDestination(destination.position);
|
|
}
|