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

55 lines
1.5 KiB
C#
Raw Normal View History

2019-12-29 05:39:35 +00:00
using MoonTools.FastCollections;
2019-12-23 00:21:07 +00:00
using System;
using System.Collections.Generic;
namespace Encompass
{
internal class ComponentBitSet
{
2019-12-29 05:39:35 +00:00
Dictionary<int, BitSet512> entities = new Dictionary<int, BitSet512>();
2019-12-23 00:21:07 +00:00
Dictionary<Type, int> TypeToIndex { get; }
public ComponentBitSet(Dictionary<Type, int> typeToIndex)
{
TypeToIndex = typeToIndex;
}
public void Clear()
{
entities.Clear();
}
public void AddEntity(int entityID)
2019-12-23 00:21:07 +00:00
{
2019-12-29 05:39:35 +00:00
entities.Add(entityID, BitSet512.Zero);
2019-12-23 00:21:07 +00:00
}
public void Set<TComponent>(int entityID) where TComponent : struct, IComponent
2019-12-23 00:21:07 +00:00
{
if (!entities.ContainsKey(entityID)) { AddEntity(entityID); }
entities[entityID] = entities[entityID].Set(TypeToIndex[typeof(TComponent)]);
2019-12-23 00:21:07 +00:00
}
public void RemoveComponent<TComponent>(int entityID) where TComponent : struct, IComponent
2019-12-23 00:21:07 +00:00
{
if (entities.ContainsKey(entityID))
2019-12-23 00:21:07 +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
{
if (entities.ContainsKey(entityID))
2019-12-23 00:21:07 +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
{
2019-12-29 05:39:35 +00:00
return entities.ContainsKey(entityID) ? entities[entityID] : BitSet512.Zero;
2019-12-23 00:21:07 +00:00
}
}
}