Skip to content

Commit

Permalink
feat: add indexers to recs
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Jul 13, 2022
1 parent c54477c commit 589bfaf
Show file tree
Hide file tree
Showing 5 changed files with 367 additions and 3 deletions.
11 changes: 9 additions & 2 deletions packages/recs/src/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
Component,
ComponentValue,
EntityIndex,
Indexer,
Metadata,
OverridableComponent,
Override,
Schema,
World,
} from "./types";
import { isFullComponentValue, isIndexer } from "./utils";

export function defineComponent<S extends Schema, M extends Metadata>(
world: World,
Expand Down Expand Up @@ -104,11 +106,16 @@ export function withValue<S extends Schema>(
return [component, value];
}

// TODO: Trivial implementation, needs to be more efficient for performant HasValue queries
export function getEntitiesWithValue<T extends Schema>(
component: Component<T>,
component: Component<T> | Indexer<T>,
value: Partial<ComponentValue<T>>
): Set<EntityIndex> {
// Shortcut for indexers
if (isIndexer(component) && isFullComponentValue(component, value)) {
return component.getEntitiesWithValue(value);
}

// Trivial implementation for regular components
const entities = new Set<EntityIndex>();
for (const entity of getComponentEntities(component)) {
const val = getComponentValue(component, entity);
Expand Down
54 changes: 54 additions & 0 deletions packages/recs/src/Indexer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { getComponentEntities, getComponentValue } from "./Component";
import { Component, ComponentValue, EntityIndex, Indexer, Schema, World } from "./types";

// Simple implementation of a "reverse mapping" indexer.
// Could be made more memory efficient by using a hash of the value as key.
export function createIndexer<S extends Schema>(world: World, component: Component<S>): Indexer<S> {
const valueToEntities = new Map<string, Set<EntityIndex>>();

function getEntitiesWithValue(value: ComponentValue<S>) {
return valueToEntities.get(getValueKey(value)) ?? new Set<EntityIndex>();
}

function getValueKey(value: ComponentValue<S>): string {
return JSON.stringify(value);
}

function add(entity: EntityIndex, value: ComponentValue<S> | undefined) {
if (!value) return;
const valueKey = getValueKey(value);
let entitiesWithValue = valueToEntities.get(valueKey);
if (!entitiesWithValue) {
entitiesWithValue = new Set<EntityIndex>();
valueToEntities.set(valueKey, entitiesWithValue);
}
entitiesWithValue.add(entity);
}

function remove(entity: EntityIndex, value: ComponentValue<S> | undefined) {
if (!value) return;
const valueKey = getValueKey(value);
const entitiesWithValue = valueToEntities.get(valueKey);
if (!entitiesWithValue) return;
entitiesWithValue.delete(entity);
}

// Initial indexing
for (const entity of getComponentEntities(component)) {
const value = getComponentValue(component, entity);
add(entity, value);
}

// Keeping index up to date
const subscription = component.update$.subscribe(({ entity, value }) => {
// Remove from previous location
remove(entity, value[1]);

// Add to new location
add(entity, value[0]);
});

world.registerDisposer(() => subscription?.unsubscribe());

return { ...component, id: `index/${component.id}`, getEntitiesWithValue };
}
4 changes: 4 additions & 0 deletions packages/recs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export interface Component<S extends Schema = Schema, M extends Metadata = Metad
update$: Subject<ComponentUpdate<S>> & { observers: any };
}

export type Indexer<S extends Schema> = Component<S> & {
getEntitiesWithValue: (value: ComponentValue<S>) => Set<EntityIndex>;
};

export type Components = {
[key: string]: Component<Schema>;
};
Expand Down
13 changes: 12 additions & 1 deletion packages/recs/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { map, pipe } from "rxjs";
import { getComponentValue } from "./Component";
import { UpdateType } from "./constants";
import { Component, ComponentUpdate, EntityIndex, Schema } from "./types";
import { Component, ComponentUpdate, ComponentValue, EntityIndex, Indexer, Schema } from "./types";

export function isComponentUpdate<S extends Schema>(
update: ComponentUpdate,
Expand All @@ -25,3 +25,14 @@ export function toUpdate<S extends Schema>(entity: EntityIndex, component: Compo
export function toUpdateStream<S extends Schema>(component: Component<S>) {
return pipe(map((entity: EntityIndex) => toUpdate(entity, component)));
}

export function isIndexer<S extends Schema>(c: Component<S> | Indexer<S>): c is Indexer<S> {
return "getEntitiesWithValue" in c;
}

export function isFullComponentValue<S extends Schema>(
component: Component<S>,
value: Partial<ComponentValue<S>>
): value is ComponentValue<S> {
return Object.keys(component.schema).every((key) => key in value);
}
Loading

0 comments on commit 589bfaf

Please sign in to comment.