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

mlange-42 / ark / 13528862241

25 Feb 2025 07:00PM CUT coverage: 94.795% (+0.008%) from 94.787%
13528862241

push

github

web-flow
Add Map.GetRelationUnchecked (#75)

6 of 6 new or added lines in 2 files covered. (100.0%)

3406 of 3593 relevant lines covered (94.8%)

51323.81 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] {
44✔
15
        id := ComponentID[T](w)
44✔
16
        return Map[T]{
44✔
17
                world:   w,
44✔
18
                id:      id,
44✔
19
                storage: &w.storage.components[id.id],
44✔
20
        }
44✔
21
}
44✔
22

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

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

37
// GetUnchecked returns the mapped component for the given entity.
38
// 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 {
493,639✔
41
        index := m.world.storage.entities[entity.id]
493,639✔
42
        checkMapHasComponent(m.storage, index.table)
493,639✔
43
        return (*T)(m.storage.columns[index.table].Get(uintptr(index.row)))
493,639✔
44
}
493,639✔
45

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

54
// HasUnchecked return whether the given entity has the mapped component.
55
// 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 {
3✔
58
        index := m.world.storage.entities[entity.id]
3✔
59
        return m.storage.columns[index.table] != nil
3✔
60
}
3✔
61

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

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

79
// SetRelation sets the relation target for the entity and the mapped component.
80
func (m *Map[T]) SetRelation(entity Entity, target Entity) {
34✔
81
        m.relations = target.toRelation(m.id, m.relations)
34✔
82
        m.world.setRelations(entity, m.relations)
34✔
83
}
34✔
84

85
// GetRelation returns the relation target for the entity and the mapped component.
86
func (m *Map[T]) GetRelation(entity Entity) Entity {
101✔
87
        return m.world.storage.getRelation(entity, m.id)
101✔
88
}
101✔
89

90
// GetRelationUnchecked returns the relation target for the entity and the mapped component.
91
// It does not check whether the entity is alive.
92
// Can be used as an optimization when it is certain that the entity is alive.
93
func (m *Map[T]) GetRelationUnchecked(entity Entity) Entity {
2✔
94
        return m.world.storage.getRelationUnchecked(entity, m.id)
2✔
95
}
2✔
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