-
Notifications
You must be signed in to change notification settings - Fork 141
/
bow.go
86 lines (73 loc) · 1.95 KB
/
bow.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
79
80
81
82
83
84
85
86
package item
import (
"github.com/df-mc/dragonfly/server/item/potion"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"math"
"time"
)
// Bow is a ranged weapon that fires arrows.
type Bow struct{}
// MaxCount always returns 1.
func (Bow) MaxCount() int {
return 1
}
// DurabilityInfo ...
func (Bow) DurabilityInfo() DurabilityInfo {
return DurabilityInfo{
MaxDurability: 385,
BrokenItem: simpleItem(Stack{}),
}
}
// Release ...
func (Bow) Release(releaser Releaser, duration time.Duration, ctx *UseContext) {
ticks := duration.Milliseconds() / 50
if ticks < 3 {
return
}
d := float64(ticks) / 20
force := math.Min((d*d+d*2)/3, 1)
if force < 0.1 {
return
}
var tip potion.Potion
creative := releaser.GameMode().CreativeInventory()
if arrow, ok := ctx.FirstFunc(func(stack Stack) bool {
_, ok := stack.Item().(Arrow)
return ok
}); ok {
tip = arrow.Item().(Arrow).Tip
if !creative {
ctx.DamageItem(1)
ctx.Consume(arrow.Grow(-arrow.Count() + 1))
}
} else if !creative {
return
}
rYaw, rPitch := releaser.Rotation()
yaw, pitch := -rYaw, -rPitch
if rYaw > 180 {
yaw = 360 - rYaw
}
proj, ok := world.EntityByName("minecraft:arrow")
if !ok {
return
}
if p, ok := proj.(interface {
New(pos, vel mgl64.Vec3, yaw, pitch float64, owner world.Entity, critical, disallowPickup, obtainArrowOnPickup bool, tip potion.Potion) world.Entity
}); ok {
player := releaser.EncodeEntity() == "minecraft:player"
arrow := p.New(eyePosition(releaser), directionVector(releaser).Mul(force*3), yaw, pitch, releaser, force >= 1, !player, !creative, tip)
releaser.PlaySound(sound.BowShoot{})
releaser.World().AddEntity(arrow)
}
}
// Requirements returns the required items to release this item.
func (Bow) Requirements() []Stack {
return []Stack{NewStack(Arrow{}, 1)}
}
// EncodeItem ...
func (Bow) EncodeItem() (name string, meta int16) {
return "minecraft:bow", 0
}