PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
Fence.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\SupportType;
30use function count;
31
32class Fence extends Transparent{
34 protected array $connections = [];
35
36 public function getThickness() : float{
37 return 0.25;
38 }
39
40 public function readStateFromWorld() : Block{
41 parent::readStateFromWorld();
42
43 $this->collisionBoxes = null;
44
45 foreach(Facing::HORIZONTAL as $facing){
46 $block = $this->getSide($facing);
47 if($block instanceof static || $block instanceof FenceGate || $block->getSupportType(Facing::opposite($facing)) === SupportType::FULL){
48 $this->connections[$facing] = true;
49 }else{
50 unset($this->connections[$facing]);
51 }
52 }
53
54 return $this;
55 }
56
60 protected function recalculateCollisionBoxes() : array{
61 $inset = 0.5 - $this->getThickness() / 2;
62
64 $bbs = [];
65
66 $connectWest = isset($this->connections[Facing::WEST]);
67 $connectEast = isset($this->connections[Facing::EAST]);
68
69 if($connectWest || $connectEast){
70 //X axis (west/east)
71 $bbs[] = AxisAlignedBB::one()
72 ->squash(Axis::Z, $inset)
73 ->extend(Facing::UP, 0.5)
74 ->trim(Facing::WEST, $connectWest ? 0 : $inset)
75 ->trim(Facing::EAST, $connectEast ? 0 : $inset);
76 }
77
78 $connectNorth = isset($this->connections[Facing::NORTH]);
79 $connectSouth = isset($this->connections[Facing::SOUTH]);
80
81 if($connectNorth || $connectSouth){
82 //Z axis (north/south)
83 $bbs[] = AxisAlignedBB::one()
84 ->squash(Axis::X, $inset)
85 ->extend(Facing::UP, 0.5)
86 ->trim(Facing::NORTH, $connectNorth ? 0 : $inset)
87 ->trim(Facing::SOUTH, $connectSouth ? 0 : $inset);
88 }
89
90 if(count($bbs) === 0){
91 //centre post AABB (only needed if not connected on any axis - other BBs overlapping will do this if any connections are made)
92 return [
93 AxisAlignedBB::one()
94 ->extend(Facing::UP, 0.5)
95 ->contract($inset, 0, $inset)
96 ];
97 }
98
99 return $bbs;
100 }
101
102 public function getSupportType(int $facing) : SupportType{
103 return Facing::axis($facing) === Axis::Y ? SupportType::CENTER : SupportType::NONE;
104 }
105}
getSide(int $side, int $step=1)
Definition Block.php:770
getSupportType(int $facing)
Definition FenceGate.php:74
getSupportType(int $facing)
Definition Fence.php:102