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

mlange-42 / arche / 4841143979

29 Apr 2023 10:47PM CUT coverage: 100.0%. Remained the same
4841143979

Pull #239

github

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

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

3036 of 3036 relevant lines covered (100.0%)

2635.32 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() Entity {
4✔
39
        if b.comps == nil {
6✔
40
                return b.world.NewEntity(b.ids...)
2✔
41
        }
2✔
42
        return b.world.NewEntityWith(b.comps...)
2✔
43
}
44

45
// NewRelation creates an entity with a relation target.
46
func (b *Builder) NewRelation(target Entity) Entity {
4✔
47
        if !b.hasTarget {
6✔
48
                panic("entity builder has no target")
2✔
49
        }
50
        if b.comps == nil {
3✔
51
                return b.world.newEntityTarget(b.targetID, target, b.ids...)
1✔
52
        }
1✔
53
        return b.world.newEntityTargetWith(b.targetID, target, b.comps...)
1✔
54
}
55

56
// NewBatch creates many entities.
57
func (b *Builder) NewBatch(count int) {
6✔
58
        if b.comps == nil {
11✔
59
                b.world.newEntities(count, -1, Entity{}, b.ids...)
5✔
60
        } else {
6✔
61
                b.world.newEntitiesWith(count, -1, Entity{}, b.comps...)
1✔
62
        }
1✔
63
}
64

65
// NewBatchRelation creates many entities with a relation target.
66
func (b *Builder) NewBatchRelation(count int, target Entity) {
4✔
67
        if !b.hasTarget {
6✔
68
                panic("entity builder has no target")
2✔
69
        }
70
        if b.comps == nil {
3✔
71
                b.world.newEntities(count, int8(b.targetID), target, b.ids...)
1✔
72
        } else {
2✔
73
                b.world.newEntitiesWith(count, int8(b.targetID), target, b.comps...)
1✔
74
        }
1✔
75
}
76

77
// NewQuery creates many entities and returns a query over them.
78
func (b *Builder) NewQuery(count int) Query {
2✔
79
        if b.comps == nil {
3✔
80
                return b.world.newEntitiesQuery(count, -1, Entity{}, b.ids...)
1✔
81
        }
1✔
82
        return b.world.newEntitiesWithQuery(count, -1, Entity{}, b.comps...)
1✔
83
}
84

85
// NewQueryRelation creates many entities with a relation target and returns a query over them.
86
func (b *Builder) NewQueryRelation(count int, target Entity) Query {
4✔
87
        if !b.hasTarget {
6✔
88
                panic("entity builder has no target")
2✔
89
        }
90
        if b.comps == nil {
3✔
91
                return b.world.newEntitiesQuery(count, int8(b.targetID), target, b.ids...)
1✔
92
        }
1✔
93
        return b.world.newEntitiesWithQuery(count, int8(b.targetID), target, b.comps...)
1✔
94
}
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