LightArray: added fill(), remove duplicated code

This commit is contained in:
Dylan K. Taylor 2019-07-09 13:47:53 +01:00
parent 4264923f4f
commit 5a2e84eb9f
2 changed files with 12 additions and 6 deletions

View File

@ -221,10 +221,8 @@ class Chunk{
* @param int $level
*/
public function setAllBlockSkyLight(int $level) : void{
$char = chr(($level & 0x0f) | ($level << 4));
$data = str_repeat($char, 2048);
for($y = $this->getHighestSubChunkIndex(); $y >= 0; --$y){
$this->getSubChunk($y, true)->setBlockSkyLightArray(new LightArray($data));
$this->getSubChunk($y, true)->setBlockSkyLightArray(LightArray::fill($level));
}
}
@ -257,10 +255,8 @@ class Chunk{
* @param int $level
*/
public function setAllBlockLight(int $level) : void{
$char = chr(($level & 0x0f) | ($level << 4));
$data = str_repeat($char, 2048);
for($y = $this->getHighestSubChunkIndex(); $y >= 0; --$y){
$this->getSubChunk($y, true)->setBlockLightArray(new LightArray($data));
$this->getSubChunk($y, true)->setBlockLightArray(LightArray::fill($level));
}
}

View File

@ -54,6 +54,16 @@ final class LightArray{
$this->collectGarbage();
}
public static function fill(int $level) : self{
if($level === 0){
return new self(self::ZERO);
}
if($level === 15){
return new self(self::FIFTEEN);
}
return new self(str_repeat(chr(($level & 0x0f) | ($level << 4)), 2048));
}
public function get(int $x, int $y, int $z) : int{
return (ord($this->data{($x << 7) | ($z << 3) | ($y >> 1)}) >> (($y & 1) << 2)) & 0xf;
}