Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/engine/entity/include/halley/entity/family.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ namespace Halley {
// Move all entities to be removed to the back of the vector
{
int n = int(entities.size());
std::vector<std::pair<int, int>> moves;
moves.reserve( removeCount );
// Note: it's important to scan it forward. Scanning backwards would improve performance for short-lived entities,
// but it causes an issue where an entity is removed and added to the same family in one frame.
for (int i = 0; i < n; i++) {
Expand All @@ -177,7 +179,7 @@ namespace Halley {
if (iter != toRemove.end() && id == *iter) {
toRemove.erase(iter);
if (i != n - 1) {
std::swap(entities[i], entities[n - 1]);
moves.emplace_back( std::make_pair( i, n - 1 ) );
i--;
}
n--;
Expand All @@ -186,6 +188,9 @@ namespace Halley {
}
}
}
for (const std::pair<int, int>& move : moves) {
std::swap(entities[move.first], entities[move.second]);
}
Ensures(size_t(n) + removeCount == entities.size());
}

Expand Down