This repository has been archived by the owner on Jun 12, 2019. It is now read-only.
/
motion.go
78 lines (67 loc) · 1.82 KB
/
motion.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package gomcbot
import (
"math"
"time"
)
// SetPosition method move your character around.
// Server will ignore this if changes too much.
func (g *Game) SetPosition(x, y, z float64, onGround bool) {
g.motion <- func() {
g.player.X, g.player.Y, g.player.Z = x, y, z
g.player.OnGround = onGround
sendPlayerPositionPacket(g) //向服务器更新位置
}
}
// LookAt method turn player's hand and make it look at a point.
func (g *Game) LookAt(x, y, z float64) {
x0, y0, z0 := g.player.X, g.player.Y, g.player.Z
x, y, z = x-x0, y-y0, z-z0
r := math.Sqrt(x*x + y*y + z*z)
yaw := -math.Atan2(x, z) / math.Pi * 180
for yaw < 0 {
yaw = 360 + yaw
}
pitch := -math.Asin(y/r) / math.Pi * 180
g.LookYawPitch(float32(yaw), float32(pitch))
}
// LookYawPitch set player's hand to the direct by yaw and pitch.
// yaw can be [0, 360) and pitch can be (-180, 180).
// if |pitch|>90 the player's hand will be very strange.
func (g *Game) LookYawPitch(yaw, pitch float32) {
g.motion <- func() {
g.player.Yaw, g.player.Pitch = yaw, pitch
sendPlayerLookPacket(g) //向服务器更新朝向
}
}
// SwingHand sent when the player's arm swings.
// if hand is true, swing the main hand
func (g *Game) SwingHand(hand bool) {
if hand {
sendAnimationPacket(g, 0)
} else {
sendAnimationPacket(g, 1)
}
}
// Dig a block in the position and wait for it's breaked
func (g *Game) Dig(x, y, z int) error {
b := g.GetBlock(x, y, z).id
sendPlayerDiggingPacket(g, 0, x, y, z, Top) //start
sendPlayerDiggingPacket(g, 2, x, y, z, Top) //end
for {
time.Sleep(time.Millisecond * 50)
if g.GetBlock(x, y, z).id != b {
break
}
g.SwingHand(true)
}
return nil
}
// UseItem use the item in hand.
// if hand is true, swing the main hand
func (g *Game) UseItem(hand bool) {
if hand {
sendUseItemPacket(g, 0)
} else {
sendUseItemPacket(g, 1)
}
}