-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
55 lines (46 loc) · 1.35 KB
/
handler.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
package npc
import (
"github.com/df-mc/dragonfly/server/entity"
"github.com/df-mc/dragonfly/server/event"
"github.com/df-mc/dragonfly/server/player"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"time"
)
// handler implements the handler for an NPC entity. It manages the execution of the HandlerFunc assigned to the NPC
// and makes sure the *world.Loader's position remains synchronised with that of the NPC.
type handler struct {
player.NopHandler
l *world.Loader
f HandlerFunc
vulnerable bool
}
// HandleHurt ...
func (h *handler) HandleHurt(ctx *event.Context, _ *float64, _ *time.Duration, src world.DamageSource) {
if src, ok := src.(entity.AttackDamageSource); ok {
if attacker, ok := src.Attacker.(*player.Player); ok {
h.f(attacker)
}
}
if !h.vulnerable {
ctx.Cancel()
}
}
// HandleMove ...
func (h *handler) HandleMove(_ *event.Context, pos mgl64.Vec3, _, _ float64) {
h.syncPosition(pos)
}
// HandleTeleport ...
func (h *handler) HandleTeleport(_ *event.Context, pos mgl64.Vec3) {
h.syncPosition(pos)
}
// syncPosition synchronises the position passed with the one in the world.Loader held by the handler. It ensures the
// chunk at this new position is loaded.
func (h *handler) syncPosition(pos mgl64.Vec3) {
h.l.Move(pos)
h.l.Load(1)
}
// HandleQuit ...
func (h *handler) HandleQuit() {
_ = h.l.Close()
}