@@ -19,7 +19,8 @@ public EntityLocation(ArchetypeContainer archetypeContainer, int index)
19
19
}
20
20
21
21
private const int InitialCapacity = 512 ;
22
- EntityLocation [ ] Locations = new EntityLocation [ InitialCapacity ] ;
22
+ int [ ] entityArchetypes = new int [ InitialCapacity ] ;
23
+ int [ ] entityIndices = new int [ InitialCapacity ] ;
23
24
int [ ] Versions = new int [ InitialCapacity ] ;
24
25
HashSet < int > freeIDs = new HashSet < int > ( ) ;
25
26
int nextId = 0 ;
@@ -30,8 +31,13 @@ public EntityLocation(ArchetypeContainer archetypeContainer, int index)
30
31
31
32
public Entity RegisterEntity ( ArchetypeContainer container , int index )
32
33
{
33
- if ( nextId == Locations . Length )
34
- Array . Resize ( ref Locations , Locations . Length * 2 ) ;
34
+ if ( nextId == entityArchetypes . Length )
35
+ {
36
+ int newSize = entityArchetypes . Length * 2 ;
37
+ Array . Resize ( ref entityArchetypes , newSize ) ;
38
+ Array . Resize ( ref entityIndices , newSize ) ;
39
+ Array . Resize ( ref Versions , newSize ) ;
40
+ }
35
41
int id ;
36
42
if ( freeIDs . Count > 0 )
37
43
{
@@ -45,21 +51,24 @@ public Entity RegisterEntity(ArchetypeContainer container, int index)
45
51
id = nextId ;
46
52
nextId ++ ;
47
53
}
48
- Locations [ id ] = new EntityLocation ( container , index ) ;
54
+ entityArchetypes [ id ] = container . ID ;
55
+ entityIndices [ id ] = index ;
49
56
return new Entity ( id , Versions [ id ] ) ;
50
57
}
51
58
public void UnregisterEntity ( Entity entity , out EntityLocation lastLocation )
52
59
{
53
60
if ( entity . Version != Versions [ entity . Id ] )
54
61
throw new Exception ( "The entity was already deleted." ) ;
55
- lastLocation = Locations [ entity . Id ] ;
56
- Locations [ entity . Id ] = default ;
62
+ lastLocation = GetLocation ( entity ) ;
63
+ entityArchetypes [ entity . Id ] = 0 ;
64
+ entityIndices [ entity . Id ] = 0 ;
57
65
Versions [ entity . Id ] ++ ;
58
66
freeIDs . Add ( entity . Id ) ;
59
67
}
60
68
public void MoveEntity ( int entity , ArchetypeContainer newContainer , int newIndex )
61
69
{
62
- Locations [ entity ] = new EntityLocation ( newContainer , newIndex ) ;
70
+ entityArchetypes [ entity ] = newContainer . ID ;
71
+ entityIndices [ entity ] = newIndex ;
63
72
}
64
73
public bool TryGetLocation ( Entity entity , out EntityLocation location )
65
74
{
@@ -70,11 +79,16 @@ public bool TryGetLocation(Entity entity, out EntityLocation location)
70
79
}
71
80
else
72
81
{
73
- location = Locations [ entity . Id ] ;
82
+ location = GetLocation ( entity ) ;
74
83
return true ;
75
84
}
76
85
}
77
86
87
+ private EntityLocation GetLocation ( Entity entity )
88
+ => new EntityLocation (
89
+ ArchetypeContainer . GetById ( entityArchetypes [ entity . Id ] ) ! ,
90
+ entityIndices [ entity . Id ] ) ;
91
+
78
92
public IEnumerator < Entity > GetEnumerator ( ) => new EntityEnumerator ( this ) ;
79
93
IEnumerator IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
80
94
struct EntityEnumerator : IEnumerator < Entity >
0 commit comments