extracted a HeightArray type from Chunk

This commit is contained in:
Dylan K. Taylor
2020-05-16 18:41:27 +01:00
parent a31240f60b
commit 88715c7055
3 changed files with 84 additions and 24 deletions

View File

@ -81,10 +81,7 @@ class Chunk{
/** @var Entity[] */
protected $entities = [];
/**
* @var \SplFixedArray|int[]
* @phpstan-var \SplFixedArray<int>
*/
/** @var HeightArray */
protected $heightMap;
/** @var BiomeArray */
@ -100,9 +97,8 @@ class Chunk{
* @param SubChunk[] $subChunks
* @param CompoundTag[] $entities
* @param CompoundTag[] $tiles
* @param int[] $heightMap
*/
public function __construct(int $chunkX, int $chunkZ, array $subChunks = [], ?array $entities = null, ?array $tiles = null, ?BiomeArray $biomeIds = null, array $heightMap = []){
public function __construct(int $chunkX, int $chunkZ, array $subChunks = [], ?array $entities = null, ?array $tiles = null, ?BiomeArray $biomeIds = null, ?HeightArray $heightMap = null){
$this->x = $chunkX;
$this->z = $chunkZ;
@ -112,14 +108,8 @@ class Chunk{
$this->subChunks[$y] = $subChunks[$y] ?? new SubChunk(BlockLegacyIds::AIR << 4, []);
}
if(count($heightMap) === 256){
$this->heightMap = \SplFixedArray::fromArray($heightMap);
}else{
assert(count($heightMap) === 0, "Wrong HeightMap value count, expected 256, got " . count($heightMap));
$val = ($this->subChunks->getSize() * 16);
$this->heightMap = \SplFixedArray::fromArray(array_fill(0, 256, $val));
}
$val = ($this->subChunks->getSize() * 16);
$this->heightMap = $heightMap ?? new HeightArray(array_fill(0, 256, $val));
$this->biomeIds = $biomeIds ?? new BiomeArray(str_repeat("\x00", 256));
$this->NBTtiles = $tiles;
@ -258,7 +248,7 @@ class Chunk{
* @param int $z 0-15
*/
public function getHeightMap(int $x, int $z) : int{
return $this->heightMap[($z << 4) | $x];
return $this->heightMap->get($x, $z);
}
/**
@ -268,7 +258,7 @@ class Chunk{
* @param int $z 0-15
*/
public function setHeightMap(int $x, int $z, int $value) : void{
$this->heightMap[($z << 4) | $x] = $value;
$this->heightMap->set($x, $z, $value);
}
/**
@ -552,18 +542,14 @@ class Chunk{
* @return int[]
*/
public function getHeightMapArray() : array{
return $this->heightMap->toArray();
return $this->heightMap->getValues();
}
/**
* @param int[] $values
* @throws \InvalidArgumentException
*/
public function setHeightMapArray(array $values) : void{
if(count($values) !== 256){
throw new \InvalidArgumentException("Expected exactly 256 values");
}
$this->heightMap = \SplFixedArray::fromArray($values);
$this->heightMap = new HeightArray($values);
}
public function isDirty() : bool{