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

mlange-42 / ark / 13617144606

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

push

github

web-flow
Simplify component/resource registration functions (#100)

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

3893 of 3996 relevant lines covered (97.42%)

46242.85 hits per line

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

91.24
/ecs/world_internal.go
1
package ecs
2

3
import (
4
        "reflect"
5
        "unsafe"
6
)
7

8
func (w *World) newEntityWith(ids []ID, comps []unsafe.Pointer, relations []RelationID) Entity {
495,801✔
9
        w.checkLocked()
495,801✔
10

495,801✔
11
        mask := NewMask(ids...)
495,801✔
12
        newTable := w.storage.findOrCreateTable(&w.storage.tables[0], &mask, relations)
495,801✔
13
        entity, idx := w.storage.createEntity(newTable.id)
495,801✔
14

495,801✔
15
        if comps != nil {
991,528✔
16
                if len(ids) != len(comps) {
495,727✔
17
                        panic("lengths of IDs and components to add do not match")
×
18
                }
19
                for i, id := range ids {
994,408✔
20
                        newTable.Set(id, idx, comps[i])
498,681✔
21
                }
498,681✔
22
        }
23
        w.storage.registerTargets(relations)
495,801✔
24
        return entity
495,801✔
25
}
26

27
func (w *World) newEntitiesWith(count int, ids []ID, comps []unsafe.Pointer, relations []RelationID) {
8✔
28
        w.checkLocked()
8✔
29

8✔
30
        mask := NewMask(ids...)
8✔
31
        newTable := w.storage.findOrCreateTable(&w.storage.tables[0], &mask, relations)
8✔
32

8✔
33
        startIdx := newTable.Len()
8✔
34
        w.storage.createEntities(newTable, count)
8✔
35

8✔
36
        if comps != nil {
16✔
37
                if len(ids) != len(comps) {
8✔
38
                        panic("lengths of IDs and components to add do not match")
×
39
                }
40
                for i := range count {
200✔
41
                        for j, id := range ids {
1,056✔
42
                                newTable.Set(id, uint32(startIdx+i), comps[j])
864✔
43
                        }
864✔
44
                }
45
        }
46
        w.storage.registerTargets(relations)
8✔
47
}
48

49
func (w *World) newEntities(count int, ids []ID, relations []RelationID) (tableID, int) {
8✔
50
        w.checkLocked()
8✔
51

8✔
52
        mask := NewMask(ids...)
8✔
53
        newTable := w.storage.findOrCreateTable(&w.storage.tables[0], &mask, relations)
8✔
54

8✔
55
        startIdx := newTable.Len()
8✔
56
        w.storage.createEntities(newTable, count)
8✔
57
        w.storage.registerTargets(relations)
8✔
58

8✔
59
        return newTable.id, startIdx
8✔
60
}
8✔
61

62
func (w *World) exchange(entity Entity, add []ID, rem []ID, addComps []unsafe.Pointer, relations []RelationID) {
646✔
63
        w.checkLocked()
646✔
64

646✔
65
        if !w.Alive(entity) {
646✔
66
                panic("can't exchange components on a dead entity")
×
67
        }
68
        if len(add) == 0 && len(rem) == 0 {
646✔
69
                return
×
70
        }
×
71

72
        index := &w.storage.entities[entity.id]
646✔
73
        oldTable := &w.storage.tables[index.table]
646✔
74
        oldArchetype := &w.storage.archetypes[oldTable.archetype]
646✔
75

646✔
76
        mask := oldArchetype.mask
646✔
77
        w.storage.getExchangeMask(&mask, add, rem)
646✔
78

646✔
79
        oldIDs := oldArchetype.components
646✔
80

646✔
81
        newTable := w.storage.findOrCreateTable(oldTable, &mask, relations)
646✔
82
        newIndex := newTable.Add(entity)
646✔
83

646✔
84
        for _, id := range oldIDs {
2,348✔
85
                if mask.Get(id) {
2,414✔
86
                        comp := oldTable.Get(id, uintptr(index.row))
712✔
87
                        newTable.Set(id, newIndex, comp)
712✔
88
                }
712✔
89
        }
90
        if addComps != nil {
973✔
91
                if len(add) != len(addComps) {
327✔
92
                        panic("lengths of IDs and components to add do not match")
×
93
                }
94
                for i, id := range add {
1,046✔
95
                        newTable.Set(id, newIndex, addComps[i])
719✔
96
                }
719✔
97
        }
98

99
        swapped := oldTable.Remove(index.row)
646✔
100

646✔
101
        if swapped {
744✔
102
                swapEntity := oldTable.GetEntity(uintptr(index.row))
98✔
103
                w.storage.entities[swapEntity.id].row = index.row
98✔
104
        }
98✔
105
        w.storage.entities[entity.id] = entityIndex{table: newTable.id, row: newIndex}
646✔
106

646✔
107
        w.storage.registerTargets(relations)
646✔
108
}
109

110
// setRelations sets the target entities for an entity relations.
111
func (w *World) setRelations(entity Entity, relations []RelationID) {
43✔
112
        w.checkLocked()
43✔
113

43✔
114
        if !w.storage.entityPool.Alive(entity) {
43✔
115
                panic("can't set relation for a dead entity")
×
116
        }
117

118
        index := &w.storage.entities[entity.id]
43✔
119
        oldTable := &w.storage.tables[index.table]
43✔
120

43✔
121
        newRelations, changed := w.storage.getExchangeTargets(oldTable, relations)
43✔
122
        if !changed {
43✔
123
                return
×
124
        }
×
125

126
        oldArch := &w.storage.archetypes[oldTable.archetype]
43✔
127
        newTable, ok := oldArch.GetTable(&w.storage, newRelations)
43✔
128
        if !ok {
55✔
129
                newTable = w.storage.createTable(oldArch, newRelations)
12✔
130
        }
12✔
131
        newIndex := newTable.Add(entity)
43✔
132

43✔
133
        for _, id := range oldArch.components {
149✔
134
                comp := oldTable.Get(id, uintptr(index.row))
106✔
135
                newTable.Set(id, newIndex, comp)
106✔
136
        }
106✔
137

138
        swapped := oldTable.Remove(index.row)
43✔
139

43✔
140
        if swapped {
59✔
141
                swapEntity := oldTable.GetEntity(uintptr(index.row))
16✔
142
                w.storage.entities[swapEntity.id].row = index.row
16✔
143
        }
16✔
144
        w.storage.entities[entity.id] = entityIndex{table: newTable.id, row: newIndex}
43✔
145

43✔
146
        w.storage.registerTargets(relations)
43✔
147
}
148

149
func (w *World) componentID(tp reflect.Type) ID {
1,971✔
150
        id, newID := w.storage.registry.ComponentID(tp)
1,971✔
151
        if newID {
2,457✔
152
                if w.IsLocked() {
486✔
153
                        w.storage.registry.unregisterLastComponent()
×
154
                        panic("attempt to register a new component in a locked world")
×
155
                }
156
                w.storage.AddComponent(id)
486✔
157
        }
158
        return ID{id: id}
1,971✔
159
}
160

161
func (w *World) resourceID(tp reflect.Type) ResID {
9✔
162
        id, _ := w.resources.registry.ComponentID(tp)
9✔
163
        return ResID{id: id}
9✔
164
}
9✔
165

166
// lock the world and get the lock bit for later unlocking.
167
func (w *World) lock() uint8 {
1,105✔
168
        return w.locks.Lock()
1,105✔
169
}
1,105✔
170

171
// unlock unlocks the given lock bit.
172
func (w *World) unlock(l uint8) {
1,105✔
173
        w.locks.Unlock(l)
1,105✔
174
}
1,105✔
175

176
// checkLocked checks if the world is locked, and panics if so.
177
func (w *World) checkLocked() {
991,196✔
178
        if w.IsLocked() {
991,196✔
179
                panic("attempt to modify a locked world")
×
180
        }
181
}
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