PocketMine-MP 5.33.2 git-919492bdcad8510eb6606439eb77e1c604f1d1ea
Loading...
Searching...
No Matches
Facing.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\math;
25
26use function strtolower;
27
28enum Facing : int{
29 public const FLAG_AXIS_POSITIVE = 1;
30
31 /* most significant 2 bits = axis, least significant bit = is positive direction */
32 case DOWN = Axis::Y->value << 1;
33 case UP = (Axis::Y->value << 1) | self::FLAG_AXIS_POSITIVE;
34 case NORTH = Axis::Z->value << 1;
35 case SOUTH = (Axis::Z->value << 1) | self::FLAG_AXIS_POSITIVE;
36 case WEST = Axis::X->value << 1;
37 case EAST = (Axis::X->value << 1) | self::FLAG_AXIS_POSITIVE;
38
39 public const ALL = [
40 self::DOWN,
41 self::UP,
42 self::NORTH,
43 self::SOUTH,
44 self::WEST,
45 self::EAST
46 ];
47
48 public const HORIZONTAL = [
49 self::NORTH,
50 self::SOUTH,
51 self::WEST,
52 self::EAST
53 ];
54
55 public const OFFSET = [
56 self::DOWN->value => [ 0, -1, 0],
57 self::UP->value => [ 0, +1, 0],
58 self::NORTH->value => [ 0, 0, -1],
59 self::SOUTH->value => [ 0, 0, +1],
60 self::WEST->value => [-1, 0, 0],
61 self::EAST->value => [+1, 0, 0]
62 ];
63
64 private const CLOCKWISE = [
65 Axis::Y->value => [
66 self::NORTH->value => self::EAST,
67 self::EAST->value => self::SOUTH,
68 self::SOUTH->value => self::WEST,
69 self::WEST->value => self::NORTH
70 ],
71 Axis::Z->value => [
72 self::UP->value => self::EAST,
73 self::EAST->value => self::DOWN,
74 self::DOWN->value => self::WEST,
75 self::WEST->value => self::UP
76 ],
77 Axis::X->value => [
78 self::UP->value => self::NORTH,
79 self::NORTH->value => self::DOWN,
80 self::DOWN->value => self::SOUTH,
81 self::SOUTH->value => self::UP
82 ]
83 ];
84
88 public static function axis(Facing $direction) : Axis{
89 return Axis::from($direction->value >> 1); //shift off positive/negative bit
90 }
91
95 public static function isPositive(Facing $direction) : bool{
96 return ($direction->value & self::FLAG_AXIS_POSITIVE) === self::FLAG_AXIS_POSITIVE;
97 }
98
102 public static function opposite(Facing $direction) : Facing{
103 return self::from($direction->value ^ self::FLAG_AXIS_POSITIVE);
104 }
105
111 public static function rotate(Facing $direction, Axis $axis, bool $clockwise) : Facing{
112 if(!isset(self::CLOCKWISE[$axis->value][$direction->value])){
113 throw new \InvalidArgumentException("Cannot rotate facing \"" . self::toString($direction) . "\" around axis \"" . Axis::toString($axis) . "\"");
114 }
115
116 $rotated = self::CLOCKWISE[$axis->value][$direction->value];
117 return $clockwise ? $rotated : self::opposite($rotated);
118 }
119
123 public static function rotateY(Facing $direction, bool $clockwise) : Facing{
124 return self::rotate($direction, Axis::Y, $clockwise);
125 }
126
130 public static function rotateZ(Facing $direction, bool $clockwise) : Facing{
131 return self::rotate($direction, Axis::Z, $clockwise);
132 }
133
137 public static function rotateX(Facing $direction, bool $clockwise) : Facing{
138 return self::rotate($direction, Axis::X, $clockwise);
139 }
140
144 public static function toString(Facing $facing) : string{
145 return strtolower($facing->name);
146 }