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

mlange-42 / arche / 4841198195

29 Apr 2023 11:04PM CUT coverage: 100.0%. Remained the same
4841198195

Pull #239

github

GitHub
Merge 173fbcffc into 93919c906
Pull Request #239: Entity creation API

380 of 380 new or added lines in 3 files covered. (100.0%)

3037 of 3037 relevant lines covered (100.0%)

2634.48 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 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 {
6✔
14
        return &Builder{
6✔
15
                world: w,
6✔
16
                ids:   comps,
6✔
17
                comps: nil,
6✔
18
        }
6✔
19
}
6✔
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
func (b *Builder) WithRelation(comp ID) *Builder {
2✔
32
        b.hasTarget = true
2✔
33
        b.targetID = comp
2✔
34
        return b
2✔
35
}
2✔
36

37
// New creates an entity.
38
func (b *Builder) New(target ...Entity) Entity {
8✔
39
        if len(target) > 0 {
12✔
40
                if !b.hasTarget {
6✔
41
                        panic("entity builder has no target")
2✔
42
                }
43
                if b.comps == nil {
3✔
44
                        return b.world.newEntityTarget(b.targetID, target[0], b.ids...)
1✔
45
                }
1✔
46
                return b.world.newEntityTargetWith(b.targetID, target[0], b.comps...)
1✔
47
        }
48
        if b.comps == nil {
6✔
49
                return b.world.NewEntity(b.ids...)
2✔
50
        }
2✔
51
        return b.world.NewEntityWith(b.comps...)
2✔
52
}
53

54
// NewBatch creates many entities.
55
func (b *Builder) NewBatch(count int, target ...Entity) {
10✔
56
        if len(target) > 0 {
14✔
57
                if !b.hasTarget {
6✔
58
                        panic("entity builder has no target")
2✔
59
                }
60
                if b.comps == nil {
3✔
61
                        b.world.newEntities(count, int8(b.targetID), target[0], b.ids...)
1✔
62
                        return
1✔
63
                }
1✔
64
                b.world.newEntitiesWith(count, int8(b.targetID), target[0], b.comps...)
1✔
65
                return
1✔
66
        }
67
        if b.comps == nil {
11✔
68
                b.world.newEntities(count, -1, Entity{}, b.ids...)
5✔
69
        } else {
6✔
70
                b.world.newEntitiesWith(count, -1, Entity{}, b.comps...)
1✔
71
        }
1✔
72
}
73

74
// NewQuery creates many entities and returns a query over them.
75
func (b *Builder) NewQuery(count int, target ...Entity) Query {
6✔
76
        if len(target) > 0 {
10✔
77
                if !b.hasTarget {
6✔
78
                        panic("entity builder has no target")
2✔
79
                }
80
                if b.comps == nil {
3✔
81
                        return b.world.newEntitiesQuery(count, int8(b.targetID), target[0], b.ids...)
1✔
82
                }
1✔
83
                return b.world.newEntitiesWithQuery(count, int8(b.targetID), target[0], b.comps...)
1✔
84
        }
85
        if b.comps == nil {
3✔
86
                return b.world.newEntitiesQuery(count, -1, Entity{}, b.ids...)
1✔
87
        }
1✔
88
        return b.world.newEntitiesWithQuery(count, -1, Entity{}, b.comps...)
1✔
89
}
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