Created
October 20, 2025 20:54
-
-
Save Rseding91/4b417bd22f18b77d738121f5ca87dcb6 to your computer and use it in GitHub Desktop.
2.0 Asteroid chunk update loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void SpacePlatform::updateAsteroidChunks() | |
| { | |
| if (this->asteroidChunks.empty()) | |
| return; | |
| const SimpleBoundingBox asteroidSpawningArea = this->getAsteroidSpawningArea(); | |
| const double left = asteroidSpawningArea.leftTop.x.getDouble(); | |
| const double top = asteroidSpawningArea.leftTop.y.getDouble(); | |
| const double right = asteroidSpawningArea.rightBottom.x.getDouble(); | |
| const double bottom = asteroidSpawningArea.rightBottom.y.getDouble(); | |
| auto collidesWithSpawningArea = [=, zero = left == right && top == bottom](const Vector position) -> bool | |
| { | |
| if (zero) | |
| return false; | |
| if (left > position.x) | |
| return false; | |
| if (top > position.y) | |
| return false; | |
| if (right < position.x) | |
| return false; | |
| if (bottom < position.y) | |
| return false; | |
| return true; | |
| }; | |
| auto shouldAsteroidDespawn = [=, center = asteroidSpawningArea.getCenter().toVector()](const AsteroidChunk& asteroidChunk) -> bool | |
| { | |
| const Vector position = asteroidChunk.getPositionVector(); | |
| const Vector movement = asteroidChunk.getVelocity(); | |
| return !collidesWithSpawningArea(position) && | |
| (center - position).dot(movement) <= 0; // Going away from the platform. | |
| }; | |
| size_t size = this->asteroidChunks.size(); | |
| for (size_t i = 0; i < size;) | |
| { | |
| if (this->asteroidChunks[i].update(*this->surface) || | |
| shouldAsteroidDespawn(this->asteroidChunks[i])) | |
| { | |
| this->asteroidChunks[i].clearTargetingMe(); | |
| if (i + 1 != size) | |
| { | |
| this->asteroidChunks[i] = std::move(this->asteroidChunks.back()); | |
| if (i < this->nextAsteroidChunkUpdateIndex) | |
| this->updateAsteroidChunkTracking(this->asteroidChunks[i]); | |
| } | |
| this->asteroidChunks.pop_back(); | |
| --size; | |
| } | |
| ++i; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment