-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecs.odin
1185 lines (998 loc) · 29.2 KB
/
ecs.odin
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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ecs
import "base:intrinsics"
import "base:runtime"
import "core:fmt"
import "core:mem"
import "core:reflect"
import "core:slice"
import "core:strings"
// Type Definitions
EntityID :: distinct u64
ArchetypeID :: u64
ComponentID :: EntityID
ComponentSet :: map[ComponentID]bool
// Errors
Error :: enum {
None,
EntityNotFound,
ComponentNotFound,
ArchetypeNotFound,
ComponentDataOutOfBounds,
InvalidComponentID,
EntityAlreadyExists,
ComponentAlreadyExists,
ComponentDisabled,
OperationFailed,
}
Result :: union($T: typeid) {
T,
Error,
}
// Component Type Registry
ComponentTypeInfo :: struct {
size: int,
type_info: ^reflect.Type_Info,
}
// Size of Type_Info
size_of_type :: proc(type_info: ^reflect.Type_Info) -> int {
return int(runtime.type_info_base(type_info).size)
}
get_component_id :: proc(world: ^World, T: typeid) -> (ComponentID, bool) {
// type_info := type_info_of(T)
// if component_id, ok := world.component_ids[type_info.id]; ok {
// return component_id, true
// }
return world.component_ids[T]
// return 0, false
}
World :: struct {
component_info: map[ComponentID]ComponentTypeInfo,
info_component: map[ComponentTypeInfo]ComponentID,
component_ids: map[typeid]ComponentID,
pair_component_ids: map[EntityID]ComponentID,
entity_index: map[EntityID]EntityInfo,
archetypes: map[ArchetypeID]^Archetype,
component_archetypes: map[ComponentID]map[ArchetypeID]^Archetype,
next_entity_id: EntityID,
queries: [dynamic]^Query,
}
// Entity Info stores the location of an entity within an archetype and its version
// TODO: store version in upper 16 bits of entity ID
// TODO: if entity ID is a Pair: store relationship target in lower 16 bits and relation id in upper 16 bits
Wildcard :: distinct struct {}
PairType :: struct($R, $T: typeid) {
relation: R,
target: T,
}
EntityInfo :: struct {
archetype: ^Archetype,
row: int,
version: u32,
}
Query :: struct {
component_ids: []ComponentID,
archetypes: [dynamic]^Archetype,
}
Archetype :: struct {
id: ArchetypeID,
component_ids: []ComponentID,
component_types: map[ComponentID]^reflect.Type_Info,
entities: [dynamic]EntityID,
tables: map[ComponentID][dynamic]byte,
tag_set: ComponentSet,
disabled_set: ComponentSet,
matching_queries: [dynamic]^Query,
add_edges: map[ComponentID]^Archetype,
remove_edges: map[ComponentID]^Archetype,
}
create_world :: proc() -> ^World {
world := new(World)
world.component_info = make(map[ComponentID]ComponentTypeInfo)
world.entity_index = make(map[EntityID]EntityInfo)
world.archetypes = make(map[ArchetypeID]^Archetype)
world.component_archetypes = make(map[ComponentID]map[ArchetypeID]^Archetype)
world.next_entity_id = EntityID(1)
world.queries = make([dynamic]^Query)
return world
}
delete_world :: proc(world: ^World) {
if world == nil {
return
}
for _, archetype in world.archetypes {
delete_archetype(archetype)
}
delete(world.component_info)
delete(world.info_component)
delete(world.component_ids)
delete(world.pair_component_ids)
delete(world.entity_index)
delete(world.archetypes)
for _, comp_map in world.component_archetypes {
delete(comp_map)
}
delete(world.component_archetypes)
delete(world.queries)
free(world)
}
register_component :: proc {
register_component_typeid,
// register_component_type,
}
register_component_typeid :: proc(world: ^World, T: typeid) -> ComponentID {
id := add_entity(world)
type_info := type_info_of(T)
info := ComponentTypeInfo {
size = size_of_type(type_info),
type_info = type_info,
}
world.component_info[id] = info
world.info_component[info] = id
world.component_ids[type_info.id] = id
return id
}
register_component_type :: proc(world: ^World, $T: typeid) -> ComponentID {
id := add_entity(world)
type_info := type_info_of(T)
info := ComponentTypeInfo {
size = size_of_type(type_info),
type_info = type_info,
}
world.component_info[id] = info
world.info_component[info] = id
world.component_ids[type_info.id] = id
return id
}
add_entity :: proc(world: ^World) -> EntityID {
entity := world.next_entity_id
world.next_entity_id = world.next_entity_id + 1
// TODO: pack version etc into ID bits
if info, exists := &world.entity_index[entity]; exists {
info.archetype = nil
info.row = -1
info.version += 1
} else {
world.entity_index[entity] = EntityInfo {
archetype = nil,
row = -1,
version = 0,
}
}
return entity
}
create_entity :: add_entity
remove_entity :: proc(world: ^World, entity: EntityID) {
info, ok := &world.entity_index[entity]
if !ok || info.archetype == nil {
return // Entity does not exist or has already been removed
}
archetype := info.archetype
row := info.row
last_row := len(archetype.entities) - 1
// Swap the entity to be removed with the last entity in the archetype
if row != last_row {
last_entity := archetype.entities[last_row]
archetype.entities[row] = last_entity
// Update all component tables
for component_id, &component_array in &archetype.tables {
if component_id in archetype.tag_set {
continue // Skip tags as they don't have data
}
type_info := archetype.component_types[component_id]
size := size_of_type(type_info)
// Move the last element's data to the removed entity's position
mem.copy(&component_array[row * size], &component_array[last_row * size], size)
}
// Update the moved entity's index
if moved_info, exists := &world.entity_index[last_entity]; exists {
moved_info.row = row
}
}
// Remove the last entity (which is now the entity we want to remove)
pop(&archetype.entities)
// Resize all component arrays
for component_id, &component_array in &archetype.tables {
if component_id in archetype.tag_set {
continue // Skip tags as they don't have data
}
type_info := archetype.component_types[component_id]
size := size_of_type(type_info)
resize(&component_array, len(archetype.entities) * size)
}
// Recycle the entity
info.archetype = nil
info.row = -1
info.version += 1
world.next_entity_id = entity
// If the archetype is now empty, remove it from the world
if len(archetype.entities) == 0 {
delete_key(&world.archetypes, archetype.id)
for component_id in archetype.component_ids {
if component_archetypes, ok := &world.component_archetypes[component_id]; ok {
delete_key(component_archetypes, archetype.id)
}
}
delete_archetype(archetype)
}
}
delete_entity :: remove_entity
entity_exists :: proc(world: ^World, entity: EntityID) -> bool {
info, ok := world.entity_index[entity]
return ok && info.archetype != nil
}
entity_alive :: entity_exists
has_component :: proc {
has_component_type,
has_component_instance,
}
has_component_type :: proc(world: ^World, entity: EntityID, $T: typeid) -> bool {
info, exists := world.entity_index[entity]
if !exists || info.archetype == nil {
return false
}
cid, ok := get_component_id(world, T)
if !ok {
return false
}
return slice.contains(info.archetype.component_ids, cid)
}
has_component_instance :: proc(world: ^World, entity: EntityID, component: $T) -> bool {
info, exists := world.entity_index[entity]
if !exists || info.archetype == nil {
return false
}
cid, ok := get_component_id(world, T)
if !ok {
return false
}
return slice.contains(info.archetype.component_ids, cid)
}
get_relation_typeid :: proc(c: PairType($R, $T)) -> typeid {
return typeid_of(R)
}
get_relation_type :: proc(c: PairType($R, $T)) -> typeid {
return R
}
add_component :: proc(world: ^World, entity: EntityID, component: $T) {
cid: ComponentID
ok: bool
when intrinsics.type_is_struct(
T,
) && intrinsics.type_has_field(T, "relation") && intrinsics.type_has_field(T, "target") {
relation_cid: ComponentID
target_cid: ComponentID
// Handle relation
when type_of(component.relation) == EntityID {
relation_cid = ComponentID(component.relation)
} else {
relation_cid, ok = get_component_id(world, type_of(component.relation))
if !ok {
relation_cid = register_component(world, type_of(component.relation))
}
}
// Handle target
when type_of(component.target) == EntityID {
target_cid = ComponentID(component.target)
} else {
target_cid, ok = get_component_id(world, type_of(component.target))
if !ok {
target_cid = register_component(world, type_of(component.target))
}
}
cid = hash_pair(relation_cid, target_cid)
// Register component for the hash pair
pair_type_info := type_info_of(T)
world.component_info[cid] = ComponentTypeInfo {
size = size_of(T),
type_info = pair_type_info,
}
} else {
cid, ok = get_component_id(world, T)
if !ok {
cid = register_component(world, T)
}
}
info := world.entity_index[entity]
old_archetype := info.archetype
new_archetype: ^Archetype
if old_archetype == nil {
new_component_ids := []ComponentID{cid}
tag_set := make(ComponentSet)
defer delete(tag_set)
if size_of(T) == 0 {
tag_set[cid] = true
}
new_archetype = get_or_create_archetype(world, new_component_ids, tag_set)
move_entity(world, entity, info, nil, new_archetype)
} else {
new_archetype, ok = old_archetype.add_edges[cid]
if !ok {
new_component_ids := make([]ComponentID, len(old_archetype.component_ids) + 1)
defer delete(new_component_ids)
copy(new_component_ids, old_archetype.component_ids)
new_component_ids[len(old_archetype.component_ids)] = cid
sort_component_ids(new_component_ids)
tag_set := make(ComponentSet)
defer delete(tag_set)
for k, v in old_archetype.tag_set {
tag_set[k] = v
}
if size_of(T) == 0 {
tag_set[cid] = true
}
new_archetype = get_or_create_archetype(world, new_component_ids, tag_set)
old_archetype.add_edges[cid] = new_archetype
}
move_entity(world, entity, info, old_archetype, new_archetype)
}
if size_of(T) > 0 {
index := world.entity_index[entity].row
local_component := component
when intrinsics.type_is_struct(
T,
) && intrinsics.type_has_field(T, "relation") && intrinsics.type_has_field(T, "target") {
add_component_data(
new_archetype,
cid,
rawptr(&local_component),
index,
type_of(component.relation),
)
} else {
add_component_data(new_archetype, cid, rawptr(&local_component), index, T)
}
}
}
add_component_data :: proc(
archetype: ^Archetype,
component_id: ComponentID,
component: rawptr,
index: int,
$T: typeid,
) {
table, ok := &archetype.tables[component_id]
if !ok {
return
}
size := size_of(T)
if size == 0 {
return // Skip tags as they don't have data
}
required_size := (index + 1) * size
if len(table^) < required_size {
resize(table, required_size)
}
offset := index * size
mem.copy(&table^[offset], component, size)
}
remove_component_id :: proc(ids: []ComponentID, cid: ComponentID) -> []ComponentID {
new_ids := make([dynamic]ComponentID, 0, len(ids) - 1)
for id in ids {
if u64(id) != u64(cid) {
append(&new_ids, id)
}
}
return new_ids[:]
}
remove_component :: proc(world: ^World, entity: EntityID, $T: typeid) {
cid := get_component_id(world, T)
info := world.entity_index[entity]
old_archetype := info.archetype
if old_archetype == nil {
return // Entity does not have this component
}
// Check if the component exists in the archetype
if !slice.contains(old_archetype.component_ids, cid) {
return // Component not present
}
// Use the remove_edges graph to get the next archetype
new_archetype, ok := old_archetype.remove_edges[cid]
if !ok {
// If the edge doesn't exist, create a new archetype
new_component_ids := remove_component_id(old_archetype.component_ids, cid)
defer delete(new_component_ids)
new_tag_set := make(map[ComponentID]bool)
for k, v in old_archetype.tag_set {
if k != cid {
new_tag_set[k] = v
}
}
new_archetype = get_or_create_archetype(world, new_component_ids, new_tag_set)
old_archetype.remove_edges[cid] = new_archetype
}
// Move entity to new archetype
move_entity(world, entity, info, old_archetype, new_archetype)
}
disable_component :: proc(world: ^World, entity: EntityID, $T: typeid) {
cid, ok := get_component_id(world, T)
if !ok {
return // Component not registered
}
info, exists := world.entity_index[entity]
if !exists {
return // Entity not found
}
archetype := info.archetype
if archetype == nil {
return // Entity has no archetype
}
if !slice.contains(archetype.component_ids, cid) {
return // Component not present in archetype
}
archetype.disabled_set[cid] = true
}
enable_component :: proc(world: ^World, entity: EntityID, $T: typeid) {
component_id, ok := get_component_id(world, T)
if !ok {
return // Component not registered
}
info, exists := world.entity_index[entity]
if !exists {
return // Entity not found
}
archetype := info.archetype
if archetype == nil {
return // Entity has no archetype
}
if !slice.contains(archetype.component_ids, component_id) {
return // Component not present in archetype
}
delete_key(&archetype.disabled_set, component_id)
}
move_entity :: proc(
world: ^World,
entity: EntityID,
info: EntityInfo,
old_archetype: ^Archetype,
new_archetype: ^Archetype,
) {
// Add to new archetype
new_row := len(new_archetype.entities)
append(&new_archetype.entities, entity)
// Update entity index
world.entity_index[entity] = EntityInfo {
archetype = new_archetype,
row = new_row,
version = info.version,
}
// Resize and copy shared component data
for component_id in new_archetype.component_ids {
if component_id in new_archetype.tag_set {
continue // Tags don't have data, so we skip them
}
new_table, ok := &new_archetype.tables[component_id]
if !ok {
new_table^ = make([dynamic]byte)
new_archetype.tables[component_id] = new_table^
}
type_info := new_archetype.component_types[component_id]
size := size_of_type(type_info)
// Ensure new_table is big enough
if len(new_table^) < (new_row + 1) * size {
resize(new_table, (new_row + 1) * size)
}
new_offset := new_row * size
if old_archetype != nil {
if old_table, exists := old_archetype.tables[component_id];
exists && len(old_table) > 0 {
// Copy component data from old to new
old_offset := info.row * size
if old_offset < len(old_table) && new_offset < len(new_table^) {
mem.copy(&new_table[new_offset], &old_table[old_offset], size)
}
} else {
// Initialize new component data to zero
mem.zero(&new_table[new_offset], size)
}
} else {
// Initialize new component data to zero
mem.zero(&new_table[new_offset], size)
}
}
// Remove from old archetype if necessary
if old_archetype != nil && info.row >= 0 {
old_row := info.row
last_index := len(old_archetype.entities) - 1
if old_row != last_index {
// Swap with the last entity
last_entity := old_archetype.entities[last_index]
old_archetype.entities[old_row] = last_entity
// Update component tables
for component_id, &table in &old_archetype.tables {
if component_id in old_archetype.tag_set {
continue // Skip tags as they don't have data
}
type_info := old_archetype.component_types[component_id]
size := size_of_type(type_info)
// Move last element to the removed position
mem.copy(&table[old_row * size], &table[last_index * size], size)
// Shrink the table
resize(&table, (last_index) * size)
}
// Update entity index for the moved entity
if entity_info, exists := &world.entity_index[last_entity]; exists {
entity_info.row = old_row
}
} else {
// If it's the last entity, just remove it from all tables
for component_id, &table in &old_archetype.tables {
if component_id in old_archetype.tag_set {
continue // Skip tags as they don't have data
}
type_info := old_archetype.component_types[component_id]
size := size_of_type(type_info)
// Shrink the table
resize(&table, (last_index) * size)
}
}
// Remove the last entity
pop(&old_archetype.entities)
}
// If the old archetype exists and has 0 entities, remove it
if old_archetype != nil && len(old_archetype.entities) == 0 {
delete_key(&world.archetypes, old_archetype.id)
delete_archetype(old_archetype)
// TODO: remove from queries
}
}
get_or_create_archetype :: proc(
world: ^World,
component_ids: []ComponentID,
tag_set: map[ComponentID]bool,
) -> ^Archetype {
archetype_id := hash_archetype(component_ids, tag_set)
archetype, exists := world.archetypes[archetype_id]
if exists {
return archetype
}
// Create new archetype
archetype = new(Archetype)
archetype.id = archetype_id
archetype.component_ids = slice.clone(component_ids)
archetype.entities = make([dynamic]EntityID)
archetype.tables = make(map[ComponentID][dynamic]byte)
archetype.component_types = make(map[EntityID]^reflect.Type_Info)
archetype.tag_set = make(map[ComponentID]bool)
for k, v in tag_set {
archetype.tag_set[k] = v
}
archetype.disabled_set = make(map[ComponentID]bool)
archetype.add_edges = make(map[ComponentID]^Archetype)
archetype.remove_edges = make(map[ComponentID]^Archetype)
// Initialize component arrays and update component archetypes
for cid in component_ids {
component_info := world.component_info[cid]
if component_info.size > 0 {
// Only allocate storage for components with data
new_table := make([dynamic]byte)
archetype.tables[cid] = new_table
archetype.component_types[cid] = component_info.type_info
} else {
// For tags, no component array is needed
archetype.tag_set[cid] = true
}
}
// Add to archetypes map
world.archetypes[archetype_id] = archetype
return archetype
}
delete_archetype :: proc(archetype: ^Archetype) {
if archetype == nil {
return
}
delete(archetype.component_ids)
delete(archetype.entities)
for _, array in archetype.tables {
delete(array)
}
delete(archetype.tables)
delete(archetype.component_types)
delete(archetype.tag_set)
delete(archetype.disabled_set)
delete(archetype.add_edges)
delete(archetype.remove_edges)
free(archetype)
}
sort_component_ids :: proc(ids: []ComponentID) {
// Simple insertion sort for small arrays
for i := 1; i < len(ids); i += 1 {
key := ids[i]
j := i - 1
for j >= 0 && ids[j] > key {
ids[j + 1] = ids[j]
j -= 1
}
ids[j + 1] = key
}
}
hash_archetype :: proc(
component_ids: []ComponentID,
tag_set: map[ComponentID]bool,
) -> ArchetypeID {
h := u64(14695981039346656037) // FNV-1a 64-bit offset basis
// Sort and hash component_ids
sorted_component_ids := make([]ComponentID, len(component_ids))
defer delete(sorted_component_ids)
copy(sorted_component_ids, component_ids)
sort_component_ids(sorted_component_ids)
for id in sorted_component_ids {
h = (h ~ u64(id)) * 1099511628211
}
// Collect and sort tag_set ComponentIDs
sorted_tags := make([dynamic]ComponentID, len(tag_set))
defer delete(sorted_tags)
for id in tag_set {
append(&sorted_tags, id)
}
sort_component_ids(sorted_tags[:])
// Hash sorted tag_set
for id in sorted_tags {
h = (h ~ u64(id)) * 1099511628211
}
return ArchetypeID(h)
}
get_component :: proc(world: ^World, entity: EntityID, component: union {
typeid,
PairType(typeid, typeid),
}) -> $T {
info := world.entity_index[entity]
cid: ComponentID
ok: bool
relation_type: typeid
switch c in component {
case typeid:
cid, ok = get_component_id(world, c)
relation_type = c
case PairType:
relation_cid, _ := get_component_id(world, type_of(c.relation))
target_cid: ComponentID
#partial switch target in c.target {
case EntityID:
target_cid = ComponentID(target)
case:
target_cid, _ = get_component_id(world, type_of(c.target))
}
cid = hash_pair(relation_cid, target_cid)
relation_type = type_of(c.relation)
ok = true
}
// if !ok {
// return T{}
// }
archetype := info.archetype
table, exists := archetype.tables[cid]
// if !exists {
// return T{}
// }
row := info.row
component_size := size_of(T)
num_components := len(table) / component_size
components := (cast(^[dynamic]T)(&table))[:num_components]
return components[row]
}
get_table :: proc {
get_table_same,
get_table_cast,
}
get_table_same :: proc(world: ^World, archetype: ^Archetype, $Component: typeid) -> []Component {
cid, ok := get_component_id(world, Component)
if !ok {
return nil
}
table, exists := archetype.tables[cid]
if !exists {
return nil
}
component_size := size_of(Component)
num_components := len(table) / component_size
return (cast(^[dynamic]Component)(&table))[:num_components]
}
get_table_cast :: proc(
world: ^World,
archetype: ^Archetype,
$Component: typeid,
$CastTo: typeid,
) -> []CastTo {
cid, ok := get_component_id(world, Component)
if !ok {
return nil
}
table, exists := archetype.tables[cid]
if !exists {
return nil
}
component_size := size_of(CastTo)
num_components := len(table) / component_size
return (cast(^[dynamic]CastTo)(&table))[:num_components]
}
get_table_row :: proc(world: ^World, entity_id: EntityID) -> int {
if info, ok := world.entity_index[entity_id]; ok {
return info.row
}
return -1
}
// Query builder structure
QueryBuilder :: struct {
world: ^World,
terms: [dynamic]Term,
}
Term :: union {
HasTerm,
NotTerm,
PairType(typeid, typeid),
PairType(EntityID, EntityID),
PairType(EntityID, typeid),
PairType(typeid, EntityID),
}
HasTerm :: struct {
component: union {
typeid,
PairType(typeid, typeid),
PairType(EntityID, EntityID),
PairType(EntityID, typeid),
PairType(typeid, EntityID),
},
}
NotTerm :: struct {
component: union {
typeid,
PairType(typeid, typeid),
PairType(EntityID, EntityID),
PairType(EntityID, typeid),
PairType(typeid, EntityID),
},
}
// Initialize a new query builder
new_query :: proc(world: ^World) -> QueryBuilder {
return QueryBuilder{world = world, terms = make([dynamic]Term, context.temp_allocator)}
}
execute :: proc(q: ^QueryBuilder) -> []^Archetype {
if len(q.terms) == 0 {
return nil
}
has_terms: [dynamic]ComponentID
not_terms: [dynamic]ComponentID
defer delete(has_terms)
defer delete(not_terms)
for term in q.terms {
cid: ComponentID
ok: bool
switch t in term {
case HasTerm:
switch component in t.component {
case typeid:
cid, ok = get_component_id_from_term_typeid(q.world, component)
case PairType(typeid, typeid):
cid, ok = get_component_id_from_term_pair_typeid_typeid(q.world, component)
case PairType(EntityID, EntityID):
cid, ok = get_component_id_from_term_pair_entity_entity(q.world, component)
case PairType(EntityID, typeid):
cid, ok = get_component_id_from_term_pair_entity_typeid(q.world, component)
case PairType(typeid, EntityID):
cid, ok = get_component_id_from_term_pair_typeid_entity(q.world, component)
}
if ok {
fmt.println("has term hit")
append(&has_terms, cid)
}
case NotTerm:
switch component in t.component {
case typeid:
cid, ok = get_component_id_from_term_typeid(q.world, component)
case PairType(typeid, typeid):
cid, ok = get_component_id_from_term_pair_typeid_typeid(q.world, component)
case PairType(EntityID, EntityID):
cid, ok = get_component_id_from_term_pair_entity_entity(q.world, component)
case PairType(EntityID, typeid):
cid, ok = get_component_id_from_term_pair_entity_typeid(q.world, component)
case PairType(typeid, EntityID):
cid, ok = get_component_id_from_term_pair_typeid_entity(q.world, component)
}
if ok {
append(¬_terms, cid)
}
case PairType(typeid, typeid):
cid, ok = get_component_id_from_pair(q.world, t)
if ok {
append(&has_terms, cid)
}
case PairType(EntityID, EntityID):
cid, ok = get_component_id_from_pair(q.world, t)
if ok {
append(&has_terms, cid)
}
case PairType(EntityID, typeid):
cid, ok = get_component_id_from_pair(q.world, t)
if ok {
append(&has_terms, cid)
}
case PairType(typeid, EntityID):
cid, ok = get_component_id_from_pair(q.world, t)
fmt.println(cid)
if ok {
append(&has_terms, cid)
}
}
if !ok {
return []^Archetype{}
}
}
result := make([dynamic]^Archetype, context.temp_allocator)
for _, archetype in q.world.archetypes {
all_has_present := true
for id in has_terms {
if !slice.contains(archetype.component_ids, id) {
all_has_present = false
break
}
}
no_not_present := true
for id in not_terms {
if slice.contains(archetype.component_ids, id) {
no_not_present = false
break
}
}
if all_has_present && no_not_present {
append(&result, archetype)
}
}
return result[:]
}
get_component_id_from_term :: proc {
get_component_id_from_term_typeid,
get_component_id_from_term_pair_typeid_typeid,
get_component_id_from_term_pair_typeid_entity,
get_component_id_from_term_pair_entity_typeid,
get_component_id_from_term_pair_entity_entity,
}
get_component_id_from_term_typeid :: proc(world: ^World, component: typeid) -> (ComponentID, bool) {
return get_component_id(world, component)
}
get_component_id_from_term_pair_typeid_typeid :: proc(world: ^World, component: PairType(typeid, typeid)) -> (ComponentID, bool) {
return get_component_id_from_pair(world, component)
}