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

mlange-42 / arche / 7863623919

11 Feb 2024 05:36PM CUT coverage: 100.0%. Remained the same
7863623919

push

github

web-flow
Use Go 1.22 in CI, rename examples directory (#376)

* Use Go 1.22 in CI
* rename folders that should be ignored by testing and coverage
* fix CI paths to examples
* fix all links to the renamed _examples directory
* update changelog

6226 of 6226 relevant lines covered (100.0%)

76777.76 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
        hasRelation bool
9
        relationID  ID
10
}
11

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

21
// NewBuilderWith creates a builder from component pointers.
22
func NewBuilderWith(w *World, comps ...Component) *Builder {
4✔
23
        return &Builder{
4✔
24
                world: w,
4✔
25
                ids:   nil,
4✔
26
                comps: comps,
4✔
27
        }
4✔
28
}
4✔
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.NewBatchQ].
33
//
34
// See [Relation] for details and examples.
35
func (b *Builder) WithRelation(comp ID) *Builder {
53✔
36
        b.hasRelation = true
53✔
37
        b.relationID = comp
53✔
38
        return b
53✔
39
}
53✔
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,023✔
46
        if len(target) > 0 {
25,038✔
47
                if !b.hasRelation {
17✔
48
                        panic("can't set target entity: builder has no relation")
2✔
49
                }
50
                if b.comps == nil {
25✔
51
                        return b.world.newEntityTarget(b.relationID, target[0], b.ids...)
12✔
52
                }
12✔
53
                return b.world.newEntityTargetWith(b.relationID, target[0], b.comps...)
1✔
54
        }
55
        if b.comps == nil {
50,013✔
56
                return b.world.NewEntity(b.ids...)
25,005✔
57
        }
25,005✔
58
        return b.world.NewEntityWith(b.comps...)
3✔
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,549✔
66
        if len(target) > 0 {
55,072✔
67
                if !b.hasRelation {
27,525✔
68
                        panic("can't set target entity: builder has no relation")
2✔
69
                }
70
                if b.comps == nil {
55,040✔
71
                        b.world.newEntities(count, b.relationID, true, target[0], b.ids...)
27,519✔
72
                        return
27,519✔
73
                }
27,519✔
74
                b.world.newEntitiesWith(count, b.relationID, true, target[0], b.comps...)
2✔
75
                return
2✔
76
        }
77
        if b.comps == nil {
50✔
78
                b.world.newEntities(count, ID{}, false, Entity{}, b.ids...)
24✔
79
        } else {
26✔
80
                b.world.newEntitiesWith(count, ID{}, false, Entity{}, b.comps...)
2✔
81
        }
2✔
82
}
83

84
// NewBatchQ 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) NewBatchQ(count int, target ...Entity) Query {
39✔
89
        if len(target) > 0 {
47✔
90
                if !b.hasRelation {
10✔
91
                        panic("can't set target entity: builder has no relation")
2✔
92
                }
93
                if b.comps == nil {
10✔
94
                        return b.world.newEntitiesQuery(count, b.relationID, true, target[0], b.ids...)
4✔
95
                }
4✔
96
                return b.world.newEntitiesWithQuery(count, b.relationID, true, target[0], b.comps...)
2✔
97
        }
98
        if b.comps == nil {
60✔
99
                return b.world.newEntitiesQuery(count, ID{}, false, Entity{}, b.ids...)
29✔
100
        }
29✔
101
        return b.world.newEntitiesWithQuery(count, ID{}, false, Entity{}, b.comps...)
2✔
102
}
103

104
// Add the builder's components to an entity.
105
//
106
// The optional argument can be used to set the target [Entity] for the Builder's [Relation].
107
// See [Builder.WithRelation].
108
func (b *Builder) Add(entity Entity, target ...Entity) {
7✔
109
        if len(target) > 0 {
11✔
110
                if !b.hasRelation {
6✔
111
                        panic("can't set target entity: builder has no relation")
2✔
112
                }
113
                if b.comps == nil {
3✔
114
                        b.world.exchange(entity, b.ids, nil, b.relationID, b.hasRelation, target[0])
1✔
115
                        return
1✔
116
                }
1✔
117
                b.world.assign(entity, b.relationID, b.hasRelation, target[0], b.comps...)
1✔
118
                return
1✔
119
        }
120
        if b.comps == nil {
5✔
121
                b.world.Exchange(entity, b.ids, nil)
2✔
122
                return
2✔
123
        }
2✔
124
        b.world.Assign(entity, b.comps...)
1✔
125
}
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