Return proper BlockIterator index, throw more exceptions, improved blockQueue performance

This commit is contained in:
Shoghi Cervantes 2014-11-06 12:12:31 +01:00
parent 9eed0a579c
commit 987d647b76

View File

@ -38,9 +38,11 @@ class BlockIterator implements \Iterator{
private $end = false; private $end = false;
/** @var Block[] */ /** @var \SplFixedArray<Block>[3] */
private $blockQueue = [null, null, null]; private $blockQueue;
private $currentBlock = 0; private $currentBlock = 0;
/** @var Block */
private $currentBlockObject = null;
private $currentDistance = 0; private $currentDistance = 0;
private $maxDistanceInt = 0; private $maxDistanceInt = 0;
@ -57,6 +59,7 @@ class BlockIterator implements \Iterator{
public function __construct(Level $level, Vector3 $start, Vector3 $direction, $yOffset = 0, $maxDistance = 0){ public function __construct(Level $level, Vector3 $start, Vector3 $direction, $yOffset = 0, $maxDistance = 0){
$this->level = $level; $this->level = $level;
$this->maxDistance = (int) $maxDistance; $this->maxDistance = (int) $maxDistance;
$this->blockQueue = new \SplFixedArray(3);
$startClone = new Vector3($start->x, $start->y, $start->z); $startClone = new Vector3($start->x, $start->y, $start->z);
$startClone->y += $yOffset; $startClone->y += $yOffset;
@ -219,20 +222,28 @@ class BlockIterator implements \Iterator{
if($this->currentBlock <= -1){ if($this->currentBlock <= -1){
throw new \OutOfBoundsException; throw new \OutOfBoundsException;
}else{ }else{
--$this->currentBlock; $this->currentBlockObject = $this->blockQueue[$this->currentBlock--];
} }
} }
/**
* @return Block
*
* @throws \OutOfBoundsException
*/
public function current(){ public function current(){
return $this->blockQueue[$this->currentBlock]; if($this->currentBlockObject === null){
throw new \OutOfBoundsException;
}
return $this->currentBlockObject;
} }
public function rewind(){ public function rewind(){
throw new \InvalidStateException("BlockIterator doesn't support rewind()");
} }
public function key(){ public function key(){
return $this->currentBlock; return $this->currentBlock - 1;
} }
public function valid(){ public function valid(){