PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
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 in_array;
27
28final class Facing{
29 private function __construct(){
30 //NOOP
31 }
32
33 public const FLAG_AXIS_POSITIVE = 1;
34
35 /* most significant 2 bits = axis, least significant bit = is positive direction */
36 public const DOWN = Axis::Y << 1;
37 public const UP = (Axis::Y << 1) | self::FLAG_AXIS_POSITIVE;
38 public const NORTH = Axis::Z << 1;
39 public const SOUTH = (Axis::Z << 1) | self::FLAG_AXIS_POSITIVE;
40 public const WEST = Axis::X << 1;
41 public const EAST = (Axis::X << 1) | self::FLAG_AXIS_POSITIVE;
42
43 public const ALL = [
44 self::DOWN,
45 self::UP,
46 self::NORTH,
47 self::SOUTH,
48 self::WEST,
49 self::EAST
50 ];
51
52 public const HORIZONTAL = [
53 self::NORTH,
54 self::SOUTH,
55 self::WEST,
56 self::EAST
57 ];
58
59 public const OFFSET = [
60 self::DOWN => [ 0, -1, 0],
61 self::UP => [ 0, +1, 0],
62 self::NORTH => [ 0, 0, -1],
63 self::SOUTH => [ 0, 0, +1],
64 self::WEST => [-1, 0, 0],
65 self::EAST => [+1, 0, 0]
66 ];
67
68 private const CLOCKWISE = [
69 Axis::Y => [
70 self::NORTH => self::EAST,
71 self::EAST => self::SOUTH,
72 self::SOUTH => self::WEST,
73 self::WEST => self::NORTH
74 ],
75 Axis::Z => [
76 self::UP => self::EAST,
77 self::EAST => self::DOWN,
78 self::DOWN => self::WEST,
79 self::WEST => self::UP
80 ],
81 Axis::X => [
82 self::UP => self::NORTH,
83 self::NORTH => self::DOWN,
84 self::DOWN => self::SOUTH,
85 self::SOUTH => self::UP
86 ]
87 ];
88
92 public static function axis(int $direction) : int{
93 return $direction >> 1; //shift off positive/negative bit
94 }
95
99 public static function isPositive(int $direction) : bool{
100 return ($direction & self::FLAG_AXIS_POSITIVE) === self::FLAG_AXIS_POSITIVE;
101 }
102
108 public static function opposite(int $direction) : int{
109 return $direction ^ self::FLAG_AXIS_POSITIVE;
110 }
111
117 public static function rotate(int $direction, int $axis, bool $clockwise) : int{
118 if(!isset(self::CLOCKWISE[$axis])){
119 throw new \InvalidArgumentException("Invalid axis $axis");
120 }
121 if(!isset(self::CLOCKWISE[$axis][$direction])){
122 throw new \InvalidArgumentException("Cannot rotate facing \"" . self::toString($direction) . "\" around axis \"" . Axis::toString($axis) . "\"");
123 }
124
125 $rotated = self::CLOCKWISE[$axis][$direction];
126 return $clockwise ? $rotated : self::opposite($rotated);
127 }
128
132 public static function rotateY(int $direction, bool $clockwise) : int{
133 return self::rotate($direction, Axis::Y, $clockwise);
134 }
135
139 public static function rotateZ(int $direction, bool $clockwise) : int{
140 return self::rotate($direction, Axis::Z, $clockwise);
141 }
142
146 public static function rotateX(int $direction, bool $clockwise) : int{
147 return self::rotate($direction, Axis::X, $clockwise);
148 }
149
155 public static function validate(int $facing) : void{
156 if(!in_array($facing, self::ALL, true)){
157 throw new \InvalidArgumentException("Invalid direction $facing");
158 }
159 }
160
164 public static function toString(int $facing) : string{
165 return match($facing){
166 self::DOWN => "down",
167 self::UP => "up",
168 self::NORTH => "north",
169 self::SOUTH => "south",
170 self::WEST => "west",
171 self::EAST => "east",
172 default => throw new \InvalidArgumentException("Invalid facing $facing")
173 };
174 }
175}
static rotate(int $direction, int $axis, bool $clockwise)
Definition Facing.php:117
static toString(int $facing)
Definition Facing.php:164
static rotateX(int $direction, bool $clockwise)
Definition Facing.php:146
static rotateZ(int $direction, bool $clockwise)
Definition Facing.php:139
static isPositive(int $direction)
Definition Facing.php:99
static validate(int $facing)
Definition Facing.php:155
static axis(int $direction)
Definition Facing.php:92
static opposite(int $direction)
Definition Facing.php:108
static rotateY(int $direction, bool $clockwise)
Definition Facing.php:132