Skip to content

Commit 0ab0cae

Browse files
authored
Tweak mapper methods (#427)
1 parent 06715a8 commit 0ab0cae

File tree

3 files changed

+100
-101
lines changed

3 files changed

+100
-101
lines changed

ecs/internal/generate/maps.go.template

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (m *Map{{.}}{{$genericsShort}}) NewBatchFn(count int, fn func(Entity, {{$fn
182182
//
183183
// ⚠️ Do not store the obtained pointers outside of the current context!
184184
func (m *Map{{.}}{{$genericsShort}}) Get(entity Entity) {{$returnTypes}} {
185-
if !m.world.Alive(entity) {
185+
if !m.world.storage.entityPool.Alive(entity) {
186186
panic("can't get components of a dead entity")
187187
}
188188
index := &m.world.storage.entities[entity.id]
@@ -211,9 +211,9 @@ func (m *Map{{.}}{{$genericsShort}}) HasAll(entity Entity) bool {
211211
if !m.world.Alive(entity) {
212212
panic("can't check components of a dead entity")
213213
}
214-
index := m.world.storage.entities[entity.id]
214+
table := m.world.storage.entities[entity.id].table
215215
return {{range $i, $v := $upper}}{{if $i}} &&
216-
{{end}}m.storage{{$v}}.columns[index.table] != nil{{end}}
216+
{{end}}m.storage{{$v}}.columns[table] != nil{{end}}
217217
}
218218

219219
// Add the mapped components to the given entity.
@@ -339,7 +339,7 @@ func (m *Map{{.}}{{$genericsShort}}) RemoveBatch(batch Batch, fn func(entity Ent
339339

340340
// GetRelation returns the relation target of an entity for the component at the given index.
341341
func (m *Map{{.}}{{$genericsShort}}) GetRelation(entity Entity, index int) Entity {
342-
if !m.world.Alive(entity) {
342+
if !m.world.storage.entityPool.Alive(entity) {
343343
panic("can't get entity relation target for a dead entity")
344344
}
345345
return m.GetRelationUnchecked(entity, index)

ecs/map.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (m *Map[T]) NewBatchFn(count int, fn func(Entity, *T), target ...Entity) {
140140
//
141141
// ⚠️ Do not store the obtained pointer outside of the current context!
142142
func (m *Map[T]) Get(entity Entity) *T {
143-
if !m.world.Alive(entity) {
143+
if !m.world.storage.entityPool.Alive(entity) {
144144
panic("can't get a component of a dead entity")
145145
}
146146
index := &m.world.storage.entities[entity.id]
@@ -164,7 +164,7 @@ func (m *Map[T]) GetUnchecked(entity Entity) *T {
164164
// Using [Map.Get] and checking for nil pointer may be faster
165165
// than calling [Map.Has] and [Map.Get] subsequently.
166166
func (m *Map[T]) Has(entity Entity) bool {
167-
if !m.world.Alive(entity) {
167+
if !m.world.storage.entityPool.Alive(entity) {
168168
panic("can't get a component of a dead entity")
169169
}
170170
return m.HasUnchecked(entity)
@@ -174,8 +174,7 @@ func (m *Map[T]) Has(entity Entity) bool {
174174
// In contrast to [Map.Has], it does not check whether the entity is alive.
175175
// Can be used as an optimization when it is certain that the entity is alive.
176176
func (m *Map[T]) HasUnchecked(entity Entity) bool {
177-
index := m.world.storage.entities[entity.id]
178-
return m.storage.columns[index.table] != nil
177+
return m.storage.columns[m.world.storage.entities[entity.id].table] != nil
179178
}
180179

181180
// Add the mapped component to the given entity.
@@ -196,7 +195,7 @@ func (m *Map[T]) Add(entity Entity, comp *T, target ...Entity) {
196195
//
197196
// ⚠️ Do not store the obtained pointer outside of the current context!
198197
func (m *Map[T]) AddFn(entity Entity, fn func(*T), target ...Entity) {
199-
if !m.world.Alive(entity) {
198+
if !m.world.storage.entityPool.Alive(entity) {
200199
panic("can't add a component to a dead entity")
201200
}
202201
m.relations = relationEntities(target).ToRelation(m.world, m.id, m.relations)
@@ -220,7 +219,7 @@ func (m *Map[T]) AddFn(entity Entity, fn func(*T), target ...Entity) {
220219
//
221220
// This is not a component operation, so it can be performed on a locked world.
222221
func (m *Map[T]) Set(entity Entity, comp *T) {
223-
if !m.world.Alive(entity) {
222+
if !m.world.storage.entityPool.Alive(entity) {
224223
panic("can't set component of a dead entity")
225224
}
226225
m.world.storage.checkHasComponent(entity, m.ids[0])
@@ -274,7 +273,7 @@ func (m *Map[T]) AddBatchFn(batch Batch, fn func(Entity, *T), target ...Entity)
274273

275274
// Remove the mapped component from the given entity.
276275
func (m *Map[T]) Remove(entity Entity) {
277-
if !m.world.Alive(entity) {
276+
if !m.world.storage.entityPool.Alive(entity) {
278277
panic("can't remove a component from a dead entity")
279278
}
280279
m.world.remove(entity, m.ids[:])

ecs/maps_gen.go

Lines changed: 90 additions & 90 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)