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

mlange-42 / arche / 7528343499

15 Jan 2024 11:43AM CUT coverage: 100.0%. Remained the same
7528343499

push

github

web-flow
Remove entity ID and generation getters (#332)

4930 of 4930 relevant lines covered (100.0%)

64817.27 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,873✔
33
        return Entity{id, 0}
262,873✔
34
}
262,873✔
35

36
// newEntityGen creates a new Entity with a given generation.
37
func newEntityGen(id eid, gen uint32) Entity {
17✔
38
        return Entity{id, gen}
17✔
39
}
17✔
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
// MarshalJSON returns a JSON representation of the entity, for serialization purposes.
47
//
48
// The JSON representation of an entity is a two-element array of entity ID and generation.
49
func (e Entity) MarshalJSON() ([]byte, error) {
1✔
50
        arr := [2]uint32{uint32(e.id), e.gen}
1✔
51
        jsonValue, _ := json.Marshal(arr) // Ignore the error, as we can be sure this works.
1✔
52
        return jsonValue, nil
1✔
53
}
1✔
54

55
// UnmarshalJSON into an entity.
56
//
57
// For serialization purposes only. Do not use this to create entities!
58
func (e *Entity) UnmarshalJSON(data []byte) error {
2✔
59
        arr := [2]uint32{}
2✔
60
        if err := json.Unmarshal(data, &arr); err != nil {
3✔
61
                return err
1✔
62
        }
1✔
63
        e.id = eid(arr[0])
1✔
64
        e.gen = arr[1]
1✔
65

1✔
66
        return nil
1✔
67
}
68

69
// entityIndex indicates where an entity is currently stored.
70
type entityIndex struct {
71
        arch  *archetype // Entity's current archetype
72
        index uint32     // Entity's current index in the archetype
73
}
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