169 lines
6.4 KiB
C#
Raw Normal View History

2024-02-20 18:39:12 +08:00
using HPJ.Presentation.Agents;
using HPJ.Simulation;
using HPJ.Simulation.Enums;
using HPJ.Simulation.Map;
using HPJ.Simulation.Pathing;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace HPJ.Presentation
{
/// <summary>
/// The example scene to display random pathing
/// </summary>
public class RandomPathsExampleScene : MonoBehaviour
{
[SerializeField]
private float _getRandomPointEvery = 0.5f;
private float _timer = 0;
[SerializeField]
private int _randomTileDistanceVariance = 5;
[SerializeField]
private int _randomRange = 25;
[SerializeField]
private NavigationAgent _testAgent;
[SerializeField]
private LineRenderer _testLine;
private NavigationJob _testJob;
private List<Vector3> _linePoints = new List<Vector3>();
public bool PaththingCompleted { get; set; }
public IntVector3 PreviousRandomDestination { get; protected set; }
public int RandomMode = 0;
private void Awake()
{
_testJob = new NavigationJob(OnCompleteCallback);
Cursor.lockState = CursorLockMode.Confined;
Cursor.visible = true;
}
public void OnCompleteCallback()
{
_testJob.CurrentStep = PathfindingCalculationStep.Finalized;
PaththingCompleted = true;
}
// Update is called once per frame
void Update()
{
if (_testJob.CurrentStep == PathfindingCalculationStep.Pathfinding)
{
return;
}
_timer += Time.deltaTime;
if (_timer >= _getRandomPointEvery)
{
_timer = 0;
// Define the plane in world space
Plane plane = new Plane(Vector3.up, Vector3.zero);
// Cast a ray from the camera to the mouse position
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Calculate the distance from the ray origin to the intersection with the plane
float distance;
if (plane.Raycast(ray, out distance))
{
Vector3 hitPoint = ray.GetPoint(distance);
if (_testAgent.CurrentMap != null)
{
if (NavigationManager.Instance.ValidPoint(hitPoint, _testAgent.CurrentMap))
{
_testJob.Clear();
IntVector3 RandomDestination;
if (RandomMode == 1)
{
RandomDestination = _testAgent.GetRandomDestination(_randomRange);
}
else if (RandomMode == 2)
{
RandomDestination = _testAgent.GetRandomDestinationAwayFromPoint(hitPoint, _randomTileDistanceVariance);
}
else
{
RandomDestination = _testAgent.GetRandomDestinationNearPoint(hitPoint, _randomTileDistanceVariance);
}
_testJob.SetDestinationInfo(NavigationManager.Instance.GetTileWorldPosition(_testAgent.transform.position), RandomDestination, _testAgent.TraversableTiles, _testAgent.PrefferedTiles, _testAgent.NavigationType, _testAgent.CurrentMap, true);
NavigationManager.Instance.SetDestination(_testJob);
PreviousRandomDestination = RandomDestination;
}
else if (NavigationManager.Instance.ValidPoint(hitPoint, out MapSet HitMap))
{
_testJob.Clear();
IntVector3 RandomDestination;
if (RandomMode == 1)
{
RandomDestination = _testAgent.GetRandomDestination(_randomRange);
}
else if (RandomMode == 2)
{
RandomDestination = _testAgent.GetRandomDestinationAwayFromPoint(hitPoint, _randomTileDistanceVariance);
}
else
{
RandomDestination = _testAgent.GetRandomDestinationNearPoint(hitPoint, _randomTileDistanceVariance);
}
_testJob.SetDestinationInfo(NavigationManager.Instance.GetTileWorldPosition(_testAgent.transform.position), RandomDestination, _testAgent.TraversableTiles, _testAgent.PrefferedTiles, _testAgent.NavigationType, _testAgent.CurrentMap, true);
NavigationManager.Instance.SetDestination(_testJob);
PreviousRandomDestination = RandomDestination;
}
}
}
}
else if (PaththingCompleted)
{
PaththingCompleted = false;
UpdateLine();
}
if (Input.GetMouseButtonDown(1))
{
_testAgent.SetDestination(PreviousRandomDestination);
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
RandomMode = 0;
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
RandomMode = 1;
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
RandomMode = 2;
}
}
private void UpdateLine()
{
//Debug.Log("Update Line");
_linePoints.Clear();
if (_testJob.CurrentPath.Path.Count <= 0)
{
_testLine.positionCount = 0;
return;
}
for (int i = 0; i < _testJob.CurrentPath.Path.Count; i++)
{
Vector3 NextPoint = NavigationManager.Instance.GetTileCenterWorldPosition(_testJob.CurrentPath.Path[i], _testAgent.CurrentMap);
NextPoint.y += 0.01f;
_linePoints.Add(NextPoint);
}
_testLine.positionCount = _testJob.CurrentPath.Path.Count;
_testLine.SetPositions(_linePoints.ToArray());
}
}
}