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

mlange-42 / ark / 13617072942

02 Mar 2025 04:02PM CUT coverage: 97.422% (-0.002%) from 97.424%
13617072942

Pull #100

github

web-flow
Merge d87f29472 into 8f54a3a49
Pull Request #100: Simplify component/resource registration functions

2 of 2 new or added lines in 1 file covered. (100.0%)

3893 of 3996 relevant lines covered (97.42%)

93522.98 hits per line

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

91.67
/ecs/map.go
1
package ecs
2

3
import "unsafe"
4

5
// Map is a mapper to access and manipulate components of an entity.
6
type Map[T any] struct {
7
        world     *World
8
        id        ID
9
        storage   *componentStorage
10
        relations []RelationID
11
}
12

13
// NewMap creates a new [Map].
14
func NewMap[T any](w *World) Map[T] {
72✔
15
        id := ComponentID[T](w)
72✔
16
        return Map[T]{
72✔
17
                world:   w,
72✔
18
                id:      id,
72✔
19
                storage: &w.storage.components[id.id],
72✔
20
        }
72✔
21
}
72✔
22

23
// NewEntity creates a new entity with the mapped component.
24
func (m *Map[T]) NewEntity(comp *T, rel ...Entity) Entity {
1,002,440✔
25
        m.relations = relationEntities(rel).toRelation(m.id, m.relations)
1,002,440✔
26
        return m.world.newEntityWith([]ID{m.id}, []unsafe.Pointer{unsafe.Pointer(comp)}, m.relations)
1,002,440✔
27
}
1,002,440✔
28

29
// Get returns the mapped component for the given entity.
30
func (m *Map[T]) Get(entity Entity) *T {
1,002,440✔
31
        if !m.world.Alive(entity) {
1,002,440✔
32
                panic("can't get a component of a dead entity")
×
33
        }
34
        return m.GetUnchecked(entity)
1,002,440✔
35
}
36

37
// GetUnchecked returns the mapped component for the given entity.
38
// In contrast to [Map.Get], it does not check whether the entity is alive.
39
// Can be used as an optimization when it is certain that the entity is alive.
40
func (m *Map[T]) GetUnchecked(entity Entity) *T {
1,002,440✔
41
        m.world.storage.checkHasComponent(entity, m.id)
1,002,440✔
42
        index := m.world.storage.entities[entity.id]
1,002,440✔
43
        return (*T)(m.storage.columns[index.table].Get(uintptr(index.row)))
1,002,440✔
44
}
1,002,440✔
45

46
// Has return whether the given entity has the mapped component.
47
func (m *Map[T]) Has(entity Entity) bool {
6✔
48
        if !m.world.Alive(entity) {
6✔
49
                panic("can't get a component of a dead entity")
×
50
        }
51
        return m.HasUnchecked(entity)
6✔
52
}
53

54
// HasUnchecked return whether the given entity has the mapped component.
55
// In contrast to [Map.Has], it does not check whether the entity is alive.
56
// Can be used as an optimization when it is certain that the entity is alive.
57
func (m *Map[T]) HasUnchecked(entity Entity) bool {
6✔
58
        index := m.world.storage.entities[entity.id]
6✔
59
        return m.storage.columns[index.table] != nil
6✔
60
}
6✔
61

62
// Add the mapped component to the given entity.
63
func (m *Map[T]) Add(entity Entity, comp *T, rel ...Entity) {
428✔
64
        if !m.world.Alive(entity) {
428✔
65
                panic("can't add a component to a dead entity")
×
66
        }
67
        m.relations = relationEntities(rel).toRelation(m.id, m.relations)
428✔
68
        m.world.exchange(entity, []ID{m.id}, nil, []unsafe.Pointer{unsafe.Pointer(comp)}, m.relations)
428✔
69
}
70

71
// Remove the mapped component from the given entity.
72
func (m *Map[T]) Remove(entity Entity) {
162✔
73
        if !m.world.Alive(entity) {
162✔
74
                panic("can't remove a component from a dead entity")
×
75
        }
76
        m.world.exchange(entity, nil, []ID{m.id}, nil, nil)
162✔
77
}
78

79
// GetRelation returns the relation target for the entity and the mapped component.
80
func (m *Map[T]) GetRelation(entity Entity) Entity {
202✔
81
        return m.world.storage.getRelation(entity, m.id)
202✔
82
}
202✔
83

84
// GetRelationUnchecked returns the relation target for the entity and the mapped component.
85
// In contrast to [Map.GetRelation], it does not check whether the entity is alive.
86
// Can be used as an optimization when it is certain that the entity is alive.
87
func (m *Map[T]) GetRelationUnchecked(entity Entity) Entity {
4✔
88
        return m.world.storage.getRelationUnchecked(entity, m.id)
4✔
89
}
4✔
90

91
// SetRelation sets the relation target for the entity and the mapped component.
92
func (m *Map[T]) SetRelation(entity Entity, target Entity) {
68✔
93
        m.relations = target.toRelation(m.id, m.relations)
68✔
94
        m.world.setRelations(entity, m.relations)
68✔
95
}
68✔
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