PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
BigDripleafHead.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
26use pocketmine\block\utils\DripleafState;
35
37
38 protected DripleafState $leafState = DripleafState::STABLE;
39
40 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
41 parent::describeBlockOnlyState($w);
42 $w->enum($this->leafState);
43 }
44
45 protected function isHead() : bool{
46 return true;
47 }
48
49 public function getLeafState() : DripleafState{
50 return $this->leafState;
51 }
52
54 public function setLeafState(DripleafState $leafState) : self{
55 $this->leafState = $leafState;
56 return $this;
57 }
58
59 public function hasEntityCollision() : bool{
60 return true;
61 }
62
63 private function setTiltAndScheduleTick(DripleafState $tilt) : void{
64 $this->position->getWorld()->setBlock($this->position, $this->setLeafState($tilt));
65 $delay = $tilt->getScheduledUpdateDelayTicks();
66 if($delay !== null){
67 $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, $delay);
68 }
69 }
70
71 private function getLeafTopOffset() : float{
72 return match($this->leafState){
73 DripleafState::STABLE, DripleafState::UNSTABLE => 1 / 16,
74 DripleafState::PARTIAL_TILT => 3 / 16,
75 default => 0
76 };
77 }
78
79 public function onEntityInside(Entity $entity) : bool{
80 if(!$entity instanceof Projectile && $this->leafState === DripleafState::STABLE){
81 //the entity must be standing on top of the leaf - do not collapse if the entity is standing underneath
82 $intersection = AxisAlignedBB::one()
83 ->offset($this->position->x, $this->position->y, $this->position->z)
84 ->trim(Facing::DOWN, 1 - $this->getLeafTopOffset());
85 if($entity->getBoundingBox()->intersectsWith($intersection)){
86 $this->setTiltAndScheduleTick(DripleafState::UNSTABLE);
87 return false;
88 }
89 }
90 return true;
91 }
92
93 public function onProjectileHit(Projectile $projectile, RayTraceResult $hitResult) : void{
94 if($this->leafState !== DripleafState::FULL_TILT){
95 $this->setTiltAndScheduleTick(DripleafState::FULL_TILT);
96 $this->position->getWorld()->addSound($this->position, new DripleafTiltDownSound());
97 }
98 }
99
100 public function onScheduledUpdate() : void{
101 if($this->leafState !== DripleafState::STABLE){
102 if($this->leafState === DripleafState::FULL_TILT){
103 $this->position->getWorld()->setBlock($this->position, $this->setLeafState(DripleafState::STABLE));
104 $this->position->getWorld()->addSound($this->position, new DripleafTiltUpSound());
105 }else{
106 $this->setTiltAndScheduleTick(match($this->leafState){
107 DripleafState::UNSTABLE => DripleafState::PARTIAL_TILT,
108 DripleafState::PARTIAL_TILT => DripleafState::FULL_TILT,
109 });
110 $this->position->getWorld()->addSound($this->position, new DripleafTiltDownSound());
111 }
112 }
113 }
114
115 protected function recalculateCollisionBoxes() : array{
116 if($this->leafState !== DripleafState::FULL_TILT){
117 return [
118 AxisAlignedBB::one()
119 ->trim(Facing::DOWN, 11 / 16)
120 ->trim(Facing::UP, $this->getLeafTopOffset())
121 ];
122 }
123 return [];
124 }
125}
onProjectileHit(Projectile $projectile, RayTraceResult $hitResult)
describeBlockOnlyState(RuntimeDataDescriber $w)
setLeafState(DripleafState $leafState)