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

mlange-42 / ark / 13620752776

03 Mar 2025 12:17AM CUT coverage: 97.759%. Remained the same
13620752776

push

github

web-flow
Set up user guide (#106)

5192 of 5311 relevant lines covered (97.76%)

38008.39 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 {
445✔
44
        return RelationIndex{
445✔
45
                index:  uint8(index),
445✔
46
                target: target,
445✔
47
        }
445✔
48
}
445✔
49

50
// Helper for converting relations
51
type relations []RelationIndex
52

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

68
// Helper for converting relations
69
type relationEntities []Entity
70

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