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

mlange-42 / ark / 13617144606

02 Mar 2025 04:11PM CUT coverage: 97.422% (-0.002%) from 97.424%
13617144606

push

github

web-flow
Simplify component/resource registration functions (#100)

2 of 2 new or added lines in 1 file covered. (100.0%)

3893 of 3996 relevant lines covered (97.42%)

46242.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.88
/ecs/relation.go
1
package ecs
2

3
import "fmt"
4

5
var relationType = typeOf[Relation]()
6

7
// Relation is a marker for entity relation components.
8
// It must be embedded as first field of a component that represent an entity relation
9
// (see the example).
10
//
11
// Entity relations allow for fast queries using entity relationships.
12
// E.g. to iterate over all entities that are the child of a certain parent entity.
13
// Currently, each entity can only have a single relation component.
14
type Relation struct{}
15

16
// RelationID specifies an entity relation target by component [ID].
17
//
18
// It is used in Ark's unsafe, ID-based API.
19
type RelationID struct {
20
        component ID
21
        target    Entity
22
}
23

24
// RelID creates a new [RelationID].
25
func RelID(id ID, target Entity) RelationID {
41✔
26
        return RelationID{
41✔
27
                component: id,
41✔
28
                target:    target,
41✔
29
        }
41✔
30
}
41✔
31

32
// RelationIndex specifies an entity relation target by component index.
33
//
34
// Note that the index refers to the position of the component in the generics
35
// of e.g. a [Map2] or [Filter2].
36
// This should not be confused with component [ID] as obtained by [ComponentID]!
37
type RelationIndex struct {
38
        index  uint8
39
        target Entity
40
}
41

42
// Rel creates a new [RelationIndex].
43
func Rel(index int, target Entity) RelationIndex {
392✔
44
        return RelationIndex{
392✔
45
                index:  uint8(index),
392✔
46
                target: target,
392✔
47
        }
392✔
48
}
392✔
49

50
type relations []RelationIndex
51

52
func (r relations) toRelations(reg *componentRegistry, ids []ID, out []RelationID) []RelationID {
2,203✔
53
        out = out[:0]
2,203✔
54
        for _, rel := range r {
2,586✔
55
                id := ids[rel.index]
383✔
56
                if !reg.IsRelation[id.id] {
383✔
57
                        panic(fmt.Sprintf("component at index %d is not a relation component", rel.index))
×
58
                }
59
                out = append(out, RelationID{
383✔
60
                        component: id,
383✔
61
                        target:    rel.target,
383✔
62
                })
383✔
63
        }
64
        return out
2,203✔
65
}
66

67
type relationEntities []Entity
68

69
func (r relationEntities) toRelation(id ID, out []RelationID) []RelationID {
494,967✔
70
        out = out[:0]
494,967✔
71
        for _, rel := range r {
495,000✔
72
                out = append(out, RelationID{
33✔
73
                        component: id,
33✔
74
                        target:    rel,
33✔
75
                })
33✔
76
        }
33✔
77
        return out
494,967✔
78
}
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