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

     1  package block
     2  
     3  import "github.com/df-mc/dragonfly/server/block/cube"
     4  
     5  // Attachment describes the attachment of a block to another block. It is either of the type WallAttachment, which can
     6  // only have 90 degree facing values, or StandingAttachment, which has more freedom using a cube.Orientation.
     7  type Attachment struct {
     8  	hanging bool
     9  	facing  cube.Direction
    10  	o       cube.Orientation
    11  }
    12  
    13  // WallAttachment returns an Attachment to a wall with a facing direction.
    14  func WallAttachment(facing cube.Direction) Attachment {
    15  	return Attachment{hanging: true, facing: facing}
    16  }
    17  
    18  // StandingAttachment returns an Attachment to the ground with an orientation.
    19  func StandingAttachment(o cube.Orientation) Attachment {
    20  	return Attachment{o: o}
    21  }
    22  
    23  // Uint8 returns the Attachment as a uint8.
    24  func (a Attachment) Uint8() uint8 {
    25  	if !a.hanging {
    26  		return 1 | (uint8(a.o) << 1)
    27  	}
    28  	return uint8(a.facing) << 1
    29  }
    30  
    31  // FaceUint8 returns the facing of the Attachment as a uint8.
    32  func (a Attachment) FaceUint8() uint8 {
    33  	if !a.hanging {
    34  		return 1
    35  	}
    36  	return uint8(a.facing) << 1
    37  }
    38  
    39  // RotateLeft rotates the Attachment the left way around by 90 degrees.
    40  func (a Attachment) RotateLeft() Attachment {
    41  	return Attachment{hanging: a.hanging, facing: a.facing.RotateLeft(), o: a.o.RotateLeft()}
    42  }
    43  
    44  // RotateRight rotates the Attachment the right way around by 90 degrees.
    45  func (a Attachment) RotateRight() Attachment {
    46  	return Attachment{hanging: a.hanging, facing: a.facing.RotateLeft(), o: a.o.RotateLeft()}
    47  }
    48  
    49  // Rotation returns the rotation of the Attachment, based on the orientation if it's a StandingAttachment, or the
    50  // facing direction if it is a WallAttachment.
    51  func (a Attachment) Rotation() cube.Rotation {
    52  	yaw := a.o.Yaw()
    53  	if a.hanging {
    54  		switch a.facing {
    55  		case cube.West:
    56  			yaw = 90
    57  		case cube.East:
    58  			yaw = -90
    59  		case cube.North:
    60  			yaw = 180
    61  		}
    62  	}
    63  	return cube.Rotation{yaw}
    64  }