diff --git a/src/StarsystemGenerator/Component/AsteroidRingGenerator.php b/src/StarsystemGenerator/Component/AsteroidRingGenerator.php index f835630..106eeea 100644 --- a/src/StarsystemGenerator/Component/AsteroidRingGenerator.php +++ b/src/StarsystemGenerator/Component/AsteroidRingGenerator.php @@ -99,13 +99,13 @@ private function createRing( $this->stuRandom->rand(0, $varianceMaximum) ); - $this->insertGaps($possibleLocations); + $ringWithGaps = $this->insertGaps($possibleLocations); $asteroidType = $this->getAsteroidType($radiusPercentage); $asteroidRingPoints = []; - foreach ($possibleLocations as $point) { + foreach ($ringWithGaps as $point) { if ($maxAsteroidsToSpawn === 0) { break; } @@ -140,13 +140,19 @@ private function createRing( } } - /** @param array $ringPoints */ - private function insertGaps(array &$ringPoints): void + /** + * @param array $ringPoints + * + * @return array + * */ + private function insertGaps(array $ringPoints): array { $gapCount = count($ringPoints) / self::RING_POINTS_PER_GAP; //echo sprintf('gapCount: %d', $gapCount); + $gapAngles = []; + for ($i = 0; $i < $gapCount; $i++) { $gapAngle = $this->stuRandom->rand(1, self::MAXIMUM_GAP_ANGLE, true); $gapStartAngle = $this->stuRandom->rand(0, 360); @@ -154,9 +160,15 @@ private function insertGaps(array &$ringPoints): void //echo sprintf('gapAngle: %d+%d', $gapStartAngle, $gapAngle); for ($angle = $gapStartAngle; $angle <= $gapStartAngle + $gapAngle; $angle++) { - unset($ringPoints[$angle]); + $gapAngles[] = $angle; } } + + return array_filter( + $ringPoints, + fn (int $angle) => !in_array($angle, $gapAngles), + ARRAY_FILTER_USE_KEY + ); } private function getAsteroidType(int $radiusPercentage): int