Skip to content
Merged
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
29 changes: 26 additions & 3 deletions packages/ecs/src/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,27 +366,50 @@ export class World {
return this.getResourceByTypeId(typeid(resourceType))
}

/**
* @template T
* @param {Constructor<T>} resourceType
* @returns {T | undefined}
*/
getOptionalResource(resourceType) {
return this.getOptionalResourceByTypeId(typeid(resourceType))
}

/**
* @template T
* @param {TypeId} id
* @returns {T}
*/
getResourceByTypeId(id) {
const resource = this.getOptionalResourceByTypeId(id)

assert(resource, `The resource or resource alias \`${id}\` is non existent.`)

// SAFETY: The typeid should match the type, caller's responsibility
return /** @type {T}*/(resource)
}

/**
* @template T
* @param {TypeId} id
* @returns {T | undefined}
*/
getOptionalResourceByTypeId(id) {
const resource = this.resources.get(id)

if (resource) {
if (resource !== undefined) {

// SAFETY: The typeid should match the type, caller's responsibility
return /** @type {T}*/(resource)
}

const aliasedid = this.resourceAliases.get(id)

assert(aliasedid, `The resource or resource alias \`${id}\` is non existent.`)
if (aliasedid === undefined) return undefined

const aliasedResource = this.resources.get(aliasedid)

assert(aliasedResource, `The resource alias \`${id}\` points to a non-existent resource \`${aliasedid}\`.`)
if (aliasedResource === undefined) return undefined

// SAFETY: The aliased typeid should match the type, caller's responsibility
return /** @type {T}*/(aliasedResource)
Expand Down
26 changes: 25 additions & 1 deletion packages/ecs/tests/world.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, describe } from "vitest";
import { deepStrictEqual, throws } from "node:assert";
import { deepStrictEqual, strictEqual, throws } from "node:assert";
import { World } from "../src";
import { typeid } from "@wimaengine/type";
import { EntityHandle } from "../src/entities";
Expand Down Expand Up @@ -109,6 +109,30 @@ describe("Testing `World`", () => {
deepStrictEqual(resource, new TestResource())
})

test('Get optional correct resource on world.', () => {
const world = new World()
world.setResource(new TestResource())
const resource = world.getOptionalResource(TestResource)

deepStrictEqual(resource, new TestResource())
})

test('Optional resource lookup on world returns aliased resources.', () => {
const world = new World()
world.setResource(new TestResource())
world.setResourceAlias(typeid(TestResource), TestAlias)
const resource = world.getOptionalResource(TestAlias)

deepStrictEqual(resource, new TestResource())
})

test('Optional resource lookup on world returns undefined when missing.', () => {
const world = new World()

strictEqual(world.getOptionalResource(TestResource), undefined)
strictEqual(world.getOptionalResource(TestAlias), undefined)
})

test('World has resource.', () => {
const world = new World()
world.setResource(new TestResource())
Expand Down