From 313039e6744ef4ed7a7f3f11b74d7af35773b1cf Mon Sep 17 00:00:00 2001 From: christopher-buss Date: Thu, 7 Nov 2024 00:45:59 +0000 Subject: [PATCH] feat!: add `destroy` method to replace current world --- eslint.config.ts | 2 +- package-lock.json | 1 - src/index.ts | 3 ++- src/query.ts | 6 +++--- src/registry.ts | 17 ++++++++++++++++- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/eslint.config.ts b/eslint.config.ts index ebdb661..5e00dac 100644 --- a/eslint.config.ts +++ b/eslint.config.ts @@ -19,6 +19,6 @@ export default style( }, }, { - ignores: ["src/jecs/**", GLOB_MARKDOWN], + ignores: [GLOB_MARKDOWN], }, ); diff --git a/package-lock.json b/package-lock.json index af3cb1e..7fa05e3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2213,7 +2213,6 @@ }, "node_modules/@clack/prompts/node_modules/is-unicode-supported": { "version": "1.3.0", - "dev": true, "inBundle": true, "license": "MIT", "engines": { diff --git a/src/index.ts b/src/index.ts index 61d8dd6..81815fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,12 +8,13 @@ export { changed, component, despawn, + destroy, get, + getRegistry, has, insert, pair, parent, - registry, remove, removed, reserve, diff --git a/src/query.ts b/src/query.ts index 3deb522..649a196 100644 --- a/src/query.ts +++ b/src/query.ts @@ -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 { @@ -72,7 +72,7 @@ function queryIter>( this: QueryHandle, ): IterableFunction> { if (this.terms) { - let ecsQuery = registry.query(...this.terms); + let ecsQuery = getRegistry().query(...this.terms); if (this.filterWithout) { ecsQuery = ecsQuery.without(...this.filterWithout); @@ -134,5 +134,5 @@ export function query = []>( * specified components. */ export function queryRuntimeIds>(...ids: T): ecs.Query> { - return registry.query(...ids); + return getRegistry().query(...ids); } diff --git a/src/registry.ts b/src/registry.ts index c0930ea..9879a01 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -28,7 +28,11 @@ export type FilterPairs = { }; const components = new Map(); -export const registry = new ecs.World(); + +let registry = new ecs.World(); +export function getRegistry(): ecs.World { + return registry; +} export const signals = { added: new Map>(), @@ -469,3 +473,14 @@ export function pair

(object: Entity, predicate?: ComponentKey

): Pair(ecs.Wildcard as Entity); reserve(ecs.ChildOf as Entity); + +/** + * 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(); +}