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