encompass-cs/encompass-cs/Collections/ComponentBitSet.cs

54 lines
1.5 KiB
C#
Raw Normal View History

2019-12-23 00:21:07 +00:00
using System;
using System.Collections.Generic;
namespace Encompass
{
internal class ComponentBitSet
{
2020-03-20 07:09:57 +00:00
private readonly Dictionary<int, BitSet512> _entities = new Dictionary<int, BitSet512>();
private readonly Dictionary<Type, int> _typeToIndex;
2019-12-23 00:21:07 +00:00
public ComponentBitSet(Dictionary<Type, int> typeToIndex)
{
2020-03-20 07:09:57 +00:00
_typeToIndex = typeToIndex;
2019-12-23 00:21:07 +00:00
}
public void Clear()
{
2020-03-20 07:09:57 +00:00
_entities.Clear();
2019-12-23 00:21:07 +00:00
}
public void AddEntity(int entityID)
2019-12-23 00:21:07 +00:00
{
2020-03-20 07:09:57 +00:00
_entities.Add(entityID, BitSet512.Zero);
2019-12-23 00:21:07 +00:00
}
2020-03-20 22:45:58 +00:00
public void Set<TComponent>(int entityID) where TComponent : struct
2019-12-23 00:21:07 +00:00
{
2020-03-20 07:09:57 +00:00
if (!_entities.ContainsKey(entityID)) { AddEntity(entityID); }
_entities[entityID] = _entities[entityID].Set(_typeToIndex[typeof(TComponent)]);
2019-12-23 00:21:07 +00:00
}
2020-03-20 22:45:58 +00:00
public void RemoveComponent<TComponent>(int entityID) where TComponent : struct
2019-12-23 00:21:07 +00:00
{
2020-03-20 07:09:57 +00:00
if (_entities.ContainsKey(entityID))
2019-12-23 00:21:07 +00:00
{
2020-03-20 07:09:57 +00:00
_entities[entityID] = _entities[entityID].UnSet(_typeToIndex[typeof(TComponent)]);
2019-12-23 00:21:07 +00:00
}
}
public void RemoveEntity(int entityID)
2019-12-23 00:21:07 +00:00
{
2020-03-20 07:09:57 +00:00
if (_entities.ContainsKey(entityID))
2019-12-23 00:21:07 +00:00
{
2020-03-20 07:09:57 +00:00
_entities.Remove(entityID);
2019-12-23 00:21:07 +00:00
}
}
2019-12-29 05:39:35 +00:00
public BitSet512 EntityBitArray(int entityID)
2019-12-23 00:21:07 +00:00
{
2020-03-20 07:09:57 +00:00
return _entities.ContainsKey(entityID) ? _entities[entityID] : BitSet512.Zero;
2019-12-23 00:21:07 +00:00
}
}
}