-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate planet stats during colonization process
- Loading branch information
Showing
8 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheGame\Application\Component\Galaxy\Domain; | ||
|
||
enum PlanetBiome: string | ||
{ | ||
case Desert = 'desert'; | ||
|
||
case Jungle = 'jungle'; | ||
|
||
case Lava = 'lava'; | ||
|
||
case Magnetic = 'magnetic'; | ||
|
||
case Watery = 'watery'; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheGame\Application\Component\Galaxy\Domain; | ||
|
||
class PlanetStats | ||
{ | ||
public function __construct( | ||
private readonly PlanetBiome $biome, | ||
private readonly int $size, | ||
private readonly int $minTemperature, | ||
private readonly int $maxTemperature, | ||
) { | ||
} | ||
|
||
public function getBiome(): PlanetBiome | ||
{ | ||
return $this->biome; | ||
} | ||
|
||
public function getSize(): int | ||
{ | ||
return $this->size; | ||
} | ||
|
||
public function getMinTemperature(): int | ||
{ | ||
return $this->minTemperature; | ||
} | ||
|
||
public function getMaxTemperature(): int | ||
{ | ||
return $this->maxTemperature; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Application/Component/Galaxy/Domain/PlanetStatsRandomizer.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheGame\Application\Component\Galaxy\Domain; | ||
|
||
final class PlanetStatsRandomizer implements PlanetStatsRandomizerInterface | ||
{ | ||
public function randomize(int $planetPosition, int $maxPosition): PlanetStats | ||
{ | ||
$biomes = PlanetBiome::cases(); | ||
$biome = $biomes[rand(0, count($biomes)-1)]; | ||
$fieldsNumber = $this->randomizeFieldsNumber($planetPosition, $maxPosition); | ||
[$minTemperature, $maxTemperature] = $this->randomizeTemperature($planetPosition, $maxPosition); | ||
|
||
return new PlanetStats( | ||
$biome, $fieldsNumber, $minTemperature, $maxTemperature | ||
); | ||
} | ||
|
||
private function randomizeFieldsNumber(int $planetPosition, int $maxPosition): int | ||
{ | ||
$pivot = $planetPosition / $maxPosition; | ||
$statsMetadata = [ | ||
[0.15, 45, 95], | ||
[0.3, 45, 120], | ||
[0.4, 120, 165], | ||
[0.6, 120, 185], | ||
[0.8, 150, 185], | ||
[0.9, 185, 265], | ||
[1.0, 235, 265], | ||
]; | ||
|
||
$minFieldsNumber = $maxFieldsNumber = 0; | ||
foreach ($statsMetadata as $metadata) { | ||
$pivotBound = $metadata[0]; | ||
$minFieldsNumberBound = $metadata[1]; | ||
$maxFieldsNumberBound = $metadata[2]; | ||
|
||
if ($pivot < $pivotBound) { | ||
$minFieldsNumber = $minFieldsNumberBound; | ||
$maxFieldsNumber = $maxFieldsNumberBound; | ||
break; | ||
} | ||
} | ||
|
||
return rand($minFieldsNumber, $maxFieldsNumber); | ||
} | ||
|
||
/** @return int[] */ | ||
private function randomizeTemperature(int $planetPosition, int $maxPosition): array | ||
{ | ||
$minPivot = ($planetPosition / $maxPosition) * 100; | ||
$maxPivot = 100; | ||
|
||
$lowerBounds = [-45, 145]; | ||
$higherBounds = [-5, 275]; | ||
|
||
$randPivot = rand($minPivot, $maxPivot) / 100; | ||
$lowerTemperature = $lowerBounds[0] + $randPivot * $lowerBounds[1]-$lowerBounds[0]; | ||
|
||
$randPivot = rand($minPivot, $maxPivot) / 100; | ||
$higherTemperature = $higherBounds[0] + $randPivot * $higherBounds[1]-$higherBounds[0]; | ||
|
||
if ($lowerTemperature > $higherTemperature) { | ||
return [$higherTemperature, $lowerTemperature]; | ||
} | ||
|
||
return [$lowerTemperature, $higherTemperature]; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Application/Component/Galaxy/Domain/PlanetStatsRandomizerInterface.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheGame\Application\Component\Galaxy\Domain; | ||
|
||
interface PlanetStatsRandomizerInterface | ||
{ | ||
public function randomize(int $planetPosition, int $maxPosition): PlanetStats; | ||
} |
This file contains 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