PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
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
57 protected function recalculateCollisionBoxes() : array{
58 $inset = 0.5 - $this->getThickness() / 2;
59
60 $bbs = [];
61
62 $connectWest = isset($this->connections[Facing::WEST]);
63 $connectEast = isset($this->connections[Facing::EAST]);
64
65 if($connectWest || $connectEast){
66 //X axis (west/east)
67 $bbs[] = AxisAlignedBB::one()
68 ->squash(Axis::Z, $inset)
69 ->extend(Facing::UP, 0.5)
70 ->trim(Facing::WEST, $connectWest ? 0 : $inset)
71 ->trim(Facing::EAST, $connectEast ? 0 : $inset);
72 }
73
74 $connectNorth = isset($this->connections[Facing::NORTH]);
75 $connectSouth = isset($this->connections[Facing::SOUTH]);
76
77 if($connectNorth || $connectSouth){
78 //Z axis (north/south)
79 $bbs[] = AxisAlignedBB::one()
80 ->squash(Axis::X, $inset)
81 ->extend(Facing::UP, 0.5)
82 ->trim(Facing::NORTH, $connectNorth ? 0 : $inset)
83 ->trim(Facing::SOUTH, $connectSouth ? 0 : $inset);
84 }
85
86 if(count($bbs) === 0){
87 //centre post AABB (only needed if not connected on any axis - other BBs overlapping will do this if any connections are made)
88 return [
89 AxisAlignedBB::one()
90 ->extend(Facing::UP, 0.5)
91 ->contract($inset, 0, $inset)
92 ];
93 }
94
95 return $bbs;
96 }
97
98 public function getSupportType(int $facing) : SupportType{
99 return Facing::axis($facing) === Axis::Y ? SupportType::CENTER : SupportType::NONE;
100 }
101}
getSide(int $side, int $step=1)
Definition Block.php:773
getSupportType(int $facing)
Definition FenceGate.php:71
getSupportType(int $facing)
Definition Fence.php:98