-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcomponent_filter_builder.go
80 lines (63 loc) · 2.23 KB
/
component_filter_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
package akara
import "github.com/gravestench/bitset"
// NewComponentFilterBuilder creates a new builder for a component filter, using
// the given world.
func NewComponentFilterBuilder(w *World) *ComponentFilterBuilder {
return &ComponentFilterBuilder{
world: w,
require: make([]Component, 0),
requireOne: make([]Component, 0),
forbid: make([]Component, 0),
}
}
// ComponentFilterBuilder creates a component filter config
type ComponentFilterBuilder struct {
world *World
require []Component
requireOne []Component
forbid []Component
}
// Build iterates through all components in the filter and registers them in the world,
// to ensure that all components have a unique Component ID. Then, the bitsets for
// Required, OneRequired, and Forbidden bits are set in the corresponding bitsets of the filter.
func (cfb *ComponentFilterBuilder) Build() *ComponentFilter {
f := &ComponentFilter{
Required: bitset.NewBitSet(),
OneRequired: bitset.NewBitSet(),
Forbidden: bitset.NewBitSet(),
}
for idx := range cfb.require {
componentID := cfb.world.RegisterComponent(cfb.require[idx])
f.Required.Set(int(componentID), true)
}
for idx := range cfb.requireOne {
componentID := cfb.world.RegisterComponent(cfb.requireOne[idx])
f.OneRequired.Set(int(componentID), true)
}
for idx := range cfb.forbid {
componentID := cfb.world.RegisterComponent(cfb.forbid[idx])
f.Forbidden.Set(int(componentID), true)
}
return f
}
// Require makes all of the given components required by the filter
func (cfb *ComponentFilterBuilder) Require(components ...Component) *ComponentFilterBuilder {
for idx := range components {
cfb.require = append(cfb.require, components[idx])
}
return cfb
}
// RequireOne makes at least one of the given components required
func (cfb *ComponentFilterBuilder) RequireOne(components ...Component) *ComponentFilterBuilder {
for idx := range components {
cfb.requireOne = append(cfb.requireOne, components[idx])
}
return cfb
}
// Forbid makes all of the given components forbidden by the filter
func (cfb *ComponentFilterBuilder) Forbid(components ...Component) *ComponentFilterBuilder {
for idx := range components {
cfb.forbid = append(cfb.forbid, components[idx])
}
return cfb
}