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

mlange-42 / ark / 13742253133

08 Mar 2025 10:59PM CUT coverage: 99.43%. Remained the same
13742253133

push

github

web-flow
Update CHANGELOG for v0.3.0 release (#166)

6456 of 6493 relevant lines covered (99.43%)

28632.4 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 "encoding/json"
4

5
type entityID uint32
6

7
var entityType = typeOf[Entity]()
8
var entitySize = sizeOf(entityType)
9
var entityIndexSize = sizeOf(typeOf[entityIndex]())
10

11
//var wildcard = Entity{1, 0}
12

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

28
func newEntity(id entityID) Entity {
8✔
29
        return Entity{id, 0}
8✔
30
}
8✔
31

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

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

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

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

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

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

1✔
72
        return nil
1✔
73
}
74

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