MoonTools.ECS/src/TypeIndices.cs

32 lines
539 B
C#
Raw Normal View History

2022-12-03 07:43:54 +00:00
using System;
using System.Collections.Generic;
namespace MoonTools.ECS
{
public class TypeIndices
{
Dictionary<Type, int> TypeToIndex = new Dictionary<Type, int>();
int nextID = 0;
public int GetIndex<T>() where T : unmanaged
{
if (!TypeToIndex.ContainsKey(typeof(T)))
{
TypeToIndex.Add(typeof(T), nextID);
nextID += 1;
}
return TypeToIndex[typeof(T)];
}
public int GetIndex(Type type)
{
return TypeToIndex[type];
}
#if DEBUG
public IEnumerable<Type> Types => TypeToIndex.Keys;
#endif
}
}