diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-01 18:56:58 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-01 18:56:58 -0700 |
commit | a8d07a790395e14fe7aedd3ba638db466f9c0842 (patch) | |
tree | 644f60a6bca79ceb55f24fcab7bdb3dee52c2d6e /src/engine/config | |
parent | aa08a8943a9a2d4a0e51893eebe6900bca7a7251 (diff) | |
download | the-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.tar.gz the-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.zip |
boundingbox + draw player
Diffstat (limited to 'src/engine/config')
-rw-r--r-- | src/engine/config/assets.ts | 42 | ||||
-rw-r--r-- | src/engine/config/constants.ts | 7 | ||||
-rw-r--r-- | src/engine/config/index.ts | 3 | ||||
-rw-r--r-- | src/engine/config/sprites.ts | 39 |
4 files changed, 91 insertions, 0 deletions
diff --git a/src/engine/config/assets.ts b/src/engine/config/assets.ts new file mode 100644 index 0000000..173bab3 --- /dev/null +++ b/src/engine/config/assets.ts @@ -0,0 +1,42 @@ +import type { SpriteSpec } from "./sprites"; +import { SPRITE_SPECS } from "./sprites"; + +export const IMAGES = new Map<string, HTMLImageElement>(); + +export const loadSpritesIntoImageElements = ( + spriteSpecs: Partial<SpriteSpec>[], +): Promise<void>[] => { + const spritePromises: Promise<void>[] = []; + + for (const spriteSpec of spriteSpecs) { + if (spriteSpec.sheet) { + const img = new Image(); + img.src = spriteSpec.sheet; + IMAGES.set(spriteSpec.sheet, img); + + spritePromises.push( + new Promise((resolve) => { + img.onload = () => resolve(); + }), + ); + } + + if (spriteSpec.states) { + spritePromises.push( + ...loadSpritesIntoImageElements(Array.from(spriteSpec.states.values())), + ); + } + } + + return spritePromises; +}; + +export const loadAssets = () => + Promise.all([ + ...loadSpritesIntoImageElements( + Array.from(SPRITE_SPECS.keys()).map( + (key) => SPRITE_SPECS.get(key) as SpriteSpec, + ), + ), + // TODO: Sound + ]); diff --git a/src/engine/config/constants.ts b/src/engine/config/constants.ts new file mode 100644 index 0000000..a00a141 --- /dev/null +++ b/src/engine/config/constants.ts @@ -0,0 +1,7 @@ +export namespace Miscellaneous { + export const WIDTH = 800; + export const HEIGHT = 800; + + export const DEFAULT_GRID_WIDTH = 30; + export const DEFAULT_GRID_HEIGHT = 30; +} diff --git a/src/engine/config/index.ts b/src/engine/config/index.ts new file mode 100644 index 0000000..a574965 --- /dev/null +++ b/src/engine/config/index.ts @@ -0,0 +1,3 @@ +export * from "./constants"; +export * from "./assets"; +export * from "./sprites"; diff --git a/src/engine/config/sprites.ts b/src/engine/config/sprites.ts new file mode 100644 index 0000000..37185fd --- /dev/null +++ b/src/engine/config/sprites.ts @@ -0,0 +1,39 @@ +import { Direction } from "../interfaces/Direction"; + +export enum Sprites { + PLAYER, +} + +export interface SpriteSpec { + sheet: string; + width: number; + height: number; + frames: number; + msPerFrame: number; + states?: Map<string | number, Partial<SpriteSpec>>; +} + +export const SPRITE_SPECS: Map<Sprites, Partial<SpriteSpec>> = new Map< + Sprites, + SpriteSpec +>(); + +const playerSpriteSpec = { + msPerFrame: 200, + width: 64, + height: 64, + frames: 3, + states: new Map<string, Partial<SpriteSpec>>(), +}; +playerSpriteSpec.states.set(Direction.NONE, { + sheet: "/assets/lambda/neutral.png", +}); +[Direction.LEFT, Direction.RIGHT, Direction.UP, Direction.DOWN].forEach( + (direction) => { + playerSpriteSpec.states.set(direction, { + sheet: `/assets/lambda/${direction.toLowerCase()}.png`, + }); + }, +); + +SPRITE_SPECS.set(Sprites.PLAYER, playerSpriteSpec); |