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

mlange-42 / ark / 15075416534

16 May 2025 06:54PM CUT coverage: 99.833%. Remained the same
15075416534

Pull #251

github

web-flow
Merge a3a4140a2 into 94436e9e0
Pull Request #251: Prepare release v0.4.3

8352 of 8366 relevant lines covered (99.83%)

18973.76 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
type entityID uint32
9

10
var entityType = reflect.TypeFor[Entity]()
11
var entitySize = sizeOf(entityType)
12
var entityIndexSize = sizeOf(reflect.TypeFor[entityIndex]())
13

14
//var wildcard = Entity{1, 0}
15

16
// Entity is an identifier for entities.
17
//
18
// Can be stored safely in components, resources or elsewhere.
19
// For stored entities, it may be necessary to check their alive status with [World.Alive].
20
//
21
// ⚠️ Always store entities by value, never by pointer!
22
//
23
// In Ark, entities are returned to a pool when they are removed from the world.
24
// These entities can be recycled, with the same ID ([Entity.ID]), but an incremented generation ([Entity.Gen]).
25
// This allows to determine whether an entity hold by the user is still alive, despite it was potentially recycled.
26
type Entity struct {
27
        id  entityID // Entity ID
28
        gen uint32   // Entity generation
29
}
30

31
func newEntity(id entityID) Entity {
8✔
32
        return Entity{id, 0}
8✔
33
}
8✔
34

35
// ID returns the entity's ID, primarily for debugging purposes.
36
func (e Entity) ID() uint32 {
101✔
37
        return uint32(e.id)
101✔
38
}
101✔
39

40
// Gen returns the entity's generation, primarily for debugging purposes.
41
func (e Entity) Gen() uint32 {
1✔
42
        return e.gen
1✔
43
}
1✔
44

45
// IsZero returns whether this entity is the reserved zero entity.
46
func (e Entity) IsZero() bool {
5,059✔
47
        return e.id == 0
5,059✔
48
}
5,059✔
49

50
// isWildcard returns whether this entity is the reserved wildcard entity.
51
func (e Entity) isWildcard() bool {
1✔
52
        return e.id == 1
1✔
53
}
1✔
54

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

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

1✔
75
        return nil
1✔
76
}
77

78
// entityIndex denotes an entity's location by table and row index.
79
type entityIndex struct {
80
        table tableID
81
        row   uint32
82
}
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