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

     1  package cube
     2  
     3  const (
     4  	// FaceDown represents the bottom face of a block.
     5  	FaceDown Face = iota
     6  	// FaceUp represents the top face of a block.
     7  	FaceUp
     8  	// FaceNorth represents the north face of a block.
     9  	FaceNorth
    10  	// FaceSouth represents the south face of a block.
    11  	FaceSouth
    12  	// FaceWest represents the west face of the block.
    13  	FaceWest
    14  	// FaceEast represents the east face of the block.
    15  	FaceEast
    16  )
    17  
    18  // Face represents the face of a block or entity.
    19  type Face int
    20  
    21  // Direction converts the Face to a Direction and returns it, assuming the Face is horizontal and not FaceUp
    22  // or FaceDown.
    23  func (f Face) Direction() Direction {
    24  	return Direction(f - 2)
    25  }
    26  
    27  // Opposite returns the opposite face. FaceDown will return up, north will return south and west will return east,
    28  // and vice versa.
    29  func (f Face) Opposite() Face {
    30  	switch f {
    31  	default:
    32  		return FaceUp
    33  	case FaceUp:
    34  		return FaceDown
    35  	case FaceNorth:
    36  		return FaceSouth
    37  	case FaceSouth:
    38  		return FaceNorth
    39  	case FaceWest:
    40  		return FaceEast
    41  	case FaceEast:
    42  		return FaceWest
    43  	}
    44  }
    45  
    46  // Axis returns the axis the face is facing. FaceEast and west correspond to the x-axis, north and south to the z
    47  // axis and up and down to the y-axis.
    48  func (f Face) Axis() Axis {
    49  	switch f {
    50  	default:
    51  		return Y
    52  	case FaceEast, FaceWest:
    53  		return X
    54  	case FaceNorth, FaceSouth:
    55  		return Z
    56  	}
    57  }
    58  
    59  // RotateRight rotates the face 90 degrees to the right horizontally and returns the new face.
    60  func (f Face) RotateRight() Face {
    61  	switch f {
    62  	case FaceNorth:
    63  		return FaceEast
    64  	case FaceEast:
    65  		return FaceSouth
    66  	case FaceSouth:
    67  		return FaceWest
    68  	case FaceWest:
    69  		return FaceNorth
    70  	}
    71  	return f
    72  }
    73  
    74  // RotateLeft rotates the face 90 degrees to the left horizontally and returns the new face.
    75  func (f Face) RotateLeft() Face {
    76  	switch f {
    77  	case FaceNorth:
    78  		return FaceWest
    79  	case FaceEast:
    80  		return FaceNorth
    81  	case FaceSouth:
    82  		return FaceEast
    83  	case FaceWest:
    84  		return FaceSouth
    85  	}
    86  	return f
    87  }
    88  
    89  // String returns the Face as a string.
    90  func (f Face) String() string {
    91  	switch f {
    92  	case FaceUp:
    93  		return "up"
    94  	case FaceDown:
    95  		return "down"
    96  	case FaceNorth:
    97  		return "north"
    98  	case FaceSouth:
    99  		return "south"
   100  	case FaceWest:
   101  		return "west"
   102  	case FaceEast:
   103  		return "east"
   104  	}
   105  	panic("invalid face")
   106  }
   107  
   108  // Faces returns a list of all faces, starting with down, then up, then north to west.
   109  func Faces() []Face {
   110  	return faces[:]
   111  }
   112  
   113  // HorizontalFaces returns a list of all horizontal faces, from north to west.
   114  func HorizontalFaces() []Face {
   115  	return hFaces[:]
   116  }
   117  
   118  var hFaces = [...]Face{FaceNorth, FaceEast, FaceSouth, FaceWest}
   119  
   120  var faces = [...]Face{FaceDown, FaceUp, FaceNorth, FaceEast, FaceSouth, FaceWest}