encompass-cs/encompass-cs/TrackingManager.cs

98 lines
3.4 KiB
C#
Raw Permalink Normal View History

2019-12-28 21:53:02 +00:00
using System;
using System.Collections.Generic;
namespace Encompass
{
internal class TrackingManager
{
2020-03-20 07:09:57 +00:00
private readonly Dictionary<Type, HashSet<Engine>> _immediateComponentTypesToEngines = new Dictionary<Type, HashSet<Engine>>();
private readonly Dictionary<Type, HashSet<Engine>> _componentTypesToEngines = new Dictionary<Type, HashSet<Engine>>();
2019-12-28 21:53:02 +00:00
2020-03-20 07:09:57 +00:00
private readonly HashSet<(int, Type)> _additions = new HashSet<(int, Type)>();
private readonly HashSet<(int, Type)> _removals = new HashSet<(int, Type)>();
2019-12-28 21:53:02 +00:00
2020-03-20 07:09:57 +00:00
private readonly HashSet<(int, Engine)> _pairsToCheck = new HashSet<(int, Engine)>();
2019-12-28 22:30:26 +00:00
2019-12-28 21:53:02 +00:00
public void RegisterComponentTypeToEngine(Type type, Engine engine)
{
if (!_componentTypesToEngines.ContainsKey(type)) { _componentTypesToEngines.Add(type, new HashSet<Engine>()); }
_componentTypesToEngines[type].Add(engine);
}
2019-12-28 22:30:26 +00:00
public void RegisterImmediateComponentTypeToEngine(Type type, Engine engine)
{
if (!_immediateComponentTypesToEngines.ContainsKey(type)) { _immediateComponentTypesToEngines.Add(type, new HashSet<Engine>()); }
_immediateComponentTypesToEngines[type].Add(engine);
}
2019-12-29 21:54:08 +00:00
public void RegisterAddition(int entityID, Type type)
2019-12-28 21:53:02 +00:00
{
2019-12-29 21:54:08 +00:00
_additions.Add((entityID, type));
2019-12-28 21:53:02 +00:00
}
2019-12-29 21:54:08 +00:00
public void RegisterRemoval(int entityID, Type type)
2019-12-28 21:53:02 +00:00
{
2019-12-29 21:54:08 +00:00
_removals.Add((entityID, type));
2019-12-28 21:53:02 +00:00
}
2019-12-29 21:54:08 +00:00
public void InitializeTracking(IEnumerable<int> entityIDs)
2019-12-28 21:53:02 +00:00
{
2019-12-29 21:54:08 +00:00
foreach (var entityID in entityIDs)
2019-12-28 21:53:02 +00:00
{
foreach (var engineSet in _componentTypesToEngines.Values)
{
foreach (var engine in engineSet)
{
2019-12-29 21:54:08 +00:00
engine.CheckAndUpdateTracking(entityID);
2019-12-28 21:53:02 +00:00
}
}
}
}
2019-12-29 21:54:08 +00:00
public void ImmediateUpdateTracking(int entityID, Type componentType)
2019-12-28 22:30:26 +00:00
{
if (_immediateComponentTypesToEngines.ContainsKey(componentType))
{
foreach (var engine in _componentTypesToEngines[componentType])
{
2019-12-29 21:54:08 +00:00
engine.ImmediateCheckAndUpdateTracking(entityID);
2019-12-28 22:30:26 +00:00
}
}
}
2019-12-28 21:53:02 +00:00
public void UpdateTracking()
{
// TODO: optimize so we only check each entity/engine pair once
foreach (var (entity, componentType) in _additions)
{
if (_componentTypesToEngines.ContainsKey(componentType))
{
foreach (var engine in _componentTypesToEngines[componentType])
{
2019-12-28 22:30:26 +00:00
_pairsToCheck.Add((entity, engine));
2019-12-28 21:53:02 +00:00
}
}
}
_additions.Clear();
foreach (var (entity, componentType) in _removals)
{
if (_componentTypesToEngines.ContainsKey(componentType))
{
foreach (var engine in _componentTypesToEngines[componentType])
{
2019-12-28 22:30:26 +00:00
_pairsToCheck.Add((entity, engine));
2019-12-28 21:53:02 +00:00
}
}
}
_removals.Clear();
2019-12-28 22:30:26 +00:00
foreach (var (entity, engine) in _pairsToCheck)
{
engine.CheckAndUpdateTracking(entity);
}
_pairsToCheck.Clear();
2019-12-28 21:53:02 +00:00
}
}
}