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

mlange-42 / ark / 13522785775

25 Feb 2025 01:49PM CUT coverage: 84.715% (+1.5%) from 83.258%
13522785775

Pull #69

github

web-flow
Merge 66cadb6fd into 747bd4f00
Pull Request #69: Add test coverage badge to README

2954 of 3487 relevant lines covered (84.71%)

52321.41 hits per line

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

91.11
/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 {
490,435✔
25
        m.relations = relationEntities(rel).toRelation(m.id, m.relations)
490,435✔
26
        return m.world.newEntityWith([]ID{m.id}, []unsafe.Pointer{unsafe.Pointer(comp)}, m.relations)
490,435✔
27
}
490,435✔
28

29
// Get returns the mapped component for the given entity.
30
func (m *Map[T]) Get(entity Entity) *T {
490,435✔
31
        if !m.world.Alive(entity) {
490,435✔
32
                panic("can't get a component of a dead entity")
×
33
        }
34
        return m.GetUnchecked(entity)
490,435✔
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 {
490,435✔
41
        index := m.world.storage.entities[entity.id]
490,435✔
42
        checkMapHasComponent(m.storage, index.table)
490,435✔
43
        return (*T)(m.storage.columns[index.table].Get(uintptr(index.row)))
490,435✔
44
}
490,435✔
45

46
// Has return whether the given entity has the mapped component.
47
func (m *Map[T]) Has(entity Entity) bool {
4✔
48
        if !m.world.Alive(entity) {
4✔
49
                panic("can't get a component of a dead entity")
×
50
        }
51
        return m.HasUnchecked(entity)
4✔
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 {
4✔
58
        index := m.world.storage.entities[entity.id]
4✔
59
        return m.storage.columns[index.table] != nil
4✔
60
}
4✔
61

62
// Add the mapped component to the given entity.
63
func (m *Map[T]) Add(entity Entity, comp *T, rel ...Entity) {
453✔
64
        if !m.world.Alive(entity) {
453✔
65
                panic("can't add a component to a dead entity")
×
66
        }
67
        m.relations = relationEntities(rel).toRelation(m.id, m.relations)
453✔
68
        m.world.exchange(entity, []ID{m.id}, nil, []unsafe.Pointer{unsafe.Pointer(comp)}, m.relations)
453✔
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) {
33✔
81
        m.relations = target.toRelation(m.id, m.relations)
33✔
82
        m.world.setRelations(entity, m.relations)
33✔
83
}
33✔
84

85
// GetRelation returns the relation target for the entity and the mapped component.
86
func (m *Map[T]) GetRelation(entity Entity) Entity {
99✔
87
        return m.world.storage.getRelation(entity, m.id)
99✔
88
}
99✔
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