Skip to content

Commit

Permalink
ECS: Fix remove entity function
Browse files Browse the repository at this point in the history
PATCH
  • Loading branch information
guillaumeAbel committed Oct 3, 2023
1 parent b8d5e2a commit 7382c09
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ECS/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enum BackLayers { BACK = 0, BACKMAX };
/*
* FRONT is the frontest layer, so when adding a new one increment the FRONT
* value and add the new one above
*/
*/

enum FrontLayers { FRONT = 0, FRONTMAX };

Expand Down
30 changes: 23 additions & 7 deletions src/ECS/SparseArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,9 @@ class SparseArray {
throw std::runtime_error(
"SparseArrays::erase: ID out of bounds!");
}
if (_sparse[id] != -1) {
auto it = _dense.begin();
std::advance(it, _sparse[id]);
_dense.erase(it);
auto revIt = _revSparse.begin();
std::advance(revIt, _sparse[id]);
_revSparse.erase(revIt);
std::size_t sparseValue = _sparse[id];
if (sparseValue != -1) {
removeDenses(id, sparseValue);
}
auto it = _sparse.begin();
std::advance(it, id);
Expand Down Expand Up @@ -101,6 +97,26 @@ class SparseArray {
}

private:
void removeDenses(std::size_t id, std::size_t sparseValue)
{
auto it = _dense.begin();
std::advance(it, sparseValue);
_dense.erase(it);
auto revIt = _revSparse.begin();
std::advance(revIt, sparseValue);
_revSparse.erase(revIt);
for (auto revIt2 = _revSparse.begin(); revIt2 != _revSparse.end(); revIt2++) {
if (*revIt2 > id) {
(*revIt2)--;
}
}
for (auto it2 = _sparse.begin(); it2 != _sparse.end(); it2++) {
if (*it2 > sparseValue) {
(*it2)--;
}
}
}

void throwIfDontExist(std::size_t id)
{
if (!exist(id)) {
Expand Down

0 comments on commit 7382c09

Please sign in to comment.