From 1b629d7ac083cf833aa83b22f81b4e32495f8100 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 8 Jun 2019 17:52:47 +0100 Subject: [PATCH] implement daylight sensor power recalculation --- src/pocketmine/block/DaylightSensor.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/pocketmine/block/DaylightSensor.php b/src/pocketmine/block/DaylightSensor.php index 2d85e898b1..d066708591 100644 --- a/src/pocketmine/block/DaylightSensor.php +++ b/src/pocketmine/block/DaylightSensor.php @@ -29,6 +29,10 @@ use pocketmine\math\AxisAlignedBB; use pocketmine\math\Facing; use pocketmine\math\Vector3; use pocketmine\Player; +use function cos; +use function max; +use function round; +use const M_PI; class DaylightSensor extends Transparent{ /** @var BlockIdentifierFlattened */ @@ -85,11 +89,26 @@ class DaylightSensor extends Transparent{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ $this->inverted = !$this->inverted; + $this->power = $this->recalculatePower(); $this->world->setBlock($this, $this); return true; } public function onScheduledUpdate() : void{ - //TODO + $this->power = $this->recalculatePower(); + $this->world->setBlock($this, $this); + $this->world->scheduleDelayedBlockUpdate($this, 20); } + + private function recalculatePower() : int{ + $lightLevel = $this->world->getRealBlockSkyLightAt($this->x, $this->y, $this->z); + if($this->inverted){ + return 15 - $lightLevel; + } + + $sunAngle = $this->world->getSunAnglePercentage(); + return max(0, (int) round(15 * cos(($sunAngle + ((($sunAngle < 0.5 ? 0 : 1) - $sunAngle) / 5)) * 2 * M_PI))); + } + + //TODO }