diff --git a/.changeset/itchy-eggs-explain.md b/.changeset/itchy-eggs-explain.md new file mode 100644 index 0000000..70c1f10 --- /dev/null +++ b/.changeset/itchy-eggs-explain.md @@ -0,0 +1,5 @@ +--- +"@tedengine/ted": minor +--- + +Fix get resource return signature missing undefined diff --git a/packages/ted/src/actor-components/mesh-component.ts b/packages/ted/src/actor-components/mesh-component.ts index f2c53d5..a3ddc3a 100644 --- a/packages/ted/src/actor-components/mesh-component.ts +++ b/packages/ted/src/actor-components/mesh-component.ts @@ -15,7 +15,7 @@ export default class TMeshComponent extends TSceneComponent { constructor( protected engine: TEngine, actor: TActor, - bodyOptions?: TPhysicsBodyOptions + bodyOptions?: TPhysicsBodyOptions, ) { super(actor, bodyOptions); @@ -50,7 +50,12 @@ export default class TMeshComponent extends TSceneComponent { * @param {TMaterials | string } materials or path */ public async applyMesh(engine: TEngine, path: string) { - this.mesh = engine.resources.get(path); + const mesh = engine.resources.get(path); + + // @todo handle error if mesh is not found + if (mesh) { + this.mesh = mesh; + } } public async applyMaterial(engine: TEngine, path: string) { @@ -63,7 +68,7 @@ export default class TMeshComponent extends TSceneComponent { normals: number[], indexes: number[], colors: number[], - paletteIndex: TPaletteIndex + paletteIndex: TPaletteIndex, ) { const mesh = new TMesh(); @@ -74,7 +79,7 @@ export default class TMeshComponent extends TSceneComponent { normals, indexes, colors, - paletteIndex + paletteIndex, ); this.mesh = mesh; diff --git a/packages/ted/src/actor-components/textured-mesh-component.ts b/packages/ted/src/actor-components/textured-mesh-component.ts index e03725d..e4de409 100644 --- a/packages/ted/src/actor-components/textured-mesh-component.ts +++ b/packages/ted/src/actor-components/textured-mesh-component.ts @@ -48,7 +48,7 @@ export default class TTexturedMeshComponent extends TSceneComponent { positions: number[], normals: number[], indexes: number[], - uvs: number[] + uvs: number[], ) { const mesh = new TTexturedMesh(); @@ -65,7 +65,12 @@ export default class TTexturedMeshComponent extends TSceneComponent { * @param {string} texture path */ public async applyTexture(engine: TEngine, texturePath: string | TTexture) { - this.texture = engine.resources.get(texturePath as string); + const texture = engine.resources.get(texturePath as string); + + // @todo handle error if texture is not found + if (texture) { + this.texture = texture; + } } public setTexture(texture: TTexture) { diff --git a/packages/ted/src/actor-components/tilemap-component.ts b/packages/ted/src/actor-components/tilemap-component.ts index 109d2f0..bafdd19 100644 --- a/packages/ted/src/actor-components/tilemap-component.ts +++ b/packages/ted/src/actor-components/tilemap-component.ts @@ -53,13 +53,17 @@ export default class TTilemapComponent extends TSpriteComponent { * @param {string} tileset path */ private getTileset(engine: TEngine, tileset: TTilesetConfig): TTileset { - let image: TImage; + let image: TImage | undefined; if (tileset.image instanceof TImage) { image = tileset.image as TImage; } else { image = engine.resources.get(tileset.image as string); } + if (!image) { + throw new Error(`Tileset image not found: ${tileset.image}`); + } + // Get the tileset from the tilemap defs const tilesetDef = this.tilemap.tilesetDefs[tileset.id]; @@ -80,7 +84,12 @@ export default class TTilemapComponent extends TSpriteComponent { if (tilemapPath instanceof TTilemap) { this.tilemap = tilemapPath as TTilemap; } else { - this.tilemap = engine.resources.get(tilemapPath as string); + const tilemap = engine.resources.get(tilemapPath as string); + + // @todo handle error if tilemap is not found + if (tilemap) { + this.tilemap = tilemap; + } } } diff --git a/packages/ted/src/core/resource-manager.ts b/packages/ted/src/core/resource-manager.ts index 5d056e2..6dfd031 100644 --- a/packages/ted/src/core/resource-manager.ts +++ b/packages/ted/src/core/resource-manager.ts @@ -9,9 +9,9 @@ export interface IJobAsset { } export default class TResourceManager { - private resources: { [key: string]: any } = {}; + private resources = new Map(); - constructor(private jobs: TJobManager) { } + constructor(private jobs: TJobManager) {} /** * Checks if a resource is already loaded into the cache @@ -19,7 +19,7 @@ export default class TResourceManager { * @param key resource key */ public isResourcedLoaded(key: string): boolean { - return this.resources[key] !== undefined; + return this.resources.has(key); } /** @@ -27,17 +27,26 @@ export default class TResourceManager { * * Must be loaded, otherwise undefined will be returned. */ - public get(key: string): T { - return this.resources[key] as T; + public get(key: string): T | undefined { + return this.resources.get(key) as T; } + /** + * Loads a resource of type T with the specified key. + * If the resource is already loaded, it returns the cached resource. + * Otherwise, it fetches the resource, caches the loaded resource, and returns it. + * + * @param type The constructor function of the resource type T. + * @param key The key or URL of the resource to load. + * @returns A promise that resolves to the loaded resource of type T. + */ public async load( - type: { new(): T }, + type: { new (): T }, key: string, ): Promise { // If already loaded, then just resolve if (this.isResourcedLoaded(key)) { - return this.resources[key]; + return this.resources.get(key) as T; } const response = await fetch(key); @@ -49,7 +58,7 @@ export default class TResourceManager { await resource.load(response); } - this.resources[key] = resource; + this.resources.set(key, resource); return resource; } }