Skip to content

Commit

Permalink
fix(core): add undefined to get resource signature
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaisthorpe committed Jun 15, 2024
1 parent f8aeeb5 commit 4421cf4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-eggs-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tedengine/ted": minor
---

Fix get resource return signature missing undefined
13 changes: 9 additions & 4 deletions packages/ted/src/actor-components/mesh-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class TMeshComponent extends TSceneComponent {
constructor(
protected engine: TEngine,
actor: TActor,
bodyOptions?: TPhysicsBodyOptions
bodyOptions?: TPhysicsBodyOptions,
) {
super(actor, bodyOptions);

Expand Down Expand Up @@ -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<TMesh>(path);
const mesh = engine.resources.get<TMesh>(path);

// @todo handle error if mesh is not found
if (mesh) {
this.mesh = mesh;
}
}

public async applyMaterial(engine: TEngine, path: string) {
Expand All @@ -63,7 +68,7 @@ export default class TMeshComponent extends TSceneComponent {
normals: number[],
indexes: number[],
colors: number[],
paletteIndex: TPaletteIndex
paletteIndex: TPaletteIndex,
) {
const mesh = new TMesh();

Expand All @@ -74,7 +79,7 @@ export default class TMeshComponent extends TSceneComponent {
normals,
indexes,
colors,
paletteIndex
paletteIndex,
);

this.mesh = mesh;
Expand Down
9 changes: 7 additions & 2 deletions packages/ted/src/actor-components/textured-mesh-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class TTexturedMeshComponent extends TSceneComponent {
positions: number[],
normals: number[],
indexes: number[],
uvs: number[]
uvs: number[],
) {
const mesh = new TTexturedMesh();

Expand All @@ -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<TTexture>(texturePath as string);
const texture = engine.resources.get<TTexture>(texturePath as string);

// @todo handle error if texture is not found
if (texture) {
this.texture = texture;
}
}

public setTexture(texture: TTexture) {
Expand Down
13 changes: 11 additions & 2 deletions packages/ted/src/actor-components/tilemap-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TImage>(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];

Expand All @@ -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<TTilemap>(tilemapPath as string);
const tilemap = engine.resources.get<TTilemap>(tilemapPath as string);

// @todo handle error if tilemap is not found
if (tilemap) {
this.tilemap = tilemap;
}
}
}

Expand Down
25 changes: 17 additions & 8 deletions packages/ted/src/core/resource-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,44 @@ export interface IJobAsset {
}

export default class TResourceManager {
private resources: { [key: string]: any } = {};
private resources = new Map<string, IAsset | IJobAsset>();

constructor(private jobs: TJobManager) { }
constructor(private jobs: TJobManager) {}

/**
* Checks if a resource is already loaded into the cache
*
* @param key resource key
*/
public isResourcedLoaded(key: string): boolean {
return this.resources[key] !== undefined;
return this.resources.has(key);
}

/**
* Returns an already loaded resource.
*
* Must be loaded, otherwise undefined will be returned.
*/
public get<T extends IAsset | IJobAsset>(key: string): T {
return this.resources[key] as T;
public get<T extends IAsset | IJobAsset>(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<T extends IAsset | IJobAsset>(
type: { new(): T },
type: { new (): T },
key: string,
): Promise<T> {
// 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);
Expand All @@ -49,7 +58,7 @@ export default class TResourceManager {
await resource.load(response);
}

this.resources[key] = resource;
this.resources.set(key, resource);
return resource;
}
}
Expand Down

0 comments on commit 4421cf4

Please sign in to comment.