block: added constants for various integer property bounds

This commit is contained in:
Dylan K. Taylor
2022-01-28 21:27:30 +00:00
parent 466b018319
commit 0642364a44
20 changed files with 107 additions and 79 deletions

View File

@ -41,6 +41,7 @@ use function min;
use function mt_rand;
class Fire extends Flowable{
public const MAX_AGE = 15;
protected int $age = 0;
@ -49,7 +50,7 @@ class Fire extends Flowable{
}
public function readStateFromData(int $id, int $stateMeta) : void{
$this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, 15);
$this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, self::MAX_AGE);
}
public function getStateBitmask() : int{
@ -60,8 +61,8 @@ class Fire extends Flowable{
/** @return $this */
public function setAge(int $age) : self{
if($age < 0 || $age > 15){
throw new \InvalidArgumentException("Age must be in range 0-15");
if($age < 0 || $age > self::MAX_AGE){
throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE);
}
$this->age = $age;
return $this;
@ -114,7 +115,7 @@ class Fire extends Flowable{
$down = $this->getSide(Facing::DOWN);
$result = null;
if($this->age < 15 && mt_rand(0, 2) === 0){
if($this->age < self::MAX_AGE && mt_rand(0, 2) === 0){
$this->age++;
$result = $this;
}
@ -122,7 +123,7 @@ class Fire extends Flowable{
if(!$down->burnsForever()){
//TODO: check rain
if($this->age === 15){
if($this->age === self::MAX_AGE){
if(!$down->isFlammable() && mt_rand(0, 3) === 3){ //1/4 chance to extinguish
$canSpread = false;
$result = VanillaBlocks::AIR();
@ -183,7 +184,7 @@ class Fire extends Flowable{
$spreadedFire = false;
if(mt_rand(0, $this->age + 9) < 5){ //TODO: check rain
$fire = clone $this;
$fire->age = min(15, $fire->age + (mt_rand(0, 4) >> 2));
$fire->age = min(self::MAX_AGE, $fire->age + (mt_rand(0, 4) >> 2));
$spreadedFire = $this->spreadBlock($block, $fire);
}
if(!$spreadedFire){
@ -243,7 +244,7 @@ class Fire extends Flowable{
if($maxChance > 0 && mt_rand(0, $randomBound - 1) <= $maxChance){
$new = clone $this;
$new->age = min(15, $this->age + (mt_rand(0, 4) >> 2));
$new->age = min(self::MAX_AGE, $this->age + (mt_rand(0, 4) >> 2));
$this->spreadBlock($block, $new);
}
}