diff --git a/becs2.h b/becs2.h index 4a81425..cf142b4 100644 --- a/becs2.h +++ b/becs2.h @@ -3,6 +3,7 @@ #include #include +#include #define MAX_ENTITIES 100 #define NUM_COMPONENTS 2 @@ -142,9 +143,19 @@ int32_t BECS_RemoveComponent(BECS_ECS *ecs, uint32_t entityID, uint32_t componen return 0; } - uint32_t lastEntityAdded = componentArray->sparseArray[componentArray->lastEntityAdded]; + uint32_t lastEntityAddedIndex = componentArray->sparseArray[componentArray->lastEntityAdded]; uint32_t deletedCompIndex = componentArray->sparseArray[entityID]; + uintptr_t denseArray = (uintptr_t)componentArray->denseArray; + + uintptr_t lastEntityAdded = denseArray + (lastEntityAddedIndex * componentArray->unitSize); + uintptr_t deletedComp = denseArray + (lastEntityAddedIndex * componentArray->unitSize); + + memcpy((void *)deletedComp, (void*)lastEntityAdded, componentArray->unitSize); + + componentArray->denseArraylength--; + componentArray->sparseArray[entityID] = lastEntityAddedIndex; + return 0; }