-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentType.cs
47 lines (40 loc) · 1.25 KB
/
ComponentType.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
namespace MelonECS
{
internal static class ComponentIndex
{
internal static int lastIndex;
internal static Dictionary<Type, int> typeIndex;
static ComponentIndex() => typeIndex = new Dictionary<Type, int>();
}
public static class ComponentType<T> where T : struct, IComponent
{
public static readonly int Index;
static ComponentType()
{
Index = ComponentIndex.typeIndex.TryGetValue(typeof(T), out int index) ? index : ++ComponentIndex.lastIndex;
ComponentIndex.typeIndex[typeof(T)] = Index;
}
}
public static class ComponentType
{
public static int Index(Type type)
{
if (ComponentIndex.typeIndex.TryGetValue(type, out int index))
return index;
index = ++ComponentIndex.lastIndex;
ComponentIndex.typeIndex.Add(type, index);
return index;
}
public static Type Type(int index)
{
foreach (var data in ComponentIndex.typeIndex)
{
if (data.Value == index)
return data.Key;
}
return null;
}
}
}