Skip to content

1681-engine / index / AssetLoader

Class: AssetLoader ​

Defined in: src/assets/AssetLoader.ts:133

Loads a scene's assets before it starts: queue them, then await load(). Images become textures in the stage's registry (frames of a sprite sheet named 0, 1, 2…); JSON, text and Tiled maps are kept here by key; atlases register every frame under their key; fonts go to document.fonts; audio is decoded into AudioBuffers (or kept as bytes until a context exists). A failed asset fails the whole load with its URL in the message.

ts
override async onEnter() {
  const assets = new AssetLoader(this.engine.stage.textures);
  assets.spritesheet('hero', 'art/hero.png', { frameWidth: 20, frameHeight: 24 }).tilemap('level', 'maps/level.tmj');
  await assets.load(({ done, total }) => bar.set(done / total));
  const map = assets.map('level');
}

Constructors ​

Constructor ​

ts
new AssetLoader(textures, options?): AssetLoader;

Defined in: src/assets/AssetLoader.ts:148

Parameters ​

ParameterType
texturesTextureSink
optionsAssetLoaderOptions

Returns ​

AssetLoader

Methods ​

atlas() ​

ts
atlas(key, url): this;

Defined in: src/assets/AssetLoader.ts:222

A TexturePacker (or Pixi) JSON atlas, hash or array format: its page image, and for a multipack atlas every page named in meta.related_multi_packs (each resolved beside the file naming it). The first page is the texture key, further pages key~1, key~2…, and every frame of every page is found as textures.get(key, frameName). Counts as one asset in the progress.

Parameters ​

ParameterType
keystring
urlstring

Returns ​

this


audio() ​

ts
audio(key, url): this;

Defined in: src/assets/AssetLoader.ts:263

A sound file decoded into an AudioBuffer through the engine's audio context (getAudio). Before any context exists (browsers open one only after a gesture) the bytes are kept and decoded by decodeAudio, so a loading screen never has to wait for the player to click.

Parameters ​

ParameterType
keystring
urlstring

Returns ​

this


decodeAudio() ​

ts
decodeAudio(key, ctx?): Promise<AudioBuffer>;

Defined in: src/assets/AssetLoader.ts:283

A loaded sound, decoding its kept bytes through ctx (default: the loader's context) the first time.

Parameters ​

ParameterType
keystring
ctx?BaseAudioContext

Returns ​

Promise<AudioBuffer>


font() ​

ts
font(
   family, 
   url, 
   descriptors?
): this;

Defined in: src/assets/AssetLoader.ts:245

A web font (woff2, woff, ttf) under family, added to document.fonts once loaded so canvas text and the DOM can use it at once — load fonts before the first Text is drawn, or it measures with the fallback.

Parameters ​

ParameterType
familystring
urlstring
descriptors?FontFaceDescriptors

Returns ​

this


getAtlas() ​

ts
getAtlas(key): AtlasInfo;

Defined in: src/assets/AssetLoader.ts:309

What a loaded atlas holds: its frame names and animations.

Parameters ​

ParameterType
keystring

Returns ​

AtlasInfo


getAudio() ​

ts
getAudio(key): AudioBuffer | null;

Defined in: src/assets/AssetLoader.ts:275

A loaded sound, decoded; null while it waits for an audio context (decodeAudio).

Parameters ​

ParameterType
keystring

Returns ​

AudioBuffer | null


getJson() ​

ts
getJson<T>(key): T;

Defined in: src/assets/AssetLoader.ts:330

A loaded JSON value.

Type Parameters ​

Type ParameterDefault type
Tunknown

Parameters ​

ParameterType
keystring

Returns ​

T


getText() ​

ts
getText(key): string;

Defined in: src/assets/AssetLoader.ts:336

A loaded text file.

Parameters ​

ParameterType
keystring

Returns ​

string


image() ​

ts
image(key, url): this;

Defined in: src/assets/AssetLoader.ts:156

Parameters ​

ParameterType
keystring
urlstring

Returns ​

this


json() ​

ts
json(key, url): this;

Defined in: src/assets/AssetLoader.ts:169

Parameters ​

ParameterType
keystring
urlstring

Returns ​

this


ldtk() ​

ts
ldtk(
   key, 
   url, 
   options?
): this;

Defined in: src/assets/AssetLoader.ts:196

An LDtk project: the .ldtk file, the .ldtkl file of each level it saves apart (resolved beside the project), then every tileset image (resolved beside the project, as LDtk writes relPath). Counts as one asset, plus one per tileset image. Read it back with ldtkProject(key); each level's map goes to TilemapView as it is.

Parameters ​

ParameterType
keystring
urlstring
optionsLdtkAssetOptions

Returns ​

this


ldtkProject() ​

ts
ldtkProject(key): LdtkProject;

Defined in: src/assets/AssetLoader.ts:210

A loaded LDtk project.

Parameters ​

ParameterType
keystring

Returns ​

LdtkProject


load() ​

ts
load(onProgress?): Promise<void>;

Defined in: src/assets/AssetLoader.ts:316

Runs everything queued; onProgress after each asset. Safe to call again after queueing more.

Parameters ​

ParameterType
onProgress?(p) => void

Returns ​

Promise<void>


map() ​

ts
map(key): TileMap;

Defined in: src/assets/AssetLoader.ts:341

A loaded Tiled map.

Parameters ​

ParameterType
keystring

Returns ​

TileMap


spritesheet() ​

ts
spritesheet(
   key, 
   url, 
   size
): this;

Defined in: src/assets/AssetLoader.ts:163

An image cut into equal frames named 0, 1, 2…, left to right, then row by row.

Parameters ​

ParameterType
keystring
urlstring
size{ frameHeight: number; frameWidth: number; }
size.frameHeightnumber
size.frameWidthnumber

Returns ​

this


text() ​

ts
text(key, url): this;

Defined in: src/assets/AssetLoader.ts:175

Parameters ​

ParameterType
keystring
urlstring

Returns ​

this


tilemap() ​

ts
tilemap(
   key, 
   url, 
   options?
): this;

Defined in: src/assets/AssetLoader.ts:182

A Tiled JSON map, then every tileset image it names (resolved beside the map file).

Parameters ​

ParameterType
keystring
urlstring
optionsTilemapAssetOptions

Returns ​

this

Released under the MIT License.