Skip to content

Commit

Permalink
Refactor query handling #154 (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
captkirk88 authored Jan 5, 2025
1 parent 199b75a commit 29b1130
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ func (q *Query) Iter(w World) iter.Seq[*Entry] {
for _, entity := range archetype.Entities() {
entry := w.Entry(entity)
if entry.entity.IsReady() {
archetype.Unlock()
if !yield(entry) {
archetype.Unlock()
return
}
archetype.Lock()
}
}
archetype.Unlock()
Expand Down
31 changes: 30 additions & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,35 @@ var (
orderableTest = donburi.NewComponentType[orderableComponentTest]()
)

func TestQueryInQuery(t *testing.T) {
world := donburi.NewWorld()
world.Create(queryTagA)
world.Create(queryTagC)
world.Create(queryTagA, queryTagB)
world.Create(queryTagA, queryTagB, queryTagC)

query := donburi.NewQuery(filter.Contains(queryTagA, queryTagB))
count := 0

for entry := range query.Iter(world) {
count++
if entry.Archetype().Layout().HasComponent(queryTagA) == false {
t.Errorf("PlayerTag should be in ent archetype")
}
innerQuery := donburi.NewQuery(filter.Contains(queryTagA, queryTagB, queryTagC))
defer func() {
if r := recover(); r != nil {
t.Errorf("panic should not happen")
}
}()
for innerEntry := range innerQuery.Iter(world) {
if innerEntry.Archetype().Layout().HasComponent(queryTagA) == false {
t.Errorf("PlayerTag should be in ent archetype")
}
}
}
}

func TestQuery(t *testing.T) {
world := donburi.NewWorld()
world.Create(queryTagA)
Expand Down Expand Up @@ -92,7 +121,7 @@ func BenchmarkQuery_EachOrdered(b *testing.B) {
countOrdered := 0
b.Run("Each", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _ = range query.Iter(world) {
for range query.Iter(world) {
countNormal++
}
}
Expand Down

0 comments on commit 29b1130

Please sign in to comment.