to new git

This commit is contained in:
2022-05-04 23:50:07 +07:00
parent 290f5515b7
commit c8af0e5284
39 changed files with 720 additions and 359 deletions

View File

@ -1,24 +1,32 @@
using System.Linq;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AI;
using System.Threading.Tasks;
[RequireComponent(typeof(NavMeshAgent))]
public class MovementController : MonoBehaviour
{
public NavPoint CurrentNavPoint { get; set; }
public int PointStartID { get; set; }
public int PointEndID { get; private set; }
public float FlagDistance { get; private set; }
private GameObject flag;
private const float updateFlagPositionDelay = 5;
[SerializeField] private NavMeshAgent navMeshAgent;
private const float updateFlagPositionDelay = 5;
private const float updateReachedDestinationDelay = 5;
private void Start()
[SerializeField] private NavMeshAgent navMeshAgent;
[SerializeField] private GameObject flag;
public float DistanceToGo { get; private set; }
public float RemainingDistance => navMeshAgent.remainingDistance;
private Dictionary<int, NavPoint> idNavPointDict;
private void Awake()
{
navMeshAgent.speed = SettingsReader.Instance.GetSettings.MovementSpeed;
idNavPointDict = MapManager.IDToNavPoint;
InvokeRepeating(nameof(UpdateFlagPosition), 0, updateFlagPositionDelay);
InvokeRepeating(nameof(ReachedDestination), 0, updateReachedDestinationDelay);
}
private void OnDestroy()
{
CancelInvoke(nameof(UpdateFlagPosition));
@ -30,18 +38,46 @@ public class MovementController : MonoBehaviour
}
public void MoveToRandomPoint()
{
Debug.Log(MapManager.navPoints == null);
goToNextNavPoint(MapManager.navPoints[Random.Range(0, MapManager.navPoints.Count)]);
{
Debug.Log(MapManager.NavPoints == null);
GoToNextNavPoint(MapManager.NavPoints[Random.Range(0, MapManager.NavPoints.Count)]);
}
public List<NavPoint> getPointsCandidate()
public List<NavPoint> GetPointsCandidate()
{
return MapManager.navPoints
.Where(point => (CurrentNavPoint.position - point.position).magnitude < SettingsReader.Instance.GetSettings.MovementSpeed)
return MapManager.NavPoints
.Where(point =>
(idNavPointDict[PointStartID].Position - point.Position).magnitude < SettingsReader.Instance.GetSettings.MovementDistance)
.ToList();
}
public void goToNextNavPoint(NavPoint destination) =>
navMeshAgent.SetDestination(destination.position);
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 = -1;
}
public void StopOnPath()
{
navMeshAgent.isStopped = true;
PointStartID = -1;
PointEndID = -1;
}
public void ReachedDestination()
{
if ((navMeshAgent.isStopped == false) && (navMeshAgent.velocity.magnitude < 0.1))
PointStartID = PointEndID;
}
}