-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEntity.cs
77 lines (66 loc) · 2.28 KB
/
Entity.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#if DEBUG
using System;
#endif
using System.Runtime.CompilerServices;
using EntityType = System.Int32;
namespace CodexECS
{
public struct Entity
{
public EntityType Val;
public Entity(EntityType entity) => Val = entity;
}
static class EntityExtension
{
public const int BitSizeHalved = sizeof(EntityType) * 4;
public static readonly Entity NullEntity = new Entity((1 << BitSizeHalved) - 1);
#if DEBUG
static EntityExtension()
{
if (NullEntity.GetVersion() > 0)
throw new EcsException("NullEntity should always have 0 version");
}
public static string ToString(this in Entity entity)
{
return Convert.ToString(entity.Val, 2).PadLeft(sizeof(EntityType) * 8, '0');
}
#endif
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EntityType GetId(this in Entity entity)
{
return entity.Val & NullEntity.Val;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetId(this ref Entity entity, in EntityType id)
{
#if DEBUG && !ECS_PERF_TEST
if (id >= NullEntity.Val)
throw new EcsException("set overflow id");
#endif
entity.Val = id | (entity.GetVersion() << BitSizeHalved);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetNullId(this ref Entity entity)
{
entity.Val = NullEntity.GetId() | (entity.GetVersion() << BitSizeHalved);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNull(this in Entity entity)
{
return entity.GetId() == NullEntity.GetId();
}
//CODEX_TODO: use smaller part for version
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EntityType GetVersion(this in Entity entity)
{
return entity.Val >> BitSizeHalved;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IncrementVersion(this ref Entity entity)
{
EntityType version = entity.GetVersion();
version++;
entity.Val = entity.GetId() | (version << BitSizeHalved);
}
}
}