@@ -190,12 +190,13 @@ public void AddComponent<T>(Entity entity, Action<ComponentRef<T>> callback = nu
190
190
_commandBuffer . AddCommand ( new AddComponentCommand ( entity . Id , typeof ( T ) ,
191
191
( ) =>
192
192
{
193
- var arr = ( T [ ] ) Components [ TagRegistry . GetTagBitIndex ( typeof ( T ) ) ] ;
193
+ var idx = TagRegistry . GetTagBitIndex ( typeof ( T ) ) ;
194
+ var arr = ( T [ ] ) Components [ idx ] ;
194
195
ref var compRef = ref arr [ entity . Id ] ;
195
196
compRef = new T ( ) ;
196
197
try
197
198
{
198
- callback ? . Invoke ( new ComponentRef < T > ( entity . Id , this ) ) ;
199
+ callback ? . Invoke ( new ComponentRef < T > ( entity . Id , idx , this ) ) ;
199
200
}
200
201
catch ( Exception e )
201
202
{
@@ -225,12 +226,13 @@ public void AddSingletonComponent<T>(Action<T> callback = null) where T : struct
225
226
_commandBuffer . AddCommand ( new AddComponentCommand ( 0 , typeof ( T ) ,
226
227
( ) =>
227
228
{
228
- var arr = ( T [ ] ) Components [ TagRegistry . GetTagBitIndex ( typeof ( T ) ) ] ;
229
+ var idx = TagRegistry . GetTagBitIndex ( typeof ( T ) ) ;
230
+ var arr = ( T [ ] ) Components [ idx ] ;
229
231
var component = new T ( ) ;
230
232
arr [ 0 ] = component ;
231
233
try
232
234
{
233
- callback ? . Invoke ( new SingletonComponentRef < T > ( 0 , this ) . Value ) ;
235
+ callback ? . Invoke ( new SingletonComponentRef < T > ( idx , this ) ) ;
234
236
}
235
237
catch ( Exception e )
236
238
{
@@ -247,10 +249,11 @@ public void AddSingletonComponent<T>(Action<T> callback = null) where T : struct
247
249
public SingletonComponentRef < T > GetSingletonComponent < T > ( ) where T : struct , ISingletonComponent
248
250
{
249
251
ref var entity = ref Entities [ 0 ] ;
250
- if ( ! entity . Tag . HasBit ( TagRegistry . GetTagBitIndex ( typeof ( T ) ) ) )
252
+ var idx = TagRegistry . GetTagBitIndex ( typeof ( T ) ) ;
253
+ if ( ! entity . Tag . HasBit ( idx ) )
251
254
throw new InvalidOperationException ( $ "Component { typeof ( T ) } not found.") ;
252
255
253
- return new SingletonComponentRef < T > ( 0 , this ) ;
256
+ return new SingletonComponentRef < T > ( idx , this ) ;
254
257
}
255
258
256
259
/// <summary>
@@ -261,9 +264,14 @@ public SingletonComponentRef<T> GetSingletonComponent<T>() where T : struct, ISi
261
264
/// <returns></returns>
262
265
public bool TryGetSingletonComponent < T > ( out SingletonComponentRef < T > value ) where T : struct , ISingletonComponent
263
266
{
267
+ value = default ;
264
268
ref var entity = ref Entities [ 0 ] ;
265
- value = new SingletonComponentRef < T > ( 0 , this ) ;
266
- return entity . Tag . HasBit ( TagRegistry . GetTagBitIndex ( typeof ( T ) ) ) ;
269
+ var idx = TagRegistry . GetTagBitIndex ( typeof ( T ) ) ;
270
+ if ( ! entity . Tag . HasBit ( idx ) )
271
+ return false ;
272
+
273
+ value = new SingletonComponentRef < T > ( idx , this ) ;
274
+ return true ;
267
275
}
268
276
269
277
/// <summary>
@@ -442,12 +450,14 @@ public async ValueTask DisposeAsync()
442
450
{
443
451
group . Clear ( ) ;
444
452
}
453
+
445
454
Groups . Clear ( ) ;
446
455
// clear all components
447
456
foreach ( var arr in Components )
448
457
{
449
458
Array . Clear ( arr , 0 , arr . Length ) ;
450
459
}
460
+
451
461
Components = null ;
452
462
// clear all systems
453
463
_executeSystems . Clear ( ) ;
@@ -511,11 +521,16 @@ private void ProcessCommandBuffer()
511
521
var id = _reusableIds . Count > 0 ? _reusableIds . Dequeue ( ) : _entityIdCounter ++ ;
512
522
var entity = new Entity ( this , id ) ;
513
523
// ensure array size
514
- if ( id >= Entities . Length )
524
+ int newLen = Math . Max ( 1 , Entities . Length ) ;
525
+ int targetLen = id + 1 ;
526
+ while ( newLen < targetLen )
527
+ {
528
+ newLen *= 2 ;
529
+ }
530
+
531
+ if ( Entities . Length < newLen )
515
532
{
516
- var arr2 = new Entity [ id + 1 ] ;
517
- Array . Copy ( Entities , arr2 , Entities . Length ) ;
518
- Entities = arr2 ;
533
+ Array . Resize ( ref Entities , newLen ) ;
519
534
}
520
535
521
536
Entities [ id ] = entity ;
@@ -584,20 +599,27 @@ private void ProcessCommandBuffer()
584
599
}
585
600
586
601
ref Entity entity = ref Entities [ addComponentCommand . Id ] ;
602
+ byte bitIdx = TagRegistry . GetTagBitIndex ( addComponentCommand . ComponentType ) ;
587
603
588
604
// add to array
589
- var arr = Components [ TagRegistry . GetTagBitIndex ( addComponentCommand . ComponentType ) ] ;
590
- if ( arr . Length < entity . Id + 1 )
605
+ var arr = Components [ bitIdx ] ;
606
+ int newLen = Math . Max ( 1 , arr . Length ) ;
607
+ int targetLen = entity . Id + 1 ;
608
+ while ( newLen < targetLen )
609
+ {
610
+ newLen *= 2 ;
611
+ }
612
+
613
+ if ( arr . Length < newLen )
591
614
{
592
- var arr2 = Array . CreateInstance ( addComponentCommand . ComponentType , entity . Id + 1 ) ;
615
+ var arr2 = Array . CreateInstance ( addComponentCommand . ComponentType , newLen ) ;
593
616
Array . Copy ( arr , arr2 , arr . Length ) ;
594
- Components [ TagRegistry . GetTagBitIndex ( addComponentCommand . ComponentType ) ] = arr2 ;
617
+ Components [ bitIdx ] = arr2 ;
595
618
}
596
619
597
620
// set tag
598
621
var oldTag = entity . Tag ;
599
- entity . Tag . SetBit (
600
- TagRegistry . GetTagBitIndex ( addComponentCommand . ComponentType ) ) ;
622
+ entity . Tag . SetBit ( bitIdx ) ;
601
623
// move group
602
624
if ( Groups . TryGetValue ( oldTag , out var oldGroup ) )
603
625
{
0 commit comments