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

mlange-42 / arche / 12455637666

22 Dec 2024 03:48PM CUT coverage: 100.0%. Remained the same
12455637666

Pull #442

github

web-flow
Merge dcf4cea1c into 30c1f4bc4
Pull Request #442: Update CHANGELOG for release v0.14.0

6411 of 6411 relevant lines covered (100.0%)

114030.79 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
//
23
// Deprecated: This method is slow. Instead, use NewWith of the generic API
24
// under [github.com/mlange-42/arche/generic.Map1], etc.
25
// This function may be removed in a future version.
26
func NewBuilderWith(w *World, comps ...Component) *Builder {
4✔
27
        return &Builder{
4✔
28
                world: w,
4✔
29
                ids:   nil,
4✔
30
                comps: comps,
4✔
31
        }
4✔
32
}
4✔
33

34
// WithRelation sets the [Relation] component for the builder.
35
//
36
// Use in conjunction with the optional target argument of [Builder.New], [Builder.NewBatch] and [Builder.NewBatchQ].
37
//
38
// See [Relation] for details and examples.
39
func (b *Builder) WithRelation(comp ID) *Builder {
53✔
40
        b.hasRelation = true
53✔
41
        b.relationID = comp
53✔
42
        return b
53✔
43
}
53✔
44

45
// New creates an entity.
46
//
47
// The optional argument can be used to set the target [Entity] for the Builder's [Relation].
48
// See [Builder.WithRelation].
49
func (b *Builder) New(target ...Entity) Entity {
25,023✔
50
        if len(target) > 0 {
25,038✔
51
                if !b.hasRelation {
17✔
52
                        panic("can't set target entity: builder has no relation")
2✔
53
                }
54
                if b.comps == nil {
25✔
55
                        return b.world.newEntityTarget(b.relationID, target[0], b.ids...)
12✔
56
                }
12✔
57
                return b.world.newEntityTargetWith(b.relationID, target[0], b.comps...)
1✔
58
        }
59
        if b.comps == nil {
50,013✔
60
                return b.world.NewEntity(b.ids...)
25,005✔
61
        }
25,005✔
62
        return b.world.NewEntityWith(b.comps...)
3✔
63
}
64

65
// NewBatch creates many entities.
66
//
67
// The optional argument can be used to set the target [Entity] for the Builder's [Relation].
68
// See [Builder.WithRelation].
69
func (b *Builder) NewBatch(count int, target ...Entity) {
27,549✔
70
        if len(target) > 0 {
55,072✔
71
                if !b.hasRelation {
27,525✔
72
                        panic("can't set target entity: builder has no relation")
2✔
73
                }
74
                if b.comps == nil {
55,040✔
75
                        b.world.newEntities(count, b.relationID, true, target[0], b.ids...)
27,519✔
76
                        return
27,519✔
77
                }
27,519✔
78
                b.world.newEntitiesWith(count, b.relationID, true, target[0], b.comps...)
2✔
79
                return
2✔
80
        }
81
        if b.comps == nil {
50✔
82
                b.world.newEntities(count, ID{}, false, Entity{}, b.ids...)
24✔
83
        } else {
26✔
84
                b.world.newEntitiesWith(count, ID{}, false, Entity{}, b.comps...)
2✔
85
        }
2✔
86
}
87

88
// NewBatchQ creates many entities and returns a query over them.
89
//
90
// The optional argument can be used to set the target [Entity] for the Builder's [Relation].
91
// See [Builder.WithRelation].
92
func (b *Builder) NewBatchQ(count int, target ...Entity) Query {
39✔
93
        if len(target) > 0 {
47✔
94
                if !b.hasRelation {
10✔
95
                        panic("can't set target entity: builder has no relation")
2✔
96
                }
97
                if b.comps == nil {
10✔
98
                        return b.world.newEntitiesQuery(count, b.relationID, true, target[0], b.ids...)
4✔
99
                }
4✔
100
                return b.world.newEntitiesWithQuery(count, b.relationID, true, target[0], b.comps...)
2✔
101
        }
102
        if b.comps == nil {
60✔
103
                return b.world.newEntitiesQuery(count, ID{}, false, Entity{}, b.ids...)
29✔
104
        }
29✔
105
        return b.world.newEntitiesWithQuery(count, ID{}, false, Entity{}, b.comps...)
2✔
106
}
107

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