mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-24 03:56:31 +00:00
implemented aspect filtering builder
This commit is contained in:
@@ -9,6 +9,7 @@ namespace MonoGame.Extended.Entities
|
||||
public class ComponentManager : UpdateSystem
|
||||
{
|
||||
public ComponentManager()
|
||||
: base(Aspect.All())
|
||||
{
|
||||
_mappers = new Bag<ComponentMapper>();
|
||||
_componentTypes = new Dictionary<Type, int>();
|
||||
@@ -17,10 +18,9 @@ namespace MonoGame.Extended.Entities
|
||||
private readonly Bag<ComponentMapper> _mappers;
|
||||
private readonly Dictionary<Type, int> _componentTypes;
|
||||
|
||||
private ComponentMapper<T> CreateMapperForType<T>()
|
||||
private ComponentMapper<T> CreateMapperForType<T>(int id)
|
||||
where T : class
|
||||
{
|
||||
var id = _mappers.Count;
|
||||
var mapper = new ComponentMapper<T>(id);
|
||||
_mappers[id] = mapper;
|
||||
return mapper;
|
||||
@@ -29,12 +29,22 @@ namespace MonoGame.Extended.Entities
|
||||
public ComponentMapper<T> GetMapper<T>()
|
||||
where T : class
|
||||
{
|
||||
if (_componentTypes.TryGetValue(typeof(T), out var id))
|
||||
var id = GetComponentTypeId(typeof(T));
|
||||
|
||||
if (_mappers[id] != null)
|
||||
return _mappers[id] as ComponentMapper<T>;
|
||||
|
||||
var mapper = CreateMapperForType<T>();
|
||||
_componentTypes.Add(typeof(T), mapper.Id);
|
||||
return mapper;
|
||||
return CreateMapperForType<T>(id);
|
||||
}
|
||||
|
||||
public int GetComponentTypeId(Type type)
|
||||
{
|
||||
if (_componentTypes.TryGetValue(type, out var id))
|
||||
return id;
|
||||
|
||||
id = _componentTypes.Count;
|
||||
_componentTypes.Add(type, id);
|
||||
return id;
|
||||
}
|
||||
|
||||
public override void Initialize(ComponentManager componentManager)
|
||||
|
||||
Reference in New Issue
Block a user