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.
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 ​
new AssetLoader(textures, options?): AssetLoader;Defined in: src/assets/AssetLoader.ts:148
Parameters ​
| Parameter | Type |
|---|---|
textures | TextureSink |
options | AssetLoaderOptions |
Returns ​
AssetLoader
Methods ​
atlas() ​
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 ​
| Parameter | Type |
|---|---|
key | string |
url | string |
Returns ​
this
audio() ​
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 ​
| Parameter | Type |
|---|---|
key | string |
url | string |
Returns ​
this
decodeAudio() ​
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 ​
| Parameter | Type |
|---|---|
key | string |
ctx? | BaseAudioContext |
Returns ​
Promise<AudioBuffer>
font() ​
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 ​
| Parameter | Type |
|---|---|
family | string |
url | string |
descriptors? | FontFaceDescriptors |
Returns ​
this
getAtlas() ​
getAtlas(key): AtlasInfo;Defined in: src/assets/AssetLoader.ts:309
What a loaded atlas holds: its frame names and animations.
Parameters ​
| Parameter | Type |
|---|---|
key | string |
Returns ​
getAudio() ​
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 ​
| Parameter | Type |
|---|---|
key | string |
Returns ​
AudioBuffer | null
getJson() ​
getJson<T>(key): T;Defined in: src/assets/AssetLoader.ts:330
A loaded JSON value.
Type Parameters ​
| Type Parameter | Default type |
|---|---|
T | unknown |
Parameters ​
| Parameter | Type |
|---|---|
key | string |
Returns ​
T
getText() ​
getText(key): string;Defined in: src/assets/AssetLoader.ts:336
A loaded text file.
Parameters ​
| Parameter | Type |
|---|---|
key | string |
Returns ​
string
image() ​
image(key, url): this;Defined in: src/assets/AssetLoader.ts:156
Parameters ​
| Parameter | Type |
|---|---|
key | string |
url | string |
Returns ​
this
json() ​
json(key, url): this;Defined in: src/assets/AssetLoader.ts:169
Parameters ​
| Parameter | Type |
|---|---|
key | string |
url | string |
Returns ​
this
ldtk() ​
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 ​
| Parameter | Type |
|---|---|
key | string |
url | string |
options | LdtkAssetOptions |
Returns ​
this
ldtkProject() ​
ldtkProject(key): LdtkProject;Defined in: src/assets/AssetLoader.ts:210
A loaded LDtk project.
Parameters ​
| Parameter | Type |
|---|---|
key | string |
Returns ​
load() ​
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 ​
| Parameter | Type |
|---|---|
onProgress? | (p) => void |
Returns ​
Promise<void>
map() ​
map(key): TileMap;Defined in: src/assets/AssetLoader.ts:341
A loaded Tiled map.
Parameters ​
| Parameter | Type |
|---|---|
key | string |
Returns ​
spritesheet() ​
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 ​
| Parameter | Type |
|---|---|
key | string |
url | string |
size | { frameHeight: number; frameWidth: number; } |
size.frameHeight | number |
size.frameWidth | number |
Returns ​
this
text() ​
text(key, url): this;Defined in: src/assets/AssetLoader.ts:175
Parameters ​
| Parameter | Type |
|---|---|
key | string |
url | string |
Returns ​
this
tilemap() ​
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 ​
| Parameter | Type |
|---|---|
key | string |
url | string |
options | TilemapAssetOptions |
Returns ​
this