Skip to content

Commit

Permalink
Make node.GerArchetype more idiomatic
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 committed Nov 30, 2024
1 parent b709940 commit 9cf5b65
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
7 changes: 4 additions & 3 deletions ecs/archetype_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ func (a *archNode) Archetypes() archetypes {
// GetArchetype returns the archetype for the given relation target.
//
// The target is ignored if the node has no relation component.
func (a *archNode) GetArchetype(target Entity) *archetype {
func (a *archNode) GetArchetype(target Entity) (*archetype, bool) {
if a.HasRelation {
return a.archetypeMap[target]
arch, ok := a.archetypeMap[target]
return arch, ok
}
return a.archetype
return a.archetype, a.archetype != nil
}

// SetArchetype sets the archetype for a node without a relation.
Expand Down
3 changes: 2 additions & 1 deletion ecs/archetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ func TestArchetypeExtendLayouts(t *testing.T) {

node = newArchNode(All(id(0), id(1)), &nodeData{}, id(2), true, 8, comps)
node.CreateArchetype(16, entity)
arch2 := node.GetArchetype(entity)
arch2, ok := node.GetArchetype(entity)

assert.True(t, ok)
assert.Equal(t, len(arch2.layouts), 16)
node.ExtendArchetypeLayouts(16)
assert.Equal(t, len(arch2.layouts), 16)
Expand Down
12 changes: 6 additions & 6 deletions ecs/world_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,8 @@ func (w *World) setRelation(entity Entity, comp ID, target Entity) {
return
}

arch := oldArch.node.GetArchetype(target)
if arch == nil {
arch, ok := oldArch.node.GetArchetype(target)
if !ok {
arch = w.createArchetype(oldArch.node, target, true)
}

Expand Down Expand Up @@ -824,8 +824,8 @@ func (w *World) setRelationArch(oldArch *archetype, oldArchLen uint32, comp ID,

oldIDs := oldArch.Components()

arch := oldArch.node.GetArchetype(target)
if arch == nil {
arch, ok := oldArch.node.GetArchetype(target)
if !ok {
arch = w.createArchetype(oldArch.node, target, true)
}

Expand Down Expand Up @@ -955,8 +955,8 @@ func (w *World) findOrCreateArchetype(start *archetype, add []ID, rem []ID, targ
curr = next
}
}
arch := curr.GetArchetype(target)
if arch == nil {
arch, ok := curr.GetArchetype(target)
if !ok {
arch = w.createArchetype(curr, target, true)
}
return arch
Expand Down

0 comments on commit 9cf5b65

Please sign in to comment.