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

mlange-42 / ark / 13605061062

01 Mar 2025 01:03PM CUT coverage: 96.198% (-0.8%) from 97.025%
13605061062

Pull #83

github

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

76 of 113 new or added lines in 4 files covered. (67.26%)

3795 of 3945 relevant lines covered (96.2%)

48290.2 hits per line

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

25.0
/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,892✔
17
        tp := reflect.TypeOf((*T)(nil)).Elem()
1,892✔
18
        return w.componentID(tp)
1,892✔
19
}
1,892✔
20

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

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

NEW
38
        return CompInfo{
×
NEW
39
                ID:         id,
×
NEW
40
                Type:       tp,
×
NEW
41
                IsRelation: w.storage.registry.IsRelation[id.id],
×
NEW
42
        }, true
×
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].
NEW
49
func TypeID(w *World, tp reflect.Type) ID {
×
NEW
50
        return w.componentID(tp)
×
NEW
51
}
×
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 {
3✔
74
        tp := reflect.TypeOf((*T)(nil)).Elem()
3✔
75
        return w.resourceID(tp)
3✔
76
}
3✔
77

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

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