Skip to content

Commit

Permalink
feat: refactor logic for creation of queries with unregistered compon…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
isaac-mason committed Feb 12, 2024
1 parent f13bd6c commit d252eee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/flat-coins-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@arancini/core": patch
"arancini": patch
---

feat: refactor logic for creation of queries with unregistered components
24 changes: 13 additions & 11 deletions packages/arancini-core/src/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ export class World<E extends AnyEntity = any> extends EntityContainer<E> {
queryDescription: QueryDescription<E, ResultEntity>,
options?: { handle: unknown }
): Query<ResultEntity> {
const queryConditions = getQueryConditions(queryDescription)
const conditions = getQueryConditions(queryDescription)

this.registerQueryDescriptionComponents(queryConditions)
this.registerQueryConditionComponents(conditions)

const key = getQueryDedupeString(this.componentRegistry, queryConditions)
const key = getQueryDedupeString(this.componentRegistry, conditions)

const handle = options?.handle ?? DEFAULT_QUERY_HANDLE

Expand All @@ -390,8 +390,8 @@ export class World<E extends AnyEntity = any> extends EntityContainer<E> {
query = new Query(
this,
key,
queryConditions,
getQueryBitSets(this.componentRegistry, queryConditions)
conditions,
getQueryBitSets(this.componentRegistry, conditions)
)

const matches = getQueryResults(query.bitSets, this.entities.values())
Expand Down Expand Up @@ -442,7 +442,7 @@ export class World<E extends AnyEntity = any> extends EntityContainer<E> {
): ResultEntity[] {
const conditions = getQueryConditions(queryDescription)

this.registerQueryDescriptionComponents(conditions)
this.registerQueryConditionComponents(conditions)

const queryDedupe = getQueryDedupeString(this.componentRegistry, conditions)

Expand Down Expand Up @@ -472,7 +472,7 @@ export class World<E extends AnyEntity = any> extends EntityContainer<E> {
): ResultEntity | undefined {
const conditions = getQueryConditions(queryDescription)

this.registerQueryDescriptionComponents(conditions)
this.registerQueryConditionComponents(conditions)

const queryDedupe = getQueryDedupeString(this.componentRegistry, conditions)

Expand Down Expand Up @@ -517,15 +517,17 @@ export class World<E extends AnyEntity = any> extends EntityContainer<E> {
return ids
}

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

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

Expand Down

0 comments on commit d252eee

Please sign in to comment.