Add some convenience fucntions

This commit is contained in:
Tim 2023-06-05 02:23:40 +02:00
parent 5e522ea417
commit cccd74f1a5
3 changed files with 22 additions and 12 deletions

View File

@ -15,18 +15,16 @@ class Noise
$this->noise = new PerlinNoise($seed);
}
public function generate(int $x, int $y): Color
private function getTransitions(): array
{
$deepwater = new Color(0, 110, 180);
$deepwater = new Color('#006eb4');
$water = new Color(0, 156, 244);
$sand = new Color(238, 226, 104);
$grass = new Color(25, 171, 64);
$mountain = new Color(96, 93, 100);
$snow = new Color(255, 255, 255);
$rand = $this->noise->random2D($x, $y, 150);
$transitions = [
return [
[-1.0, $deepwater],
[-0.9, $water],
[0.0, $sand],
@ -34,13 +32,24 @@ class Noise
[0.997, $mountain],
[1.0, $snow],
];
}
public function generate(int $x, int $y): Color
{
$rand = $this->noise->random2D($x, $y, 150);
$transitions = $this->getTransitions();
for ($i = 0; $i < count($transitions) - 1; $i++) {
$transition = $transitions[$i];
$nextTransition = $transitions[$i + 1];
if ($rand >= $transition[0] && $rand < $nextTransition[0]) {
return $transition[1]->lerp($nextTransition[1], ($rand - $transition[0]) * (1.0 / ($nextTransition[0] - $transition[0])));
return $this->lerpTerrain($transition[1], $nextTransition[1], $transition[0], $nextTransition[0], $rand);
}
}
}
public function lerpTerrain(Color $color1, Color $color2, float $begin, float $end, float $value): Color
{
return $color1->lerp($color2, ($value - $begin) * (1.0 / ($end - $begin)));
}
}

View File

@ -148,7 +148,6 @@ class PerlinNoise
return $value;
}
//Same as random1D() only for 2 dimensions.
function random2D(float $x, float $y, ?int $size = NULL): float
{
if ($size === NULL) {

View File

@ -4,14 +4,16 @@ namespace Ardent\Game\dto;
class Color
{
public function __construct(
public int $red,
public int $green,
public int $blue,
public int $alpha = 255
private int|string $red,
private int $green = 0,
private int $blue = 0,
private readonly int $alpha = 255
)
{
if (is_string($red)) {
list($this->red, $this->green, $this->blue) = sscanf($this->red, "#%02x%02x%02x");
}
}
public function getCode(): string