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

mlange-42 / arche / 7495846752

11 Jan 2024 11:39PM CUT coverage: 100.0%. Remained the same
7495846752

push

github

web-flow
Split benchmarks into multiple CI jobs (#321)

* split benchmarks into multiple jobs
* simplify benchmark CI job names

3918 of 3918 relevant lines covered (100.0%)

81315.24 hits per line

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

100.0
/ecs/entity.go
1
package ecs
2

3
import (
4
        "encoding/json"
5
        "reflect"
6
)
7

8
// Reflection type of an [Entity].
9
var entityType = reflect.TypeOf(Entity{})
10

11
// Size of an [Entity] in memory, in bytes.
12
var entitySize uint32 = uint32(entityType.Size())
13

14
// Size of an [entityIndex] in memory.
15
var entityIndexSize uint32 = uint32(reflect.TypeOf(entityIndex{}).Size())
16

17
// Entity identifier.
18
// Holds an entity ID and it's generation for recycling.
19
//
20
// Entities are only created via the [World], using [World.NewEntity] or [World.NewEntityWith].
21
// Batch creation of entities is possible via [Builder].
22
//
23
// ⚠️ Important:
24
// Entities are intended to be stored and passed around via copy, not via pointers!
25
// The zero value should be used to indicate "nil", and can be checked with [Entity.IsZero].
26
type Entity struct {
27
        id  eid    // Entity ID
28
        gen uint32 // Entity generation
29
}
30

31
// newEntity creates a new Entity.
32
func newEntity(id eid) Entity {
262,871✔
33
        return Entity{id, 0}
262,871✔
34
}
262,871✔
35

36
// newEntityGen creates a new Entity with a given generation.
37
func newEntityGen(id eid, gen uint32) Entity {
18✔
38
        return Entity{id, gen}
18✔
39
}
18✔
40

41
// IsZero returns whether this entity is the reserved zero entity.
42
func (e Entity) IsZero() bool {
87,970✔
43
        return e.id == 0
87,970✔
44
}
87,970✔
45

46
// ID of the entity. For serialization purposes only!
47
//
48
// Do not use this in normal model or game code!
49
func (e Entity) ID() uint32 {
5✔
50
        return uint32(e.id)
5✔
51
}
5✔
52

53
// Gen returns the generation of the entity. For serialization purposes only!
54
//
55
// Do not use this in normal model or game code!
56
func (e Entity) Gen() uint32 {
1✔
57
        return e.gen
1✔
58
}
1✔
59

60
// MarshalJSON returns a JSON representation of the entity, for serialization purposes.
61
//
62
// The JSON representation of an entity is a two-element array of entity ID and generation.
63
func (e Entity) MarshalJSON() ([]byte, error) {
1✔
64
        arr := [2]uint32{uint32(e.id), e.gen}
1✔
65
        jsonValue, _ := json.Marshal(arr) // Ignore the error, as we can be sure this works.
1✔
66
        return jsonValue, nil
1✔
67
}
1✔
68

69
// UnmarshalJSON into an entity.
70
//
71
// For serialization purposes only. Do not use this to create entities!
72
func (e *Entity) UnmarshalJSON(data []byte) error {
2✔
73
        arr := [2]uint32{}
2✔
74
        if err := json.Unmarshal(data, &arr); err != nil {
3✔
75
                return err
1✔
76
        }
1✔
77
        e.id = eid(arr[0])
1✔
78
        e.gen = arr[1]
1✔
79

1✔
80
        return nil
1✔
81
}
82

83
// entityIndex indicates where an entity is currently stored.
84
type entityIndex struct {
85
        arch  *archetype // Entity's current archetype
86
        index uint32     // Entity's current index in the archetype
87
}
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