PocketMine-MP 5.32.2 git-237b304ef9858756b018e44e8f298093f66f823b
Loading...
Searching...
No Matches
DaylightSensor.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block;
25
27use pocketmine\block\utils\AnalogRedstoneSignalEmitterTrait;
28use pocketmine\block\utils\SupportType;
35use function cos;
36use function max;
37use function round;
38use const M_PI;
39
41 use AnalogRedstoneSignalEmitterTrait;
42
43 protected bool $inverted = false;
44
45 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
46 $w->boundedIntAuto(0, 15, $this->signalStrength);
47 $w->bool($this->inverted);
48 }
49
50 public function isInverted() : bool{
51 return $this->inverted;
52 }
53
57 public function setInverted(bool $inverted = true) : self{
58 $this->inverted = $inverted;
59 return $this;
60 }
61
62 public function getFuelTime() : int{
63 return 300;
64 }
65
66 protected function recalculateCollisionBoxes() : array{
67 return [AxisAlignedBB::one()->trim(Facing::UP, 10 / 16)];
68 }
69
70 public function getSupportType(int $facing) : SupportType{
71 return SupportType::NONE;
72 }
73
74 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
75 $this->inverted = !$this->inverted;
76 $this->signalStrength = $this->recalculateSignalStrength();
77 $this->position->getWorld()->setBlock($this->position, $this);
78 return true;
79 }
80
81 public function onScheduledUpdate() : void{
82 $world = $this->position->getWorld();
83 $signalStrength = $this->recalculateSignalStrength();
84 if($this->signalStrength !== $signalStrength){
85 $this->signalStrength = $signalStrength;
86 $world->setBlock($this->position, $this);
87 }
88 $world->scheduleDelayedBlockUpdate($this->position, 20);
89 }
90
91 private function recalculateSignalStrength() : int{
92 $world = $this->position->getWorld();
93 $lightLevel = $world->getRealBlockSkyLightAt($this->position->x, $this->position->y, $this->position->z);
94 if($this->inverted){
95 return 15 - $lightLevel;
96 }
97
98 $sunAngle = $world->getSunAnglePercentage();
99 return max(0, (int) round($lightLevel * cos(($sunAngle + ((($sunAngle < 0.5 ? 0 : 1) - $sunAngle) / 5)) * 2 * M_PI)));
100 }
101
102 //TODO
103}
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
describeBlockOnlyState(RuntimeDataDescriber $w)
setInverted(bool $inverted=true)