Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add destroy method to replace current world #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export default style(
},
},
{
ignores: ["src/jecs/**", GLOB_MARKDOWN],
ignores: [GLOB_MARKDOWN],
},
);
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ export {
changed,
component,
despawn,
destroy,
get,
getRegistry,
has,
insert,
pair,
parent,
registry,
remove,
removed,
reserve,
Expand Down
6 changes: 3 additions & 3 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Modding } from "@flamework/core";
import * as ecs from "@rbxts/jecs";

import type { Entity, FilterPairs, Id, ResolveKeys } from "./registry";
import { component, getId, registry } from "./registry";
import { component, getId, getRegistry } from "./registry";

// Almost full credits to @fireboltofdeath for all of these types.
export interface Without<T> {
Expand Down Expand Up @@ -72,7 +72,7 @@ function queryIter<T extends Array<unknown>>(
this: QueryHandle<T>,
): IterableFunction<LuaTuple<[Entity, ...T]>> {
if (this.terms) {
let ecsQuery = registry.query(...this.terms);
let ecsQuery = getRegistry().query(...this.terms);

if (this.filterWithout) {
ecsQuery = ecsQuery.without(...this.filterWithout);
Expand Down Expand Up @@ -134,5 +134,5 @@ export function query<T extends Array<unknown> = []>(
* specified components.
*/
export function queryRuntimeIds<T extends Array<Id>>(...ids: T): ecs.Query<ecs.InferComponents<T>> {
return registry.query(...ids);
return getRegistry().query(...ids);
}
17 changes: 16 additions & 1 deletion src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export type FilterPairs<T> = {
};

const components = new Map<string, Entity>();
export const registry = new ecs.World();

let registry = new ecs.World();
export function getRegistry(): ecs.World {
return registry;
}
Fireboltofdeath marked this conversation as resolved.
Show resolved Hide resolved

export const signals = {
added: new Map<Entity, Signal<[Entity]>>(),
Expand Down Expand Up @@ -469,3 +473,14 @@ export function pair<P>(object: Entity, predicate?: ComponentKey<P>): Pair<P, un

reserve<Wildcard>(ecs.Wildcard as Entity<Wildcard>);
reserve<ChildOf>(ecs.ChildOf as Entity<ChildOf>);

/**
* Destroys the current world and all associated entities, and resets the
* registry to a new world.
*
* @note This function should likely only be used for testing purposes.
*/
export function destroy(): void {
components.clear();
registry = new ecs.World();
}