-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityManager.h
838 lines (702 loc) · 22 KB
/
EntityManager.h
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
#define MaxUnitInstructions 100
enum UnitInstruction
{
Unit_Wait,
Unit_MoveUp,
Unit_MoveDown,
Unit_MoveLeft,
Unit_MoveRight,
Unit_FlipBit,
Unit_Instruction_Count,
};
DefineDynamicArray(UnitInstruction);
static String UnitInstructionToString(UnitInstruction step)
{
switch (step)
{
case Unit_Wait:
{
return S("Wait");
}break;
case Unit_MoveUp:
{
return S("Up");
}break;
case Unit_MoveDown:
{
return S("Down");
}break;
case Unit_MoveLeft:
{
return S("Left");
}break;
case Unit_MoveRight:
{
return S("Right");
}break;
default:
{
return S("Unknown");
}break;
}
}
struct UnitData
{
u32 serial;
UnitInstructionDynamicArray instructions;
u32 at;
f32 t;
};
DefineDynamicArray(UnitData);
struct BitData
{
u32 serial;
b32 value;
//b32 inUse;
};
DefineDynamicArray(BitData);
struct EntitySerialResult
{
u16 type; // this could be a u8... but there are no u 24... maybe we find some kind of static data...
u16 index;
};
DefineDynamicArray(EntitySerialResult);
static EntitySerialResult InvalidEntitySerial___()
{
return {0xFFFF, 0xFFFF};
}
#define InvalidEntitySerial InvalidEntitySerial___()
struct TileOctTreeNode
{
b32 isLeaf;
AABBi bound;
union
{
TileOctTreeNode *next; // for clear list
TileOctTreeNode *children[8];
struct
{
TileOctTreeNode *TopUpLeft; // top = minZ, Up = minY, Left = minX
TileOctTreeNode *TopUpRight;
TileOctTreeNode *TopDownLeft;
TileOctTreeNode *TopDownRight;
TileOctTreeNode *BotUpLeft;
TileOctTreeNode *BotUpRight;
TileOctTreeNode *BotDownLeft;
TileOctTreeNode *BotDownRight;
};
u32 entitySerials[16];
};
};
struct TileOctTree
{
TileOctTreeNode root;
TileOctTreeNode *freeList;
Arena *arena;
};
static TileOctTree InitOctTree(Arena *currentStateArena, u32 initialCapacity)
{
TileOctTree ret;
ret.root.bound = CreateAABBi(V3i(-128, -128, -128), V3i(128, 128, 128));
ret.root.isLeaf = true;
for (u32 i = 0; i < 16; i++)
{
ret.root.entitySerials[i] = 0xFFFFFFFF;
}
ret.arena = currentStateArena;
ret.freeList = NULL;
for (u32 i = 0; i < initialCapacity; i++)
{
TileOctTreeNode *node = PushStruct(currentStateArena, TileOctTreeNode);
node->next = ret.freeList;
ret.freeList = node;
}
return ret;
}
static TileOctTreeNode *GetANode(TileOctTree *tree)
{
if (tree->freeList)
{
TileOctTreeNode *ret = tree->freeList;
tree->freeList = tree->freeList->next;
*ret = {};
return ret;
}
return PushZeroStruct(tree->arena, TileOctTreeNode);
}
static TileOctTreeNode *GetALeaf(TileOctTree *tree)
{
if (tree->freeList)
{
TileOctTreeNode *ret = tree->freeList;
tree->freeList = tree->freeList->next;
for (u32 i = 0; i < 16; i++)
{
ret->entitySerials[i] = 0xFFFFFFFF;
}
ret->isLeaf = true;
return ret;
}
TileOctTreeNode *ret = PushStruct(tree->arena, TileOctTreeNode);
for (u32 i = 0; i < 16; i++)
{
ret->entitySerials[i] = 0xFFFFFFFF;
}
ret->isLeaf = true;
return ret;
}
#if 0
static void RemoveAllEntities(TileOctTreeNode *node)
{
if (node->isLeaf)
{
for (u32 i = 0; i < 16; i++)
{
node->entitySerials[i] = 0xFFFFFFFF;
}
return;
}
for (u32 i = 0; i < 8; i++)
{
RemoveAllEntities(node->children[i]);
}
}
static void RemoveAllEntities(TileOctTree *tree)
{
RemoveAllEntities(&tree->root);
}
#endif
struct EntityManager
{
String levelName;
TileOctTree entityTree;
UnitDataDynamicArray unitData;
AnimationStateDynamicArray animationStates; // parralel to unitArray
BitDataDynamicArray bitData;
BuddyAllocator alloc;
union
{
EntityDynamicArray entityArrays[Entity_Count];
struct
{
EntityDynamicArray noneArray;
EntityDynamicArray unitArray;
EntityDynamicArray wallArray;
EntityDynamicArray bitArray;
};
};
u32 serializer;
EntitySerialResultDynamicArray entitySerialMap; // this is just huge and that fine???
};
static Entity *GetEntity(EntityManager *entityManager, u32 serial)
{
EntitySerialResult result = entityManager->entitySerialMap[serial];
Entity *ret = entityManager->entityArrays[result.type] + result.index;
return ret;
}
static void RemoveEntityFromTree(EntityManager *entityManager, Entity *e)
{
if (!(e->flags & EntityFlag_InTree)) return;
TileOctTree *tree = &entityManager->entityTree;
TileOctTreeNode *cur = &tree->root;
e->flags &= ~EntityFlag_InTree;
while (!cur->isLeaf)
{
for (u32 i = 0; i < 8; i++) // lazy
{
if (PointInAABBi(cur->children[i]->bound, e->physicalPos))
{
cur = cur->children[i];
break;
}
}
}
for (u32 i = 0; i < 16; i++)
{
if (e->serial == cur->entitySerials[i])
{
cur->entitySerials[i] = 0xFFFFFFFF;
return;
}
}
Die;
}
static EntityPtrArray GetEntities(EntityManager *entityManager, v3i tile, u64 flags = 0)
{
TileOctTreeNode *cur = &entityManager->entityTree.root;
Assert(PointInAABBi(cur->bound, tile));
while (!cur->isLeaf)
{
for (u32 i = 0; i < ArrayCount(cur->children); i++)
{
if (PointInAABBi(cur->children[i]->bound, tile))
{
cur = cur->children[i];
break;
}
}
}
BeginArray(frameArena, EntityPtr, ret);
for (u32 i = 0; i < ArrayCount(cur->entitySerials); i++)
{
if (cur->entitySerials[i] == 0xFFFFFFFF) continue;
Entity *e = GetEntity(entityManager, cur->entitySerials[i]);
if (e->physicalPos == tile && ((e->flags & flags) == flags))
{
*PushStruct(frameArena, EntityPtr) = e;
}
}
EndArray(frameArena, EntityPtr, ret);
return ret;
}
static void InsertEntity(EntityManager *entityManager, TileOctTreeNode *cur, Entity *e)
{
TimedBlock;
if (!e) return;
TileOctTree *tree = &entityManager->entityTree;
while (!cur->isLeaf)
{
for (u32 i = 0; i < ArrayCount(cur->children); i++) // lazy
{
if (PointInAABBi(cur->children[i]->bound, e->physicalPos))
{
cur = cur->children[i];
break;
}
}
}
Assert(cur);
Assert(cur->isLeaf);
//check for place allready taken, this is the only false case
#if 0
for (u32 i = 0; i < 16; i++)
{
if (cur->entitySerials[i] != 0xFFFFFFFF)
{
Entity *other = GetEntities(entityManager, cur->entitySerials[i]);
if (other->physicalPos == e->physicalPos)
{
if (swap) cur->entitySerials[i] = e->serialNumber;
return other;
}
}
}
#endif
// try to insert
for (u32 i = 0; i < ArrayCount(cur->entitySerials); i++)
{
if (cur->entitySerials[i] == 0xFFFFFFFF)
{
cur->entitySerials[i] = e->serial;
return;
}
}
u32 oldSize = (cur->bound.maxDim.x - cur->bound.minDim.x);
u32 newSize = oldSize >> 1;
Assert(newSize);
Entity *currentEntities[ArrayCount(cur->entitySerials) + 1];
for (u32 i = 0; i < ArrayCount(cur->entitySerials); i++)
{
currentEntities[i] = (cur->entitySerials[i] != 0xFFFFFFFF) ? GetEntity(entityManager, cur->entitySerials[i]) : NULL;
}
currentEntities[ArrayCount(cur->entitySerials)] = e;
b32 allTheSame = true;
for (u32 i = 0; i < ArrayCount(cur->entitySerials); i++)
{
if (e->physicalPos != currentEntities[i]->physicalPos)
{
allTheSame = false;
break;
}
}
if (allTheSame)
{
Die;
e->physicalPos -= V3i(0, 0, 1);
InsertEntity(entityManager, &entityManager->entityTree.root, e);
return;
}
//split
TileOctTreeNode *TopUpLeft = GetALeaf(tree);
TopUpLeft->bound = CreateAABBi(cur->bound.minDim, cur->bound.minDim + V3i(newSize, newSize, newSize));
cur->TopUpLeft = TopUpLeft;
TileOctTreeNode *TopUpRight = GetALeaf(tree);
TopUpRight->bound = CreateAABBi(cur->bound.minDim + V3i(newSize, 0, 0), cur->bound.minDim + V3i(oldSize, newSize, newSize));
cur->TopUpRight = TopUpRight;
TileOctTreeNode *TopDownLeft = GetALeaf(tree);
TopDownLeft->bound = CreateAABBi(cur->bound.minDim + V3i(0, newSize, 0), cur->bound.minDim + V3i(newSize, oldSize, newSize));
cur->TopDownLeft = TopDownLeft;
TileOctTreeNode *TopDownRight = GetALeaf(tree);
TopDownRight->bound = CreateAABBi(cur->bound.minDim + V3i(newSize, newSize, 0), cur->bound.minDim + V3i(oldSize, oldSize, newSize));
cur->TopDownRight = TopDownRight;
TileOctTreeNode *BotUpLeft = GetALeaf(tree);
BotUpLeft->bound = CreateAABBi(cur->bound.minDim + V3i(0, 0, newSize), cur->bound.minDim + V3i(newSize, newSize, oldSize));
cur->BotUpLeft = BotUpLeft;
TileOctTreeNode *BotUpRight = GetALeaf(tree);
BotUpRight->bound = CreateAABBi(cur->bound.minDim + V3i(newSize, 0, newSize), cur->bound.minDim + V3i(oldSize, newSize, oldSize));
cur->BotUpRight = BotUpRight;
TileOctTreeNode *BotDownLeft = GetALeaf(tree);
BotDownLeft->bound = CreateAABBi(cur->bound.minDim + V3i(0, newSize, newSize), cur->bound.minDim + V3i(newSize, oldSize, oldSize));
cur->BotDownLeft = BotDownLeft;
TileOctTreeNode *BotDownRight = GetALeaf(tree);
BotDownRight->bound = CreateAABBi(cur->bound.minDim + V3i(newSize, newSize, newSize), cur->bound.minDim + V3i(oldSize, oldSize, oldSize));
cur->BotDownRight = BotDownRight;
cur->isLeaf = false;
for (u32 i = 0; i < 17; i++)
{
InsertEntity(entityManager, cur, currentEntities[i]);
}
return;
}
static void ResetTreeHelper(EntityManager *entityManager, TileOctTree *tree, TileOctTreeNode *node)
{
if (node->isLeaf) return;
for (u32 i = 0; i < 8; i++)
{
auto c = node->children[i];
ResetTreeHelper(entityManager, tree, c);
c->next = tree->freeList;
tree->freeList = c;
}
}
// not used right now
static void ResetTree(EntityManager *entityManager, TileOctTree *tree) // todo speed can this be lineraized?
{
ResetTreeHelper(entityManager, tree, &tree->root);
tree->root.isLeaf = true;
for (u32 i = 0; i < ArrayCount(tree->root.entitySerials); i++)
{
tree->root.entitySerials[i] = 0xFFFFFFFF;
}
for(u32 i = 1; i < Entity_Count; i++)
{
For(entityManager->entityArrays[i])
{
it->flags &= ~EntityFlag_InTree;
}
}
}
static void InsertEntity(EntityManager *entityManager, Entity *e)
{
Assert(!(e->flags & EntityFlag_InTree));
TileOctTree *tree = &entityManager->entityTree;
TileOctTreeNode *cur = &tree->root;
Assert(PointInAABBi(cur->bound, e->physicalPos));
// change this to isNotLeaf, to not have to do this not?
InsertEntity(entityManager, cur, e);
e->flags |= EntityFlag_InTree;
}
static void MoveEntityInTree(EntityManager *entityManager, Entity *e, v3i by)
{
RemoveEntityFromTree(entityManager, e);
e->physicalPos += by;
InsertEntity(entityManager, e);
}
static Entity *CreateEntityInternal(EntityManager *entityManager, EntityData data)
{
Entity ret;
ApplyEntityDataToEntity(&ret, &data);
ret.serial = entityManager->serializer++;
ret.initialPos = ret.physicalPos;
Assert(ret.type < Entity_Count);
u32 arrayIndex = ArrayAdd(&entityManager->entityArrays[ret.type], ret);
Assert(ret.serial == entityManager->entitySerialMap.amount);
EntitySerialResult toAdd;
toAdd.index = (u16)arrayIndex;
toAdd.type = (u16)ret.type;
ArrayAdd(&entityManager->entitySerialMap, toAdd);
if (ret.flags) // entity != none? // move this out?
{
InsertEntity(entityManager, entityManager->entityArrays[ret.type] + arrayIndex);
}
return entityManager->entityArrays[ret.type] + arrayIndex;
};
static void RestoreEntity(EntityManager *entityManager, EntityData data)
{
Entity ret;
ApplyEntityDataToEntity(&ret, &data);
u32 arrayIndex = ArrayAdd(&entityManager->entityArrays[ret.type], ret);
EntitySerialResult toAdd;
toAdd.index = (u16)arrayIndex;
toAdd.type = (u16)ret.type;
Assert(entityManager->entitySerialMap[ret.serial].index == 0xFFFF);
entityManager->entitySerialMap[ret.serial] = toAdd;
if (ret.flags) // entity != none?
{
InsertEntity(entityManager, entityManager->entityArrays[ret.type] + arrayIndex);
}
}
static void RemoveEntity(EntityManager *entityManager, u32 serial)
{
EntitySerialResult result = entityManager->entitySerialMap[serial];
Assert(result.type < Entity_Count);
Entity *toRemove = entityManager->entityArrays[result.type] + result.index;
RemoveEntityFromTree(entityManager, toRemove);
u16 index = result.index;
entityManager->entitySerialMap[serial] = InvalidEntitySerial;
u32 lastIndex = entityManager->entityArrays[result.type].amount - 1;
if (index != lastIndex)
{
u32 serialNumberToChange = entityManager->entityArrays[result.type][lastIndex].serial;
entityManager->entitySerialMap[serialNumberToChange].index = index;
}
UnorderedRemove(&entityManager->entityArrays[result.type], index);
}
static void ResetEntityManager(EntityManager *entityManager)
{
For(entityManager->unitData)
{
it->t = 1.0f;
it->at = 0;
}
For(entityManager->unitArray)
{
RemoveEntityFromTree(entityManager, it);
}
For(entityManager->unitArray)
{
it->physicalPos = it->initialPos;
it->flags &= ~EntityFlag_FrameResetFlag;
InsertEntity(entityManager, it);
it->visualPos = V3(it->initialPos);
}
}
static u64 GetStandardFlags(EntityType type)
{
switch (type)
{
case Entity_None:
{
return 0;
}break;
case Entity_Dude:
{
return EntityFlag_BlocksUnit | EntityFlag_SupportsUnit | EntityFlag_IsDynamic;
}break;
case Entity_Wall:
{
return EntityFlag_BlocksUnit | EntityFlag_SupportsUnit;
}break;
case Entity_Bit:
{
return EntityFlag_Interactable; // ?, they do not really have to get Updated I guess.
}break;
InvalidDefaultCase;
};
return 0;
}
static Entity CreateDude(EntityManager *entityManager, v3i pos, f32 scale = 1.0f, Quaternion orientation = {1, 0, 0, 0}, v3 offset = V3(), v4 color = V4(1, 1, 1, 1), u32 meshId = 0xFFFFFFFF)
{
EntityData entityData;
entityData.orientation = orientation;
entityData.physicalPos = pos;
entityData.offset = offset;
entityData.color = color;
entityData.scale = scale;
entityData.flags = GetStandardFlags(Entity_Dude);
entityData.type = Entity_Dude;
entityData.meshId = meshId;
Entity *e = CreateEntityInternal(entityManager, entityData);
UnitData data = {};
data.serial = e->serial;
data.instructions = UnitInstructionCreateDynamicArray(&entityManager->alloc);
ArrayAdd(&entityManager->unitData, data);
AnimationState anim = {};
anim.meshId = e->meshId;
anim.serial = e->serial;
ForC(anim.animations)
{
it->animationId = 0xFFFFFFFF;
}
ForC(anim.iks)
{
it->boneIndex = 0xFFFFFFFF;
}
ArrayAdd(&entityManager->animationStates, anim);
return *e;
}
static Entity CreateWall(EntityManager *entityManager, v3i pos, f32 scale = 1.0f, Quaternion orientation = {1, 0, 0, 0}, v3 offset = V3(), v4 color = V4(1, 1, 1, 1), u32 meshId = 0xFFFFFFFF)
{
EntityData entityData;
entityData.orientation = orientation;
entityData.physicalPos = pos;
entityData.offset = offset;
entityData.color = color;
entityData.scale = scale;
entityData.type = Entity_Wall;
entityData.flags = GetStandardFlags(Entity_Wall);
entityData.meshId = meshId;
Entity *e = CreateEntityInternal(entityManager, entityData);
BitData data = {};
data.serial = e->serial;
ArrayAdd(&entityManager->bitData, data);
return *e;
}
static Entity CreateBit(EntityManager *entityManager, v3i pos, f32 scale = 1.0f, Quaternion orientation = {1, 0, 0, 0}, v3 offset = V3(), v4 color = V4(1, 1, 1, 1), u32 meshId = 0xFFFFFFFF)
{
EntityData entityData;
entityData.orientation = orientation;
entityData.physicalPos = pos;
entityData.offset = offset;
entityData.color = color;
entityData.scale = scale;
entityData.type = Entity_Bit;
entityData.flags = GetStandardFlags(Entity_Bit);
entityData.meshId = meshId;
return *CreateEntityInternal(entityManager, entityData);
}
static Entity CreateEntityNone(EntityManager *entityManager, v3i pos, f32 scale = 1.0f, Quaternion orientation = {1, 0, 0, 0}, v3 offset = V3(), v4 color = V4(1, 1, 1, 1), u32 meshId = 0xFFFFFFFF)
{
EntityData entityData;
entityData.orientation = orientation;
entityData.physicalPos = pos;
entityData.offset = offset;
entityData.color = color;
entityData.scale = scale;
entityData.type = Entity_None;
entityData.flags = GetStandardFlags(Entity_None);
entityData.meshId = meshId;
return *CreateEntityInternal(entityManager, entityData);
}
// this seems pretty bad in retrospect
static Entity CreateEntity(EntityManager *entityManager, EntityType type, u32 meshId, v3i pos, f32 scale, Quaternion orientation, v3 offset, v4 color)
{
switch (type)
{
case Entity_None:
{
return CreateEntityNone(entityManager, pos, scale, orientation, offset, color, meshId);
}break;
case Entity_Dude:
{
return CreateDude(entityManager, pos, scale, orientation, offset, color, meshId);
}break;
case Entity_Wall:
{
return CreateWall(entityManager, pos, scale, orientation, offset, color, meshId);
}break;
case Entity_Bit:
{
return CreateBit(entityManager, pos, scale, orientation, offset, color, meshId);
}break;
InvalidDefaultCase;
}
return {};
}
static void InitEntityManager(EntityManager *entityManager, Arena *currentStateArena, Level *level)
{
entityManager->levelName = CopyString(level->name, currentStateArena);
entityManager->serializer = 0;
entityManager->entityTree = InitOctTree(currentStateArena, 100); // todo hardcoded.
entityManager->alloc = CreateBuddyAllocator(currentStateArena, MegaBytes(64), 1024);
// todo something something, probabaly do not allocate level->entites for each one...
entityManager->noneArray = EntityCreateDynamicArray(&entityManager->alloc, 10);
entityManager->unitArray = EntityCreateDynamicArray(&entityManager->alloc, 10);
entityManager->wallArray = EntityCreateDynamicArray(&entityManager->alloc, level->entities.amount);
entityManager->bitArray = EntityCreateDynamicArray(&entityManager->alloc, 10);
entityManager->animationStates = AnimationStateCreateDynamicArray(&entityManager->alloc);
entityManager->entitySerialMap = EntitySerialResultCreateDynamicArray(&entityManager->alloc, level->entities.amount);
entityManager->unitData = UnitDataCreateDynamicArray(&entityManager->alloc);
entityManager->bitData = BitDataCreateDynamicArray(&entityManager->alloc);
For(level->entities)
{
CreateEntity(entityManager, it->type, it->meshId, it->physicalPos, it->scale, it->orientation, it->offset, it->color);
}
#if 1
RandomSeries series = GetRandomSeries();
For(entityManager->unitData)
{
for(u32 i = 0; i < 10; i++)
{
ArrayAdd(&it->instructions, (UnitInstruction)((RandomU32(&series)>>10) % Unit_Instruction_Count));
}
}
#endif
}
static v3i GetAdvanceForOneStep(UnitInstruction step)
{
v3i advance;
switch (step)
{
case Unit_Wait:
{
advance = {};
}break;
case Unit_MoveDown:
{
advance = V3i(0, -1, 0);
}break;
case Unit_MoveUp:
{
advance = V3i(0, 1, 0);
}break;
case Unit_MoveLeft:
{
advance = V3i(-1, 0, 0);
}break;
case Unit_MoveRight:
{
advance = V3i(1, 0, 0);
}break;
default:
{
advance = {}; // todo robustness, should we fill this out?
}break;
}
return advance;
}
static v3i ToTilemapDir(v3 d)
{
i32 xSign = (d.x > 0) ? 1 : -1;
i32 ySign = (d.y > 0) ? 1 : -1;
if (BoxNorm(d) <= 0.5f)
{
return V3i();
}
else if ((f32)ySign * d.y > (f32)xSign * d.x)
{
return ((ySign > 0) ? V3i(0, 1, 0) : V3i(0, -1, 0));
}
else
{
return ((xSign > 0) ? V3i(1, 0, 0) : V3i(-1, 0, 0));
}
}
static UnitInstruction GetOneStepForDir(v3i dir)
{
if(dir == V3i(0, 0, 0))
{
return Unit_Wait;
}else if(dir == V3i(1, 0, 0))
{
return Unit_MoveRight;
}else if(dir == V3i(-1, 0, 0))
{
return Unit_MoveLeft;
}else if(dir == V3i(0, 1, 0))
{
return Unit_MoveUp;
}else if(dir == V3i(0, -1, 0))
{
return Unit_MoveDown;
}
else
{
Die;
}
return Unit_Wait;
}
static v3i SnapToTileMap(v3 pos)
{
return V3i((i32)pos.x, (i32)pos.y, (i32)pos.z);
}
static v3i RoundToTileMap(v3 pos)
{
return V3i((i32)floorf(pos.x + 0.5f), (i32)floorf(pos.y + 0.5f), (i32)floorf(pos.z + 0.5f));
}
static BitData *GetBitData(EntityManager *entityManager, Entity *bit)
{
Assert(bit->type == Entity_Bit);
u32 index = (u32)(bit - entityManager->bitArray.data);
return (entityManager->bitData + index);
}