github.com/df-mc/dragonfly@v0.9.13/server/block/cube/direction.go (about)

     1  package cube
     2  
     3  // Direction represents a direction towards one of the horizontal axes of the world.
     4  type Direction int
     5  
     6  const (
     7  	// North represents the north direction, towards the negative Z.
     8  	North Direction = iota
     9  	// South represents the south direction, towards the positive Z.
    10  	South
    11  	// West represents the west direction, towards the negative X.
    12  	West
    13  	// East represents the east direction, towards the positive X.
    14  	East
    15  )
    16  
    17  // Face converts the direction to a Face and returns it.
    18  func (d Direction) Face() Face {
    19  	return Face(d + 2)
    20  }
    21  
    22  // Opposite returns Direction opposite to the current one.
    23  func (d Direction) Opposite() Direction {
    24  	switch d {
    25  	case North:
    26  		return South
    27  	case South:
    28  		return North
    29  	case West:
    30  		return East
    31  	case East:
    32  		return West
    33  	}
    34  	panic("invalid direction")
    35  }
    36  
    37  // RotateRight rotates the direction 90 degrees to the right horizontally and returns the new direction.
    38  func (d Direction) RotateRight() Direction {
    39  	switch d {
    40  	case North:
    41  		return East
    42  	case East:
    43  		return South
    44  	case South:
    45  		return West
    46  	case West:
    47  		return North
    48  	}
    49  	panic("invalid direction")
    50  }
    51  
    52  // RotateLeft rotates the direction 90 degrees to the left horizontally and returns the new direction.
    53  func (d Direction) RotateLeft() Direction {
    54  	switch d {
    55  	case North:
    56  		return West
    57  	case East:
    58  		return North
    59  	case South:
    60  		return East
    61  	case West:
    62  		return South
    63  	}
    64  	panic("invalid direction")
    65  }
    66  
    67  // String returns the Direction as a string.
    68  func (d Direction) String() string {
    69  	switch d {
    70  	case North:
    71  		return "north"
    72  	case East:
    73  		return "east"
    74  	case South:
    75  		return "south"
    76  	case West:
    77  		return "west"
    78  	}
    79  	panic("invalid direction")
    80  }
    81  
    82  var directions = [...]Direction{North, East, South, West}
    83  
    84  // Directions returns a list of all directions, going from North to West.
    85  func Directions() []Direction {
    86  	return directions[:]
    87  }