Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Janko Richter committed Sep 7, 2023
1 parent d9d4a33 commit 3f3b2de
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/StarsystemGenerator/Component/AsteroidRingGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -140,23 +140,35 @@ private function createRing(
}
}

/** @param array<int, PointInterface> $ringPoints */
private function insertGaps(array &$ringPoints): void
/**
* @param array<int, PointInterface> $ringPoints
*
* @return array<int, PointInterface>
* */
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);

//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
Expand Down

0 comments on commit 3f3b2de

Please # to comment.