-
Notifications
You must be signed in to change notification settings - Fork 5
/
builder.go
99 lines (86 loc) · 2.06 KB
/
builder.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
package object
import (
"github.com/johanhenriksson/goworld/math/quat"
"github.com/johanhenriksson/goworld/math/vec3"
"github.com/johanhenriksson/goworld/render/texture"
)
// Builder API for game objects
type builder[K Object] struct {
object K
position vec3.T
rotation quat.T
scale vec3.T
active bool
parent Object
children []Component
}
// Builder instantiates a new group builder.
func Builder[K Object](object K) *builder[K] {
return &builder[K]{
object: object,
position: vec3.Zero,
rotation: quat.Ident(),
scale: vec3.One,
active: true,
}
}
// Attach a child component
func (b *builder[K]) Attach(child Component) *builder[K] {
b.children = append(b.children, child)
return b
}
// Set the parent of the object
func (b *builder[K]) Parent(parent Object) *builder[K] {
b.parent = parent
return b
}
// Position sets the intial position of the object.
func (b *builder[K]) Position(p vec3.T) *builder[K] {
b.position = p
return b
}
// Rotation sets the intial rotation of the object.
func (b *builder[K]) Rotation(r quat.T) *builder[K] {
b.rotation = r
return b
}
// Scale sets the intial scale of the object.
func (b *builder[K]) Scale(s vec3.T) *builder[K] {
b.scale = s
return b
}
// Active sets the objects active flag.
func (b *builder[K]) Active(active bool) *builder[K] {
b.active = active
return b
}
func (b *builder[K]) Texture(slot texture.Slot, ref texture.Ref) *builder[K] {
type Textured interface {
SetTexture(slot texture.Slot, ref texture.Ref)
}
if textured, ok := any(b.object).(Textured); ok {
textured.SetTexture(slot, ref)
} else {
// todo: raise a warning if its not possible?
}
return b
}
// Create instantiates a new object with the current builder settings.
func (b *builder[K]) Create() K {
obj := b.object
obj.Transform().SetPosition(b.position)
obj.Transform().SetRotation(b.rotation)
obj.Transform().SetScale(b.scale)
if b.active {
Enable(obj)
} else {
Disable(obj)
}
for _, child := range b.children {
Attach(obj, child)
}
if b.parent != nil {
Attach(b.parent, obj)
}
return obj
}