Add some convenience fucntions
This commit is contained in:
parent
5e522ea417
commit
cccd74f1a5
@ -15,18 +15,16 @@ class Noise
|
|||||||
$this->noise = new PerlinNoise($seed);
|
$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);
|
$water = new Color(0, 156, 244);
|
||||||
$sand = new Color(238, 226, 104);
|
$sand = new Color(238, 226, 104);
|
||||||
$grass = new Color(25, 171, 64);
|
$grass = new Color(25, 171, 64);
|
||||||
$mountain = new Color(96, 93, 100);
|
$mountain = new Color(96, 93, 100);
|
||||||
$snow = new Color(255, 255, 255);
|
$snow = new Color(255, 255, 255);
|
||||||
|
|
||||||
$rand = $this->noise->random2D($x, $y, 150);
|
return [
|
||||||
|
|
||||||
$transitions = [
|
|
||||||
[-1.0, $deepwater],
|
[-1.0, $deepwater],
|
||||||
[-0.9, $water],
|
[-0.9, $water],
|
||||||
[0.0, $sand],
|
[0.0, $sand],
|
||||||
@ -34,13 +32,24 @@ class Noise
|
|||||||
[0.997, $mountain],
|
[0.997, $mountain],
|
||||||
[1.0, $snow],
|
[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++) {
|
for ($i = 0; $i < count($transitions) - 1; $i++) {
|
||||||
$transition = $transitions[$i];
|
$transition = $transitions[$i];
|
||||||
$nextTransition = $transitions[$i + 1];
|
$nextTransition = $transitions[$i + 1];
|
||||||
if ($rand >= $transition[0] && $rand < $nextTransition[0]) {
|
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)));
|
||||||
|
}
|
||||||
}
|
}
|
@ -148,7 +148,6 @@ class PerlinNoise
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Same as random1D() only for 2 dimensions.
|
|
||||||
function random2D(float $x, float $y, ?int $size = NULL): float
|
function random2D(float $x, float $y, ?int $size = NULL): float
|
||||||
{
|
{
|
||||||
if ($size === NULL) {
|
if ($size === NULL) {
|
||||||
|
@ -4,14 +4,16 @@ namespace Ardent\Game\dto;
|
|||||||
|
|
||||||
class Color
|
class Color
|
||||||
{
|
{
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public int $red,
|
private int|string $red,
|
||||||
public int $green,
|
private int $green = 0,
|
||||||
public int $blue,
|
private int $blue = 0,
|
||||||
public int $alpha = 255
|
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
|
public function getCode(): string
|
||||||
|
Loading…
Reference in New Issue
Block a user