Skip to content
Open
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
24 changes: 19 additions & 5 deletions packages/core/src/world/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import { universe } from '../universe/universe';
import { allocateWorldId, releaseWorldId } from './utils/world-index';

type Options = {
traits?: ConfigurableTrait[];
lazy?: boolean;
lazy: true;
};

export class World {
Expand Down Expand Up @@ -71,16 +70,19 @@ export class World {

constructor(polyArg?: Options | ConfigurableTrait, ...traits: ConfigurableTrait[]) {
if (polyArg && typeof polyArg === 'object' && !Array.isArray(polyArg)) {
const { traits: optionTraits = [], lazy = false } = polyArg as Options;
if (!lazy) this.init(...optionTraits);
if (traits.length > 0) {
throw new Error(
'createWorld({ lazy: true }) does not accept trailing traits. Pass traits to world.init(...traits).'
);
}
} else {
this.init(...(polyArg ? [polyArg, ...traits] : traits));
}
}

init(...traits: ConfigurableTrait[]) {
const ctx = this[$internal];
if (this.#isInitialized) return;
if (this.#isInitialized) return this;

this.#isInitialized = true;
universe.worlds[this.#id] = this;
Expand All @@ -102,6 +104,8 @@ export class World {

// Create world entity.
ctx.worldEntity = createEntity(this, IsExcluded, ...traits);

return this;
}

spawn(...traits: ConfigurableTrait[]): Entity {
Expand Down Expand Up @@ -133,6 +137,12 @@ export class World {
}

destroy() {
if (!this.#isInitialized) {
throw new Error(
'Cannot destroy a world that is not initialized. Call world.init() first.'
);
}

// Destroy world entity.
destroyEntity(this, this[$internal].worldEntity);
this[$internal].worldEntity = null!;
Expand All @@ -146,6 +156,10 @@ export class World {
}

reset() {
if (!this.#isInitialized) {
throw new Error('Cannot reset a world that is not initialized. Call world.init() first.');
}

const ctx = this[$internal];

// Destroy all entities so any cleanup is done.
Expand Down
25 changes: 24 additions & 1 deletion packages/core/tests/world.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
});

it('should optionaly init lazily', () => {
const Test = trait({ last: 0, delta: 0 });

const world = createWorld({ lazy: true });
expect(world.isInitialized).toBe(false);
world.init();

world.init(Test);

expect(world.isInitialized).toBe(true);
expect(world.get(Test)).toBeDefined();
});

it('should reset the world', () => {
Expand Down Expand Up @@ -65,6 +70,24 @@
expect(() => world.destroy()).not.toThrow();
});

it('should be able to init after destroy', () => {
const world = createWorld({ lazy: true });
expect(world.entities.length).toBe(0);

world.init();
expect(world.entities.length).toBe(1);

world.destroy();
expect(world.entities.length).toBe(0);

Check failure on line 81 in packages/core/tests/world.test.ts

View workflow job for this annotation

GitHub Actions / test

tests/world.test.ts > World > should be able to init after destroy

AssertionError: expected 1 to be +0 // Object.is equality - Expected + Received - 0 + 1 ❯ tests/world.test.ts:81:33

world.init();
expect(world.isInitialized).toBe(true);

// Can add entities
world.spawn();
expect(world.entities.length).toBe(2);
});

it('errors if more than 16 worlds are created', () => {
for (let i = 0; i < 16; i++) {
createWorld();
Expand Down
Loading