using Gley.UrbanSystem.Internal;
using System.Collections.Generic;
using System.Linq;
namespace Gley.TrafficSystem.Internal
{
///
/// Controls the update of active intersections.
///
internal class ActiveIntersectionsManager : IDestroyable
{
private readonly AllIntersectionsDataHandler _allIntersectionsHandler;
private List _activeIntersections;
internal ActiveIntersectionsManager(AllIntersectionsDataHandler trafficIntersectionsDataHandler)
{
_allIntersectionsHandler = trafficIntersectionsDataHandler;
_activeIntersections = new List();
Assign();
GridEvents.OnActiveGridCellsChanged += UpdateActiveIntersections;
}
public void Assign()
{
DestroyableManager.Instance.Register(this);
}
///
/// Create a list of active intersections
///
private void UpdateActiveIntersections(CellData[] activeCells)
{
List intersectionIndexes = new List();
for (int i = 0; i < activeCells.Length; i++)
{
intersectionIndexes.AddRange(activeCells[i].IntersectionsInCell.Except(intersectionIndexes));
}
List result = _allIntersectionsHandler.GetIntersections(intersectionIndexes);
if (_activeIntersections.Count == result.Count && _activeIntersections.All(result.Contains))
{
}
else
{
_activeIntersections = result;
IntersectionEvents.TriggetActiveIntersectionsChangedEvent(_activeIntersections);
}
}
public void OnDestroy()
{
GridEvents.OnActiveGridCellsChanged -= UpdateActiveIntersections;
}
}
}