• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / arche / 4866344964

02 May 2023 10:26PM CUT coverage: 100.0%. Remained the same
4866344964

push

github

GitHub
Better document RelationsUnchecked et al. methods (#261)

1 of 1 new or added line in 1 file covered. (100.0%)

3543 of 3543 relevant lines covered (100.0%)

1860.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/ecs/builder.go
1
package ecs
2

3
// Builder for more flexible and batched entity creation.
4
type Builder struct {
5
        world     *World
6
        ids       []ID
7
        comps     []Component
8
        hasTarget bool
9
        targetID  ID
10
}
11

12
// NewBuilder creates a builder from component IDs.
13
func NewBuilder(w *World, comps ...ID) *Builder {
11✔
14
        return &Builder{
11✔
15
                world: w,
11✔
16
                ids:   comps,
11✔
17
                comps: nil,
11✔
18
        }
11✔
19
}
11✔
20

21
// NewBuilderWith creates a builder from component pointers.
22
func NewBuilderWith(w *World, comps ...Component) *Builder {
2✔
23
        return &Builder{
2✔
24
                world: w,
2✔
25
                ids:   nil,
2✔
26
                comps: comps,
2✔
27
        }
2✔
28
}
2✔
29

30
// WithRelation sets the [Relation] component for the builder.
31
//
32
// Use in conjunction with the optional target argument of [Builder.New], [Builder.NewBatch] and [Builder.NewQuery].
33
//
34
// See [Relation] for details and examples.
35
func (b *Builder) WithRelation(comp ID) *Builder {
7✔
36
        b.hasTarget = true
7✔
37
        b.targetID = comp
7✔
38
        return b
7✔
39
}
7✔
40

41
// New creates an entity.
42
//
43
// The optional argument can be used to set the target [Entity] for the Builder's [Relation].
44
// See [Builder.WithRelation].
45
func (b *Builder) New(target ...Entity) Entity {
9✔
46
        if len(target) > 0 {
14✔
47
                if !b.hasTarget {
7✔
48
                        panic("entity builder has no target")
2✔
49
                }
50
                if b.comps == nil {
5✔
51
                        return b.world.newEntityTarget(b.targetID, target[0], b.ids...)
2✔
52
                }
2✔
53
                return b.world.newEntityTargetWith(b.targetID, target[0], b.comps...)
1✔
54
        }
55
        if b.comps == nil {
6✔
56
                return b.world.NewEntity(b.ids...)
2✔
57
        }
2✔
58
        return b.world.NewEntityWith(b.comps...)
2✔
59
}
60

61
// NewBatch creates many entities.
62
//
63
// The optional argument can be used to set the target [Entity] for the Builder's [Relation].
64
// See [Builder.WithRelation].
65
func (b *Builder) NewBatch(count int, target ...Entity) {
16✔
66
        if len(target) > 0 {
25✔
67
                if !b.hasTarget {
11✔
68
                        panic("entity builder has no target")
2✔
69
                }
70
                if b.comps == nil {
13✔
71
                        b.world.newEntities(count, int8(b.targetID), target[0], b.ids...)
6✔
72
                        return
6✔
73
                }
6✔
74
                b.world.newEntitiesWith(count, int8(b.targetID), target[0], b.comps...)
1✔
75
                return
1✔
76
        }
77
        if b.comps == nil {
13✔
78
                b.world.newEntities(count, -1, Entity{}, b.ids...)
6✔
79
        } else {
7✔
80
                b.world.newEntitiesWith(count, -1, Entity{}, b.comps...)
1✔
81
        }
1✔
82
}
83

84
// NewQuery creates many entities and returns a query over them.
85
//
86
// The optional argument can be used to set the target [Entity] for the Builder's [Relation].
87
// See [Builder.WithRelation].
88
func (b *Builder) NewQuery(count int, target ...Entity) Query {
8✔
89
        if len(target) > 0 {
14✔
90
                if !b.hasTarget {
8✔
91
                        panic("entity builder has no target")
2✔
92
                }
93
                if b.comps == nil {
7✔
94
                        return b.world.newEntitiesQuery(count, int8(b.targetID), target[0], b.ids...)
3✔
95
                }
3✔
96
                return b.world.newEntitiesWithQuery(count, int8(b.targetID), target[0], b.comps...)
1✔
97
        }
98
        if b.comps == nil {
3✔
99
                return b.world.newEntitiesQuery(count, -1, Entity{}, b.ids...)
1✔
100
        }
1✔
101
        return b.world.newEntitiesWithQuery(count, -1, Entity{}, b.comps...)
1✔
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc