@@ -6,6 +6,14 @@ import { choice, randomInt } from "../util/random.ts"
66import { bindToggleFullscreenOnce } from "../util/fullscreen.ts"
77import * 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+
917export 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 }
0 commit comments