55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using MoonTools.FastCollections;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Encompass
|
|
{
|
|
internal class ComponentBitSet
|
|
{
|
|
Dictionary<int, BitSet512> entities = new Dictionary<int, BitSet512>();
|
|
Dictionary<Type, int> TypeToIndex { get; }
|
|
|
|
public ComponentBitSet(Dictionary<Type, int> typeToIndex)
|
|
{
|
|
TypeToIndex = typeToIndex;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
entities.Clear();
|
|
}
|
|
|
|
public void AddEntity(int entityID)
|
|
{
|
|
entities.Add(entityID, BitSet512.Zero);
|
|
}
|
|
|
|
public void Set<TComponent>(int entityID) where TComponent : struct, IComponent
|
|
{
|
|
if (!entities.ContainsKey(entityID)) { AddEntity(entityID); }
|
|
entities[entityID] = entities[entityID].Set(TypeToIndex[typeof(TComponent)]);
|
|
}
|
|
|
|
public void RemoveComponent<TComponent>(int entityID) where TComponent : struct, IComponent
|
|
{
|
|
if (entities.ContainsKey(entityID))
|
|
{
|
|
entities[entityID] = entities[entityID].UnSet(TypeToIndex[typeof(TComponent)]);
|
|
}
|
|
}
|
|
|
|
public void RemoveEntity(int entityID)
|
|
{
|
|
if (entities.ContainsKey(entityID))
|
|
{
|
|
entities.Remove(entityID);
|
|
}
|
|
}
|
|
|
|
public BitSet512 EntityBitArray(int entityID)
|
|
{
|
|
return entities.ContainsKey(entityID) ? entities[entityID] : BitSet512.Zero;
|
|
}
|
|
}
|
|
}
|