diff --git a/src/Noise.php b/src/Noise.php index bf6772d..c391314 100644 --- a/src/Noise.php +++ b/src/Noise.php @@ -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))); + } } \ No newline at end of file diff --git a/src/PerlinNoise.php b/src/PerlinNoise.php index df9143d..1e3fd06 100644 --- a/src/PerlinNoise.php +++ b/src/PerlinNoise.php @@ -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) { diff --git a/src/dto/Color.php b/src/dto/Color.php index d2e6bab..6fb721f 100644 --- a/src/dto/Color.php +++ b/src/dto/Color.php @@ -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