forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlingering_potion.go
110 lines (91 loc) · 2.72 KB
/
lingering_potion.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package entity
import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/internal/nbtconv"
"github.com/df-mc/dragonfly/server/item/potion"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
)
// LingeringPotion is a variant of a splash potion that can be thrown to leave clouds with status effects that linger on
// the ground in an area.
type LingeringPotion struct {
splashable
transform
close bool
owner world.Entity
c *ProjectileComputer
}
// NewLingeringPotion ...
func NewLingeringPotion(pos mgl64.Vec3, owner world.Entity, t potion.Potion) *LingeringPotion {
l := &LingeringPotion{
owner: owner,
splashable: splashable{t: t, m: 0.25},
c: newProjectileComputer(0.05, 0.01),
}
l.transform = newTransform(l, pos)
return l
}
// Type returns LingeringPotionType.
func (*LingeringPotion) Type() world.EntityType {
return LingeringPotionType{}
}
// Lingers always returns true.
func (l *LingeringPotion) Lingers() bool {
return true
}
// Tick ...
func (l *LingeringPotion) Tick(w *world.World, current int64) {
if l.close {
_ = l.Close()
return
}
l.mu.Lock()
m, result := l.c.TickMovement(l, l.pos, l.vel, 0, 0)
l.pos, l.vel = m.pos, m.vel
l.mu.Unlock()
m.Send()
if m.pos[1] < float64(w.Range()[0]) && current%10 == 0 {
l.close = true
return
}
if result != nil {
l.splash(l, w, m.pos, result, l.Type().BBox(l))
w.AddEntity(NewAreaEffectCloud(m.pos, l.t))
l.close = true
}
}
// New creates a LingeringPotion with the position and velocity provided. It doesn't spawn the
// LingeringPotion, only returns it.
func (l *LingeringPotion) New(pos, vel mgl64.Vec3, t potion.Potion, owner world.Entity) world.Entity {
lingering := NewLingeringPotion(pos, nil, t)
lingering.vel = vel
lingering.owner = owner
return lingering
}
// Owner ...
func (l *LingeringPotion) Owner() world.Entity {
l.mu.Lock()
defer l.mu.Unlock()
return l.owner
}
// LingeringPotionType is a world.EntityType implementation for LingeringPotion.
type LingeringPotionType struct{}
func (LingeringPotionType) EncodeEntity() string {
return "minecraft:lingering_potion"
}
func (LingeringPotionType) BBox(world.Entity) cube.BBox {
return cube.Box(-0.125, 0, -0.125, 0.125, 0.25, 0.125)
}
func (LingeringPotionType) DecodeNBT(m map[string]any) world.Entity {
pot := NewLingeringPotion(nbtconv.Vec3(m, "Pos"), nil, potion.From(nbtconv.Int32(m, "PotionId")))
pot.vel = nbtconv.Vec3(m, "Motion")
return pot
}
func (LingeringPotionType) EncodeNBT(e world.Entity) map[string]any {
pot := e.(*LingeringPotion)
return map[string]any{
"Pos": nbtconv.Vec3ToFloat32Slice(pot.Position()),
"Motion": nbtconv.Vec3ToFloat32Slice(pot.Velocity()),
"PotionId": pot.t.Uint8(),
}
}