-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomponent.go
More file actions
821 lines (667 loc) · 31.4 KB
/
component.go
File metadata and controls
821 lines (667 loc) · 31.4 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
package volt
import (
"fmt"
"slices"
)
// ComponentInterface is the interface for all the Components.
//
// It wraps the GetComponentId method, that returns a Component identifier.
type ComponentInterface interface {
GetComponentId() ComponentId
}
type ComponentIdConf struct {
ComponentId
conf any
}
func (world *World) getComponentsIds(components ...ComponentInterface) []ComponentId {
componentsIds := make([]ComponentId, len(components))
for i, component := range components {
componentsIds[i] = component.GetComponentId()
}
return componentsIds
}
// ConfigureComponent configures a Component of type T using the build function related to it.
//
// The parameter conf contains all the data required for the configuration.
func ConfigureComponent[T ComponentInterface](world *World, conf any) T {
var t T
componentRegistry := world.componentsRegistry[t.GetComponentId()]
componentRegistry.builderFn(&t, conf)
return t
}
// AddComponent adds the component T to the existing EntityId.
//
// It returns an error if:
// - the entity does not exist
// - the entity has the component
// - an internal error occurs
func AddComponent[T ComponentInterface](world *World, entityId EntityId, component T) error {
if int(entityId) >= len(world.entities) {
return fmt.Errorf("entity %v does not exist", entityId)
}
entityRecord := world.entities[entityId]
componentId := component.GetComponentId()
if world.hasComponents(entityRecord, componentId) {
return fmt.Errorf("the entity %d already owns the component %d", entityId, componentId)
}
archetype := world.getNextArchetype(entityRecord, world.getComponentsIds(component)...)
err := addComponentsToArchetype1(world, entityRecord, archetype, component)
if err != nil {
return fmt.Errorf("the component %d cannot be added to entity %d: %w", componentId, entityId, err)
}
world.componentAddedFn(entityId, componentId)
return nil
}
// AddComponents2 adds the components A, B to the existing EntityId.
//
// It returns an error if:
// - the entity does not exist
// - the entity has one of the component
// - an internal error occurs
//
// This solution is faster than an atomic solution.
func AddComponents2[A, B ComponentInterface](world *World, entityId EntityId, a A, b B) error {
if int(entityId) >= len(world.entities) {
return fmt.Errorf("entity %v does not exist", entityId)
}
entityRecord := world.entities[entityId]
return addComponents2(world, entityRecord, a, b)
}
func addComponents2[A, B ComponentInterface](world *World, entityRecord entityRecord, a A, b B) error {
archetype := world.getNextArchetype(entityRecord, a.GetComponentId(), b.GetComponentId())
entityId := entityRecord.Id
if world.hasComponents(entityRecord, a.GetComponentId(), b.GetComponentId()) {
return fmt.Errorf("the entity %d already owns the components %v", entityId, []ComponentId{a.GetComponentId(), b.GetComponentId()})
}
err := addComponentsToArchetype2(world, entityRecord, archetype, a, b)
if err != nil {
return fmt.Errorf("the components %d cannot be added to entity %d: %w", []ComponentId{a.GetComponentId(), b.GetComponentId()}, entityId, err)
}
world.componentAddedFn(entityId, a.GetComponentId())
world.componentAddedFn(entityId, b.GetComponentId())
return nil
}
// AddComponents3 adds the components A, B, C to the existing EntityId.
//
// It returns an error if:
// - the entity does not exist
// - the entity has one of the component
// - an internal error occurs
//
// This solution is faster than an atomic solution.
func AddComponents3[A, B, C ComponentInterface](world *World, entityId EntityId, a A, b B, c C) error {
if int(entityId) >= len(world.entities) {
return fmt.Errorf("entity %v does not exist", entityId)
}
entityRecord := world.entities[entityId]
return addComponents3(world, entityRecord, a, b, c)
}
func addComponents3[A, B, C ComponentInterface](world *World, entityRecord entityRecord, a A, b B, c C) error {
archetype := world.getNextArchetype(entityRecord, a.GetComponentId(), b.GetComponentId(), c.GetComponentId())
entityId := entityRecord.Id
if world.hasComponents(entityRecord, a.GetComponentId(), b.GetComponentId(), c.GetComponentId()) {
return fmt.Errorf("the entity %d already owns the components %v", entityId, []ComponentId{a.GetComponentId(), b.GetComponentId(), c.GetComponentId()})
}
err := addComponentsToArchetype3(world, entityRecord, archetype, a, b, c)
if err != nil {
return fmt.Errorf("the components %d cannot be added to entity %d: %w", []ComponentId{a.GetComponentId(), b.GetComponentId(), c.GetComponentId()}, entityId, err)
}
world.componentAddedFn(entityId, a.GetComponentId())
world.componentAddedFn(entityId, b.GetComponentId())
world.componentAddedFn(entityId, c.GetComponentId())
return nil
}
// AddComponents4 adds the components A, B, C, D to the existing EntityId.
//
// It returns an error if:
// - the entity does not exist
// - the entity has one of the component
// - an internal error occurs
//
// This solution is faster than an atomic solution.
func AddComponents4[A, B, C, D ComponentInterface](world *World, entityId EntityId, a A, b B, c C, d D) error {
if int(entityId) >= len(world.entities) {
return fmt.Errorf("entity %v does not exist", entityId)
}
entityRecord := world.entities[entityId]
return addComponents4(world, entityRecord, a, b, c, d)
}
func addComponents4[A, B, C, D ComponentInterface](world *World, entityRecord entityRecord, a A, b B, c C, d D) error {
archetype := world.getNextArchetype(entityRecord, a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId())
entityId := entityRecord.Id
if world.hasComponents(entityRecord, a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId()) {
return fmt.Errorf("the entity %d already owns the components %v", entityId, []ComponentId{a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId()})
}
err := addComponentsToArchetype4(world, entityRecord, archetype, a, b, c, d)
if err != nil {
return fmt.Errorf("the components %d cannot be added to entity %d: %w", []ComponentId{a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId()}, entityId, err)
}
world.componentAddedFn(entityId, a.GetComponentId())
world.componentAddedFn(entityId, b.GetComponentId())
world.componentAddedFn(entityId, c.GetComponentId())
world.componentAddedFn(entityId, d.GetComponentId())
return nil
}
// AddComponents5 adds the components A, B, C, D, E to the existing EntityId.
//
// It returns an error if:
// - the entity does not exist
// - the entity has one of the component
// - an internal error occurs
//
// This solution is faster than an atomic solution.
func AddComponents5[A, B, C, D, E ComponentInterface](world *World, entityId EntityId, a A, b B, c C, d D, e E) error {
if int(entityId) >= len(world.entities) {
return fmt.Errorf("entity %v does not exist", entityId)
}
entityRecord := world.entities[entityId]
return addComponents5(world, entityRecord, a, b, c, d, e)
}
func addComponents5[A, B, C, D, E ComponentInterface](world *World, entityRecord entityRecord, a A, b B, c C, d D, e E) error {
archetype := world.getNextArchetype(entityRecord, a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId())
entityId := entityRecord.Id
if world.hasComponents(entityRecord, a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId()) {
return fmt.Errorf("the entity %d already owns the components %v", entityId, []ComponentId{a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId()})
}
err := addComponentsToArchetype5(world, entityRecord, archetype, a, b, c, d, e)
if err != nil {
return fmt.Errorf("the components %d cannot be added to entity %d: %w", []ComponentId{a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId()}, entityId, err)
}
world.componentAddedFn(entityId, a.GetComponentId())
world.componentAddedFn(entityId, b.GetComponentId())
world.componentAddedFn(entityId, c.GetComponentId())
world.componentAddedFn(entityId, d.GetComponentId())
world.componentAddedFn(entityId, e.GetComponentId())
return nil
}
// AddComponents6 adds the components A, B, C, D, E, F to the existing EntityId.
//
// It returns an error if:
// - the entity does not exist
// - the entity has one of the component
// - an internal error occurs
//
// This solution is faster than an atomic solution.
func AddComponents6[A, B, C, D, E, F ComponentInterface](world *World, entityId EntityId, a A, b B, c C, d D, e E, f F) error {
if int(entityId) >= len(world.entities) {
return fmt.Errorf("entity %v does not exist", entityId)
}
entityRecord := world.entities[entityId]
return addComponents6(world, entityRecord, a, b, c, d, e, f)
}
func addComponents6[A, B, C, D, E, F ComponentInterface](world *World, entityRecord entityRecord, a A, b B, c C, d D, e E, f F) error {
archetype := world.getNextArchetype(entityRecord, a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId(), f.GetComponentId())
entityId := entityRecord.Id
if world.hasComponents(entityRecord, a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId(), f.GetComponentId()) {
return fmt.Errorf("the entity %d already owns the components %v", entityId, []ComponentId{a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId(), f.GetComponentId()})
}
err := addComponentsToArchetype6(world, entityRecord, archetype, a, b, c, d, e, f)
if err != nil {
return fmt.Errorf("the components %d cannot be added to entity %d: %w", []ComponentId{a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId(), f.GetComponentId()}, entityId, err)
}
world.componentAddedFn(entityId, a.GetComponentId())
world.componentAddedFn(entityId, b.GetComponentId())
world.componentAddedFn(entityId, c.GetComponentId())
world.componentAddedFn(entityId, d.GetComponentId())
world.componentAddedFn(entityId, e.GetComponentId())
world.componentAddedFn(entityId, f.GetComponentId())
return nil
}
// AddComponents7 adds the components A, B, C, D, E, F, G to the existing EntityId.
//
// It returns an error if:
// - the entity does not exist
// - the entity has one of the component
// - an internal error occurs
//
// This solution is faster than an atomic solution.
func AddComponents7[A, B, C, D, E, F, G ComponentInterface](world *World, entityId EntityId, a A, b B, c C, d D, e E, f F, g G) error {
if int(entityId) >= len(world.entities) {
return fmt.Errorf("entity %v does not exist", entityId)
}
entityRecord := world.entities[entityId]
return addComponents7(world, entityRecord, a, b, c, d, e, f, g)
}
func addComponents7[A, B, C, D, E, F, G ComponentInterface](world *World, entityRecord entityRecord, a A, b B, c C, d D, e E, f F, g G) error {
archetype := world.getNextArchetype(entityRecord, a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId(), f.GetComponentId(), g.GetComponentId())
entityId := entityRecord.Id
if world.hasComponents(entityRecord, a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId(), f.GetComponentId(), g.GetComponentId()) {
return fmt.Errorf("the entity %d already owns the components %v", entityId, []ComponentId{a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId(), f.GetComponentId(), g.GetComponentId()})
}
err := addComponentsToArchetype7(world, entityRecord, archetype, a, b, c, d, e, f, g)
if err != nil {
return fmt.Errorf("the components %d cannot be added to entity %d: %w", []ComponentId{a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId(), f.GetComponentId(), g.GetComponentId()}, entityId, err)
}
world.componentAddedFn(entityId, a.GetComponentId())
world.componentAddedFn(entityId, b.GetComponentId())
world.componentAddedFn(entityId, c.GetComponentId())
world.componentAddedFn(entityId, d.GetComponentId())
world.componentAddedFn(entityId, e.GetComponentId())
world.componentAddedFn(entityId, f.GetComponentId())
world.componentAddedFn(entityId, g.GetComponentId())
return nil
}
// AddComponents8 adds the components A, B, C, D, E, F, G, H to the existing EntityId.
//
// It returns an error if:
// - the entity does not exist
// - the entity has one of the component
// - an internal error occurs
//
// This solution is faster than an atomic solution.
func AddComponents8[A, B, C, D, E, F, G, H ComponentInterface](world *World, entityId EntityId, a A, b B, c C, d D, e E, f F, g G, h H) error {
if int(entityId) >= len(world.entities) {
return fmt.Errorf("entity %v does not exist", entityId)
}
entityRecord := world.entities[entityId]
return addComponents8(world, entityRecord, a, b, c, d, e, f, g, h)
}
func addComponents8[A, B, C, D, E, F, G, H ComponentInterface](world *World, entityRecord entityRecord, a A, b B, c C, d D, e E, f F, g G, h H) error {
archetype := world.getNextArchetype(entityRecord, a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId(), f.GetComponentId(), g.GetComponentId(), h.GetComponentId())
entityId := entityRecord.Id
if world.hasComponents(entityRecord, a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId(), f.GetComponentId(), g.GetComponentId(), h.GetComponentId()) {
return fmt.Errorf("the entity %d already owns the components %v", entityId, []ComponentId{a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId(), f.GetComponentId(), g.GetComponentId(), h.GetComponentId()})
}
err := addComponentsToArchetype8(world, entityRecord, archetype, a, b, c, d, e, f, g, h)
if err != nil {
return fmt.Errorf("the components %d cannot be added to entity %d: %w", []ComponentId{a.GetComponentId(), b.GetComponentId(), c.GetComponentId(), d.GetComponentId(), e.GetComponentId(), f.GetComponentId(), g.GetComponentId(), h.GetComponentId()}, entityId, err)
}
world.componentAddedFn(entityId, a.GetComponentId())
world.componentAddedFn(entityId, b.GetComponentId())
world.componentAddedFn(entityId, c.GetComponentId())
world.componentAddedFn(entityId, d.GetComponentId())
world.componentAddedFn(entityId, e.GetComponentId())
world.componentAddedFn(entityId, f.GetComponentId())
world.componentAddedFn(entityId, g.GetComponentId())
world.componentAddedFn(entityId, h.GetComponentId())
return nil
}
// AddComponent adds the component with ComponentId to the EntityId.
//
// This non-generic version is adapted for when generics are not available, though might be slower.
// It returns an error if:
// - the entity already has the componentId
// - the componentId is not registered in the World
// - an internal error occurs
func (world *World) AddComponent(entityId EntityId, componentId ComponentId, conf any) error {
if int(entityId) >= len(world.entities) {
return fmt.Errorf("entity %v does not exist", entityId)
}
entityRecord := world.entities[entityId]
if world.hasComponents(entityRecord, componentId) {
return fmt.Errorf("the entity %d already owns the component %d", entityId, componentId)
}
componentRegistry, err := world.getConfigByComponentId(componentId)
if err != nil {
return err
}
err = componentRegistry.addComponent(world, entityId, conf)
if err != nil {
return err
}
world.componentAddedFn(entityId, componentId)
return nil
}
// AddComponents adds variadic components to the EntityId.
//
// This non-generic version is adapted for when generics are not available, though might be slower.
// It returns an error if:
// - the entity already has the components Ids
// - the componentsIds are not registered in the World
// - an internal error occurs
func (world *World) AddComponents(entityId EntityId, componentsIdsConfs ...ComponentIdConf) error {
if int(entityId) >= len(world.entities) {
return fmt.Errorf("entity %v does not exist", entityId)
}
entityRecord := world.entities[entityId]
var componentsIds []ComponentId
for _, componentIdConf := range componentsIdsConfs {
componentsIds = append(componentsIds, componentIdConf.ComponentId)
}
if world.hasComponents(entityRecord, componentsIds...) {
return fmt.Errorf("the entity %d already owns the components %v", entityId, componentsIds)
}
for _, componentIdConf := range componentsIdsConfs {
componentRegistry, err := world.getConfigByComponentId(componentIdConf.ComponentId)
if err != nil {
return err
}
err = componentRegistry.addComponent(world, entityId, componentIdConf.conf)
if err != nil {
return err
}
world.componentAddedFn(entityId, componentIdConf.ComponentId)
}
return nil
}
// RemoveComponent removes the component to EntityId.
//
// It returns an error if the EntityId does not have the component.
func RemoveComponent[T ComponentInterface](world *World, entityId EntityId) error {
var t T
componentId := t.GetComponentId()
if int(entityId) >= len(world.entities) {
return fmt.Errorf("entity %v does not exist", entityId)
}
entityRecord := world.entities[entityId]
if !world.hasComponents(entityRecord, componentId) {
return fmt.Errorf("the entity %d doesn't own the component %d", entityId, componentId)
}
// Remove from the previous archetype
s := getStorage[T](world)
removeComponent(world, s, entityRecord, componentId)
return nil
}
// RemoveComponent removes the component with ComponentId from the EntityId.
//
// This non-generic version is adapted for when generics are not available, though might be slower.
// It returns an error if:
// - the entity does not have the component
// - the ComponentId is not registered in the World
func (world *World) RemoveComponent(entityId EntityId, componentId ComponentId) error {
entityRecord := world.entities[entityId]
if !world.hasComponents(entityRecord, componentId) {
return fmt.Errorf("the entity %d doesn't own the component %d", entityId, componentId)
}
// Remove from the previous archetype
s, err := world.getStorageForComponentId(componentId)
if err != nil {
return err
}
removeComponent(world, s, entityRecord, componentId)
return nil
}
func removeComponent(world *World, s storage, entityRecord entityRecord, componentId ComponentId) {
world.componentRemovedFn(entityRecord.Id, componentId)
oldArchetype := &world.archetypes[entityRecord.archetypeId]
s.moveLastToKey(oldArchetype.Id, entityRecord.key)
// Move every components to the new one, and set all the records
componentKey := slices.Index(oldArchetype.Type, componentId)
componentsIds := make([]ComponentId, len(oldArchetype.Type))
copy(componentsIds, oldArchetype.Type)
componentsIds = append(componentsIds[:componentKey], componentsIds[componentKey+1:]...)
archetype := world.getArchetypeForComponentsIds(componentsIds...)
moveComponentsToArchetype(world, entityRecord, oldArchetype, archetype)
world.setArchetype(entityRecord, archetype)
}
// HasComponents returns whether the entity has the given variadic list of ComponentId.
//
// It returns false if at least one ComponentId is not owned.
func (world *World) HasComponents(entityId EntityId, componentsIds ...ComponentId) bool {
if int(entityId) >= len(world.entities) {
return false
}
entityRecord := world.entities[entityId]
return world.hasComponents(entityRecord, componentsIds...)
}
func (world *World) hasComponents(entityRecord entityRecord, componentsIds ...ComponentId) bool {
archetype := world.archetypes[entityRecord.archetypeId]
for _, componentId := range componentsIds {
if !slices.Contains(archetype.Type, componentId) {
return false
}
}
return true
}
// GetComponent returns a pointer to the component T owned by the entity.
//
// If the entity does not have the component, it returns nil
func GetComponent[T ComponentInterface](world *World, entityId EntityId) *T {
s := getStorage[T](world)
entityRecord := world.entities[entityId]
if !s.hasArchetype(entityRecord.archetypeId) {
return nil
}
return &s.archetypesComponentsEntities[entityRecord.archetypeId][entityRecord.key]
}
// GetComponent returns the component with ComponentId for EntityId.
//
// This non-generic version is adapted for when generics are not available,
// though might be slower and requires a type assertion.
// It returns an error if:
// - the ComponentId is not registered in the World
// - the entity does not have the component
func (world *World) GetComponent(entityId EntityId, componentId ComponentId) (any, error) {
entityRecord := world.entities[entityId]
s, err := world.getStorageForComponentId(componentId)
if err != nil {
return nil, err
}
if !s.hasArchetype(entityRecord.archetypeId) {
return nil, fmt.Errorf("the entity %d doesn't own the component %d", entityId, componentId)
}
return s.get(entityRecord.archetypeId, entityRecord.key), nil
}
func addComponentsToArchetype1[A ComponentInterface](world *World, entityRecord entityRecord, archetype *archetype, component A) error {
storageA := getStorage[A](world)
if storageA == nil {
componentId := component.GetComponentId()
return fmt.Errorf("no storage found for component %d", componentId)
}
// If the entity has no component, simply add it the archetype
if entityRecord.archetypeId == 0 {
world.setArchetype(entityRecord, archetype)
} else {
oldArchetype := world.getArchetype(entityRecord)
if archetype.Id != oldArchetype.Id {
moveComponentsToArchetype(world, entityRecord, oldArchetype, archetype)
world.setArchetype(entityRecord, archetype)
}
}
storageA.add(archetype.Id, component)
return nil
}
func addComponentsToArchetype2[A, B ComponentInterface](world *World, entityRecord entityRecord, archetype *archetype, componentA A, componentB B) error {
storageA := getStorage[A](world)
storageB := getStorage[B](world)
if storageA == nil || storageB == nil {
componentsIds := []ComponentId{componentA.GetComponentId(), componentB.GetComponentId()}
return fmt.Errorf("no storage found for component %v", componentsIds)
}
// If the entity has no component, simply add it the archetype
if entityRecord.archetypeId == 0 {
world.setArchetype(entityRecord, archetype)
} else {
oldArchetype := world.getArchetype(entityRecord)
if archetype.Id != oldArchetype.Id {
moveComponentsToArchetype(world, entityRecord, oldArchetype, archetype)
world.setArchetype(entityRecord, archetype)
}
}
storageA.add(archetype.Id, componentA)
storageB.add(archetype.Id, componentB)
return nil
}
func addComponentsToArchetype3[A, B, C ComponentInterface](world *World, entityRecord entityRecord, archetype *archetype, componentA A, componentB B, componentC C) error {
storageA := getStorage[A](world)
storageB := getStorage[B](world)
storageC := getStorage[C](world)
if storageA == nil || storageB == nil || storageC == nil {
componentsIds := []ComponentId{componentA.GetComponentId(), componentB.GetComponentId(), componentC.GetComponentId()}
return fmt.Errorf("no storage found for components %v", componentsIds)
}
// If the entity has no component, simply add it the archetype
if entityRecord.archetypeId == 0 {
world.setArchetype(entityRecord, archetype)
} else {
oldArchetype := world.getArchetype(entityRecord)
if archetype.Id != oldArchetype.Id {
moveComponentsToArchetype(world, entityRecord, oldArchetype, archetype)
world.setArchetype(entityRecord, archetype)
}
}
storageA.add(archetype.Id, componentA)
storageB.add(archetype.Id, componentB)
storageC.add(archetype.Id, componentC)
return nil
}
func addComponentsToArchetype4[A, B, C, D ComponentInterface](world *World, entityRecord entityRecord, archetype *archetype, componentA A, componentB B, componentC C, componentD D) error {
storageA := getStorage[A](world)
storageB := getStorage[B](world)
storageC := getStorage[C](world)
storageD := getStorage[D](world)
if storageA == nil || storageB == nil || storageC == nil || storageD == nil {
componentsIds := []ComponentId{componentA.GetComponentId(), componentB.GetComponentId(), componentC.GetComponentId(), componentD.GetComponentId()}
return fmt.Errorf("no storage found for components %v", componentsIds)
}
// If the entity has no component, simply add it the archetype
if entityRecord.archetypeId == 0 {
world.setArchetype(entityRecord, archetype)
} else {
oldArchetype := world.getArchetype(entityRecord)
if archetype.Id != oldArchetype.Id {
moveComponentsToArchetype(world, entityRecord, oldArchetype, archetype)
world.setArchetype(entityRecord, archetype)
}
}
storageA.add(archetype.Id, componentA)
storageB.add(archetype.Id, componentB)
storageC.add(archetype.Id, componentC)
storageD.add(archetype.Id, componentD)
return nil
}
func addComponentsToArchetype5[A, B, C, D, E ComponentInterface](world *World, entityRecord entityRecord, archetype *archetype, componentA A, componentB B, componentC C, componentD D, componentE E) error {
storageA := getStorage[A](world)
storageB := getStorage[B](world)
storageC := getStorage[C](world)
storageD := getStorage[D](world)
storageE := getStorage[E](world)
if storageA == nil || storageB == nil || storageC == nil || storageD == nil || storageE == nil {
componentsIds := []ComponentId{componentA.GetComponentId(), componentB.GetComponentId(), componentC.GetComponentId(), componentD.GetComponentId(), componentE.GetComponentId()}
return fmt.Errorf("no storage found for components %v", componentsIds)
}
// If the entity has no component, simply add it the archetype
if entityRecord.archetypeId == 0 {
world.setArchetype(entityRecord, archetype)
} else {
oldArchetype := world.getArchetype(entityRecord)
if archetype.Id != oldArchetype.Id {
moveComponentsToArchetype(world, entityRecord, oldArchetype, archetype)
world.setArchetype(entityRecord, archetype)
}
}
storageA.add(archetype.Id, componentA)
storageB.add(archetype.Id, componentB)
storageC.add(archetype.Id, componentC)
storageD.add(archetype.Id, componentD)
storageE.add(archetype.Id, componentE)
return nil
}
func addComponentsToArchetype6[A, B, C, D, E, F ComponentInterface](world *World, entityRecord entityRecord, archetype *archetype, componentA A, componentB B, componentC C, componentD D, componentE E, componentF F) error {
storageA := getStorage[A](world)
storageB := getStorage[B](world)
storageC := getStorage[C](world)
storageD := getStorage[D](world)
storageE := getStorage[E](world)
storageF := getStorage[F](world)
if storageA == nil || storageB == nil || storageC == nil || storageD == nil || storageE == nil || storageF == nil {
componentsIds := []ComponentId{componentA.GetComponentId(), componentB.GetComponentId(), componentC.GetComponentId(), componentD.GetComponentId(), componentE.GetComponentId(), componentF.GetComponentId()}
return fmt.Errorf("no storage found for components %v", componentsIds)
}
// If the entity has no component, simply add it the archetype
if entityRecord.archetypeId == 0 {
world.setArchetype(entityRecord, archetype)
} else {
oldArchetype := world.getArchetype(entityRecord)
if archetype.Id != oldArchetype.Id {
moveComponentsToArchetype(world, entityRecord, oldArchetype, archetype)
world.setArchetype(entityRecord, archetype)
}
}
storageA.add(archetype.Id, componentA)
storageB.add(archetype.Id, componentB)
storageC.add(archetype.Id, componentC)
storageD.add(archetype.Id, componentD)
storageE.add(archetype.Id, componentE)
storageF.add(archetype.Id, componentF)
return nil
}
func addComponentsToArchetype7[A, B, C, D, E, F, G ComponentInterface](world *World, entityRecord entityRecord, archetype *archetype, componentA A, componentB B, componentC C, componentD D, componentE E, componentF F, componentG G) error {
storageA := getStorage[A](world)
storageB := getStorage[B](world)
storageC := getStorage[C](world)
storageD := getStorage[D](world)
storageE := getStorage[E](world)
storageF := getStorage[F](world)
storageG := getStorage[G](world)
if storageA == nil || storageB == nil || storageC == nil || storageD == nil || storageE == nil || storageF == nil || storageG == nil {
componentsIds := []ComponentId{componentA.GetComponentId(), componentB.GetComponentId(), componentC.GetComponentId(), componentD.GetComponentId(), componentE.GetComponentId(), componentF.GetComponentId(), componentG.GetComponentId()}
return fmt.Errorf("no storage found for components %v", componentsIds)
}
// If the entity has no component, simply add it the archetype
if entityRecord.archetypeId == 0 {
world.setArchetype(entityRecord, archetype)
} else {
oldArchetype := world.getArchetype(entityRecord)
if archetype.Id != oldArchetype.Id {
moveComponentsToArchetype(world, entityRecord, oldArchetype, archetype)
world.setArchetype(entityRecord, archetype)
}
}
storageA.add(archetype.Id, componentA)
storageB.add(archetype.Id, componentB)
storageC.add(archetype.Id, componentC)
storageD.add(archetype.Id, componentD)
storageE.add(archetype.Id, componentE)
storageF.add(archetype.Id, componentF)
storageG.add(archetype.Id, componentG)
return nil
}
func addComponentsToArchetype8[A, B, C, D, E, F, G, H ComponentInterface](world *World, entityRecord entityRecord, archetype *archetype, componentA A, componentB B, componentC C, componentD D, componentE E, componentF F, componentG G, componentH H) error {
storageA := getStorage[A](world)
storageB := getStorage[B](world)
storageC := getStorage[C](world)
storageD := getStorage[D](world)
storageE := getStorage[E](world)
storageF := getStorage[F](world)
storageG := getStorage[G](world)
storageH := getStorage[H](world)
if storageA == nil || storageB == nil || storageC == nil || storageD == nil || storageE == nil || storageF == nil || storageG == nil || storageH == nil {
componentsIds := []ComponentId{componentA.GetComponentId(), componentB.GetComponentId(), componentC.GetComponentId(), componentD.GetComponentId(), componentE.GetComponentId(), componentF.GetComponentId(), componentG.GetComponentId(), componentH.GetComponentId()}
return fmt.Errorf("no storage found for components %v", componentsIds)
}
// If the entity has no component, simply add it the archetype
if entityRecord.archetypeId == 0 {
world.setArchetype(entityRecord, archetype)
} else {
oldArchetype := world.getArchetype(entityRecord)
if archetype.Id != oldArchetype.Id {
moveComponentsToArchetype(world, entityRecord, oldArchetype, archetype)
world.setArchetype(entityRecord, archetype)
}
}
storageA.add(archetype.Id, componentA)
storageB.add(archetype.Id, componentB)
storageC.add(archetype.Id, componentC)
storageD.add(archetype.Id, componentD)
storageE.add(archetype.Id, componentE)
storageF.add(archetype.Id, componentF)
storageG.add(archetype.Id, componentG)
storageH.add(archetype.Id, componentH)
return nil
}
func moveComponentsToArchetype(world *World, entityRecord entityRecord, oldArchetype *archetype, archetype *archetype) int {
var key, lastEntityKey int
for _, componentId := range oldArchetype.Type {
// tags are not movable
if componentId >= TAGS_INDICES {
continue
}
if !slices.Contains(archetype.Type, componentId) {
continue
}
s, _ := world.getStorageForComponentId(componentId)
if s != nil {
key = s.copy(oldArchetype.Id, archetype.Id, entityRecord.key)
s.moveLastToKey(oldArchetype.Id, entityRecord.key)
}
}
lastEntityKey = len(oldArchetype.entities) - 1
lastEntityId := oldArchetype.entities[lastEntityKey]
lastEntity := world.entities[lastEntityId]
lastEntity.key = entityRecord.key
world.entities[lastEntityId] = lastEntity
oldArchetype.entities[entityRecord.key] = lastEntityId
oldArchetype.entities = oldArchetype.entities[:lastEntityKey]
return key
}