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

mlange-42 / ark / 13605076179

01 Mar 2025 01:04PM CUT coverage: 96.958% (-0.07%) from 97.025%
13605076179

Pull #83

github

web-flow
Merge 72026b89c into 3013a616a
Pull Request #83: Add functionality required for (de)-serialization

106 of 113 new or added lines in 4 files covered. (93.81%)

3825 of 3945 relevant lines covered (96.96%)

47444.75 hits per line

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

93.18
/ecs/functions.go
1
package ecs
2

3
import "reflect"
4

5
// ComponentID returns the [ID] for a component type via generics.
6
// Registers the type if it is not already registered.
7
//
8
// The number of unique component types per [World] is limited to 256 ([MaskTotalBits]).
9
//
10
// Panics if called on a locked world and the type is not registered yet.
11
//
12
// Note that type aliases are not considered separate component types.
13
// Type re-definitions, however, are separate types.
14
//
15
// ⚠️ Warning: Using IDs that are outside of the range of registered IDs anywhere in [World] or other places will result in undefined behavior!
16
func ComponentID[T any](w *World) ID {
1,899✔
17
        tp := reflect.TypeOf((*T)(nil)).Elem()
1,899✔
18
        return w.componentID(tp)
1,899✔
19
}
1,899✔
20

21
// ComponentIDs returns a list of all registered component IDs.
22
func ComponentIDs(w *World) []ID {
1✔
23
        intIds := w.storage.registry.IDs
1✔
24
        ids := make([]ID, len(intIds))
1✔
25
        for i, iid := range intIds {
3✔
26
                ids[i] = id8(iid)
2✔
27
        }
2✔
28
        return ids
1✔
29
}
30

31
// ComponentInfo returns the [CompInfo] for a component [ID], and whether the ID is assigned.
32
func ComponentInfo(w *World, id ID) (CompInfo, bool) {
2✔
33
        tp, ok := w.storage.registry.ComponentType(id.id)
2✔
34
        if !ok {
3✔
35
                return CompInfo{}, false
1✔
36
        }
1✔
37

38
        return CompInfo{
1✔
39
                ID:         id,
1✔
40
                Type:       tp,
1✔
41
                IsRelation: w.storage.registry.IsRelation[id.id],
1✔
42
        }, true
1✔
43
}
44

45
// TypeID returns the [ID] for a component type.
46
// Registers the type if it is not already registered.
47
//
48
// The number of unique component types per [World] is limited to [MaskTotalBits].
49
func TypeID(w *World, tp reflect.Type) ID {
2✔
50
        return w.componentID(tp)
2✔
51
}
2✔
52

53
// Comp is a helper to pass component types to functions and methods.
54
// Use function [C] to create one.
55
type Comp struct {
56
        tp reflect.Type
57
}
58

59
// C creates a [Comp] instance for the given type.
60
func C[T any]() Comp {
67✔
61
        return Comp{typeOf[T]()}
67✔
62
}
67✔
63

64
// Type returns the reflect.Type of the component.
NEW
65
func (c Comp) Type() reflect.Type {
×
NEW
66
        return c.tp
×
NEW
67
}
×
68

69
// ResourceID returns the [ResID] for a resource type via generics.
70
// Registers the type if it is not already registered.
71
//
72
// The number of resources per [World] is limited to [MaskTotalBits].
73
func ResourceID[T any](w *World) ResID {
6✔
74
        tp := reflect.TypeOf((*T)(nil)).Elem()
6✔
75
        return w.resourceID(tp)
6✔
76
}
6✔
77

78
// ResourceIDs returns a list of all registered resource IDs.
79
func ResourceIDs(w *World) []ResID {
1✔
80
        intIds := w.resources.registry.IDs
1✔
81
        ids := make([]ResID, len(intIds))
1✔
82
        for i, iid := range intIds {
3✔
83
                ids[i] = ResID{id: iid}
2✔
84
        }
2✔
85
        return ids
1✔
86
}
87

88
// ResourceType returns the reflect.Type for a resource [ResID], and whether the ID is assigned.
89
func ResourceType(w *World, id ResID) (reflect.Type, bool) {
2✔
90
        return w.resources.registry.ComponentType(id.id)
2✔
91
}
2✔
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