57 lines
1.5 KiB
C#
57 lines
1.5 KiB
C#
|
|
using UnityEngine;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
|
||
|
|
public class PatrolManager : MonoBehaviour
|
||
|
|
{
|
||
|
|
public static PatrolManager Instance;
|
||
|
|
|
||
|
|
// Dictionary tracking which AI is going to which world position
|
||
|
|
private Dictionary<AIController, Vector3> aiTargets = new Dictionary<AIController, Vector3>();
|
||
|
|
|
||
|
|
[Tooltip("How far apart AI patrol targets must be. Prevents them from driving down the exact same stretch of road.")]
|
||
|
|
public float minimumSpacing = 40f;
|
||
|
|
|
||
|
|
void Awake()
|
||
|
|
{
|
||
|
|
if (Instance == null)
|
||
|
|
{
|
||
|
|
Instance = this;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Destroy(gameObject);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void RegisterTarget(AIController ai, Vector3 targetPoint)
|
||
|
|
{
|
||
|
|
aiTargets[ai] = targetPoint;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void UnregisterTarget(AIController ai)
|
||
|
|
{
|
||
|
|
if (aiTargets.ContainsKey(ai))
|
||
|
|
{
|
||
|
|
aiTargets.Remove(ai);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Checks if a generated road point is too close to where another AI is already heading
|
||
|
|
public bool IsPointCrowded(Vector3 point, AIController requestingAI)
|
||
|
|
{
|
||
|
|
foreach (var kvp in aiTargets)
|
||
|
|
{
|
||
|
|
// Skip checking against ourselves
|
||
|
|
if (kvp.Key == requestingAI) continue;
|
||
|
|
|
||
|
|
// If another AI is heading to a point very close to this one, it's crowded!
|
||
|
|
if (Vector3.Distance(point, kvp.Value) < minimumSpacing)
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|