Skip to content

Instantly share code, notes, and snippets.

@Rseding91
Created March 19, 2025 09:28
Show Gist options
  • Save Rseding91/aecc426adc32039844f6d43bf3f3c63f to your computer and use it in GitHub Desktop.
Save Rseding91/aecc426adc32039844f6d43bf3f3c63f to your computer and use it in GitHub Desktop.
How asteroids spawn
void SpacePlatform::spawnAsteroids()
{
const bool randomOrientation = this->speed < 0.2;
if (SpaceLocationID locationID = this->position.getCurrentLocation())
for (auto& spawnDefinition : locationID->asteroidSpawnDefinitions)
this->trySpawnAsteroid(spawnDefinition.asteroid, spawnDefinition.chunk, spawnDefinition, randomOrientation);
else
{
const PointOnSpaceConnection& pointOnConnection = this->position.getPointOnConnection();
for (auto& spawnDefinition : pointOnConnection.connection->asteroidSpawnDefinitions)
this->trySpawnAsteroid(spawnDefinition.asteroid, spawnDefinition.asteroidChunk, spawnDefinition.interpolate(pointOnConnection.distance), randomOrientation);
// Spawn SpaceLocation asteroids on space connections if the platform is in the location's sphere of influence.
this->spawnAsteroidsOnConnectionNearEndPoint(**pointOnConnection.connection->from, pointOnConnection.distance);
this->spawnAsteroidsOnConnectionNearEndPoint(**pointOnConnection.connection->to, 1 - pointOnConnection.distance);
}
}
void SpacePlatform::spawnAsteroidsOnConnectionNearEndPoint(const SpaceLocationPrototype& endPoint, double distanceFromEndPoint)
{
if (distanceFromEndPoint < endPoint.asteroidSpawnInfluence)
for (const SpaceLocationAsteroidSpawnDefinition& spawnDefinition : endPoint.asteroidSpawnDefinitions)
this->trySpawnAsteroid(spawnDefinition.asteroid,
spawnDefinition.chunk,
Math::lerp<AsteroidSpawnPoint>(spawnDefinition,
AsteroidSpawnPoint(0, 0, spawnDefinition.angleWhenStopped),
Math::inverseLerp(0.0, endPoint.asteroidSpawnInfluence, distanceFromEndPoint)),
!this->isMoving());
}
void SpacePlatform::trySpawnAsteroid(EntityID entityID, AsteroidChunkID asteroidChunkID, const AsteroidSpawnPoint& data, bool randomOrientation)
{
static constexpr uint32_t BASE_SPAWNING_AREA = 1024;
double probability = this->getAsteroidSpawningArea().getWidth().getDouble() + this->getAsteroidSpawningArea().getHeight().getDouble();
if (!randomOrientation)
probability += this->getAsteroidSpawningArea().getWidth().getDouble() * this->speed * 10;
else if (!this->position.getCurrentLocation())
probability *= 0.1; // Reduce asteroid spawning for platforms which stopped mid-route.
probability *= *this->map.mapSettings->asteroidSettings.spawningRate / BASE_SPAWNING_AREA * data.probability;
randomOrientation |= this->speed < UtilityConstants::instance().asteroidSpawningWithRandomOrientationMaxSpeed;
for (; probability > 1; --probability)
this->spawnAsteroid(entityID, asteroidChunkID, data, randomOrientation);
if (this->map.getRandomGenerator().testChance(probability))
this->spawnAsteroid(entityID, asteroidChunkID, data, randomOrientation);
}
SimpleBoundingBox SpacePlatform::getAsteroidSpawningArea()
{
const SimpleBoundingBox& asteroidSpawningOffset = UtilityConstants::instance().asteroidSpawningOffset;
return SimpleBoundingBox(this->platformBoundingBox.leftTop.x + asteroidSpawningOffset.leftTop.x,
this->platformBoundingBox.leftTop.y + asteroidSpawningOffset.leftTop.y,
this->platformBoundingBox.rightBottom.x + asteroidSpawningOffset.rightBottom.x,
this->platformBoundingBox.rightBottom.y + asteroidSpawningOffset.rightBottom.y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment