Skip to content

Commit 7f8ea81

Browse files
committed
chore: show fallback image when the character assets are not ready
1 parent 7dc7f59 commit 7f8ea81

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

model/character.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import { choice, randomInt } from "../util/random.ts"
66
import { bindToggleFullscreenOnce } from "../util/fullscreen.ts"
77
import * as signal from "../util/signal.ts"
88

9+
const fallbackImagePhase0 = await fetch(
10+
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAADdJREFUOE9jZMAE/9GEGNH4KPLokiC1Q9AAkpzMwMCA4m0QZxgYgJ4SSPLSaDqAJAqSAm3wJSQApTMgCUQZ7FoAAAAASUVORK5CYII=",
11+
).then((res) => res.blob()).then((blob) => createImageBitmap(blob))
12+
13+
const fallbackImagePhase1 = await fetch(
14+
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAD5JREFUOE9jZGBg+M+AChjR+HjlQYqHgQFoXibNS+gBBjKMpDAZHAaQ5GQGBgYUV4+mA7QAgaYokgJ14NMBAK1TIAlUJpxYAAAAAElFTkSuQmCC",
15+
).then((res) => res.blob()).then((blob) => createImageBitmap(blob))
16+
917
export type ILoader = {
1018
loadAssets(): Promise<void>
1119
get assetsReady(): boolean
@@ -256,20 +264,19 @@ export abstract class Character implements IChar {
256264
image(): ImageBitmap {
257265
if (this.#isMoving) {
258266
if (this.#movePhase < 8) {
259-
// idle state
260-
return this.#assets![`${this.#dir}0`]
267+
return this.getImage(this.#dir, 0)
261268
} else {
262269
// active state
263-
return this.#assets![`${this.#dir}1`]
270+
return this.getImage(this.#dir, 1)
264271
}
265272
} else {
266-
// idle stat
273+
// idle state
267274
if (this.#idleCounter % 128 < 64) {
268275
// idle state
269-
return this.#assets![`${this.#dir}0`]
276+
return this.getImage(this.#dir, 0)
270277
} else {
271278
// active state
272-
return this.#assets![`${this.#dir}1`]
279+
return this.getImage(this.#dir, 1)
273280
}
274281
}
275282
}
@@ -320,10 +327,7 @@ export abstract class Character implements IChar {
320327
return this.y + CELL_SIZE / 2
321328
}
322329

323-
/**
324-
* Loads the assets and store resulted HTMLImageElement in the fields.
325-
* Assets are managed like this way to make garbage collection easier.
326-
*/
330+
/** Loads the assets and store ImageBitmaps in #assets. */
327331
async loadAssets() {
328332
const [up0, up1, down0, down1, left0, left1, right0, right1] = await Promise
329333
.all([
@@ -348,6 +352,17 @@ export abstract class Character implements IChar {
348352
}
349353
}
350354

355+
getImage(dir: Dir, phase: 0 | 1): ImageBitmap {
356+
if (!this.#assets) {
357+
if (phase === 0) {
358+
return fallbackImagePhase0
359+
} else {
360+
return fallbackImagePhase1
361+
}
362+
}
363+
return this.#assets[`${dir}${phase}`]
364+
}
365+
351366
get assetsReady(): boolean {
352367
return !!this.#assets
353368
}

player/game-screen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ class Field implements IFieldTester {
428428
for (const char of spawns) {
429429
actors.add(char)
430430
}
431-
await actors.loadAssets()
431+
actors.loadAssets()
432432
}
433433
}
434434
}
@@ -537,7 +537,7 @@ export function GameScreen({ el, query }: Context) {
537537

538538
const loop = gameloop(() => {
539539
i++
540-
if (!actors.assetsReady || !field.assetsReady || !items.assetsReady) {
540+
if (!field.assetsReady || !items.assetsReady) {
541541
signal.isGameLoading.update(true)
542542
return
543543
}

0 commit comments

Comments
 (0)