-
Notifications
You must be signed in to change notification settings - Fork 84
/
attachSpace.go
90 lines (78 loc) · 2.42 KB
/
attachSpace.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
package collision
import (
"errors"
"github.com/oakmound/oak/v3/event"
"github.com/oakmound/oak/v3/physics"
)
// An AttachSpace is a composable struct that provides attachment
// functionality for entities. An entity with AttachSpace can have its
// associated space passed into Attach with the vector the space should
// be attached to.
// Example usage: Any moving character with a collision space. When
// moving the character around by the vector passed in to Attach, the space
// will move with it.
type AttachSpace struct {
follow physics.Vector
aSpace **Space
tree *Tree
offX, offY float64
binding event.Binding
}
func (as *AttachSpace) getAttachSpace() *AttachSpace {
return as
}
func (as *AttachSpace) CID() event.CallerID {
return (*as.aSpace).CID
}
var _ attachSpace = &AttachSpace{}
type attachSpace interface {
event.Caller
getAttachSpace() *AttachSpace
}
// Attach attaches v to the given space with optional x,y offsets. See AttachSpace.
func Attach(v physics.Vector, s *Space, tree *Tree, offsets ...float64) error {
return AttachWithBus(v, s, tree, event.DefaultBus, offsets...)
}
func AttachWithBus(v physics.Vector, s *Space, tree *Tree, bus event.Handler, offsets ...float64) error {
en := bus.GetCallerMap().GetEntity(s.CID)
if t, ok := en.(attachSpace); ok {
as := t.getAttachSpace()
as.aSpace = &s
as.follow = v
as.tree = tree
if as.tree == nil {
as.tree = DefaultTree
}
as.binding = event.Bind(bus, event.Enter, t, attachSpaceEnter)
if len(offsets) > 0 {
as.offX = offsets[0]
if len(offsets) > 1 {
as.offY = offsets[1]
}
}
return nil
}
return errors.New("this space's entity is not composed of AttachSpace")
}
// Detach removes the attachSpaceEnter binding from an entity composed with
// AttachSpace
func Detach(s *Space) error {
return DetachWithBus(s, event.DefaultBus)
}
func DetachWithBus(s *Space, bus event.Handler) error {
en := bus.GetCallerMap().GetEntity(s.CID)
if as, ok := en.(attachSpace); ok {
as.getAttachSpace().binding.Unbind()
return nil
}
return errors.New("this space's entity is not composed of AttachSpace")
}
func attachSpaceEnter(asIface attachSpace, _ event.EnterPayload) event.Response {
as := asIface.(attachSpace).getAttachSpace()
x, y := as.follow.X()+as.offX, as.follow.Y()+as.offY
if x != (*as.aSpace).X() ||
y != (*as.aSpace).Y() {
as.tree.UpdateSpace(x, y, (*as.aSpace).GetW(), (*as.aSpace).GetH(), *as.aSpace)
}
return 0
}