Skip to content

Commit

Permalink
fix: register unregistered components on creating a query
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-mason committed Feb 12, 2024
1 parent 76a10aa commit 42a87c8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/fresh-toes-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"arancini": patch
"@arancini/core": patch
---

fix: register unregistered components on creating a query
26 changes: 26 additions & 0 deletions packages/arancini-core/src/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from './entity-metadata'
import {
Query,
QueryCondition,
QueryDescription,
evaluateQueryBitSets,
getFirstQueryResult,
Expand Down Expand Up @@ -367,6 +368,8 @@ export class World<E extends AnyEntity = any> extends EntityContainer<E> {
const queryConditions = getQueryConditions(queryDescription)
const key = getQueryDedupeString(this.componentRegistry, queryConditions)

this.registerQueryDescriptionComponents(queryConditions)

const handle = options?.handle ?? DEFAULT_QUERY_HANDLE

const queryUsages = this.queryUsages.get(key)
Expand Down Expand Up @@ -437,6 +440,9 @@ export class World<E extends AnyEntity = any> extends EntityContainer<E> {
queryDescription: QueryDescription<E, ResultEntity>
): ResultEntity[] {
const conditions = getQueryConditions(queryDescription)

this.registerQueryDescriptionComponents(conditions)

const queryDedupe = getQueryDedupeString(this.componentRegistry, conditions)

const query = this.queries.get(queryDedupe)
Expand Down Expand Up @@ -464,6 +470,9 @@ export class World<E extends AnyEntity = any> extends EntityContainer<E> {
queryDescription: QueryDescription<E, ResultEntity>
): ResultEntity | undefined {
const conditions = getQueryConditions(queryDescription)

this.registerQueryDescriptionComponents(conditions)

const queryDedupe = getQueryDedupeString(this.componentRegistry, conditions)

const query = this.queries.get(queryDedupe)
Expand Down Expand Up @@ -507,6 +516,23 @@ export class World<E extends AnyEntity = any> extends EntityContainer<E> {
return ids
}

private registerQueryDescriptionComponents(queryConditions: QueryCondition<E>[]) {
const queryComponents = new Set(
queryConditions.flatMap((condition) => condition.components)
)

const unregisteredComponents: string[] = []
for (const component of queryComponents) {
if (this.componentRegistry[component as string] === undefined) {
unregisteredComponents.push(component as string)
}
}

if (unregisteredComponents.length > 0) {
this.registerComponents(unregisteredComponents)
}
}

private index(entity: E) {
if (!this.has(entity)) return

Expand Down
8 changes: 6 additions & 2 deletions packages/arancini-core/tst/world.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ describe('World', () => {
})

it('supports automatically registering components', () => {
const world = new World<Entity>({ components: ['foo'] })
const world = new World<Entity>()

const entity = { foo: 'test', bar: 1 }
const entity = { foo: 'test' }

world.create(entity)

const query = world.query((q) => q.has('bar'))

expect(query.entities.length).toBe(0)

world.add(entity, 'bar', 1)

expect(query.entities.length).toBe(1)
})

Expand Down

0 comments on commit 42a87c8

Please sign in to comment.