import { EntityData } from '../model/data/entity'; import { Renderer } from './renderer/renderer'; import { SpriteCoords, SpriteView, SpriteViewBuilder } from './sprite'; import { loadImage, mkTexture } from './util/image'; import { vec2 } from 'gl-matrix'; export async function loadEntity( r: Renderer, data: EntityData, ): Promise { const tile = await loadImage(`resources/sprite/entity/${data.sprite}.png`); const [texture, size] = mkTexture(r, tile); const frameSize = [size[0], size[1] / data.frames]; const offset = vec2.mul(vec2.create(), frameSize, data.anchor); r.snapToGrid(offset, offset); const coords: SpriteCoords = [ -offset[0], -offset[1], -offset[0] + frameSize[0], -offset[1] + frameSize[1], ]; const sprites: SpriteView[] = []; for (let frame = 0; frame < data.frames; frame++) { const builder = new SpriteViewBuilder(r, texture); builder.addSprite(coords, [0, frame / data.frames, 1, (frame + 1) / data.frames]); sprites.push(builder.build()); } return sprites; }