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

mlange-42 / arche / 4869574586

03 May 2023 07:56AM CUT coverage: 100.0%. Remained the same
4869574586

push

github

GitHub
Stress test for relations and archetype re-use (#262)

3543 of 3543 relevant lines covered (100.0%)

103316.27 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 {
61✔
14
        return &Builder{
61✔
15
                world: w,
61✔
16
                ids:   comps,
61✔
17
                comps: nil,
61✔
18
        }
61✔
19
}
61✔
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 {
32✔
36
        b.hasTarget = true
32✔
37
        b.targetID = comp
32✔
38
        return b
32✔
39
}
32✔
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 {
25,009✔
46
        if len(target) > 0 {
25,014✔
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 {
50,006✔
56
                return b.world.NewEntity(b.ids...)
25,002✔
57
        }
25,002✔
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) {
27,516✔
66
        if len(target) > 0 {
55,025✔
67
                if !b.hasTarget {
27,511✔
68
                        panic("entity builder has no target")
2✔
69
                }
70
                if b.comps == nil {
55,013✔
71
                        b.world.newEntities(count, int8(b.targetID), target[0], b.ids...)
27,506✔
72
                        return
27,506✔
73
                }
27,506✔
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 {
33✔
89
        if len(target) > 0 {
39✔
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 {
53✔
99
                return b.world.newEntitiesQuery(count, -1, Entity{}, b.ids...)
26✔
100
        }
26✔
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