diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-11 00:56:22 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-11 01:46:08 +0100 |
commit | b83b596b0f79fa1d5b95c462d3fa7171ff221a19 (patch) | |
tree | 0601d178aff79a47cade680fc2a193cd67108aca /src/view/entity.ts | |
parent | 426b2c37ff72d8de40f716365c8301449acfdfcb (diff) | |
download | rpgedit-b83b596b0f79fa1d5b95c462d3fa7171ff221a19.tar rpgedit-b83b596b0f79fa1d5b95c462d3fa7171ff221a19.zip |
Add support for simple periodic sprite animations
Diffstat (limited to 'src/view/entity.ts')
-rw-r--r-- | src/view/entity.ts | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/src/view/entity.ts b/src/view/entity.ts index b59bf92..7600826 100644 --- a/src/view/entity.ts +++ b/src/view/entity.ts @@ -8,21 +8,28 @@ import { vec2 } from 'gl-matrix'; export async function loadEntity( r: Renderer, data: EntityData, -): Promise<SpriteView> { +): Promise<SpriteView[]> { const tile = await loadImage(`resources/sprite/entity/${data.sprite}.png`); - const [texture, size, coords] = mkTexture(r, tile); - const offset = vec2.mul(vec2.create(), data.anchor, size); - r.snapToGrid(offset, offset); + const sprites: SpriteView[] = []; - const anchorCoords: SpriteCoords = [ - coords[0] - offset[0], - coords[1] - offset[1], - coords[2] - offset[0], - coords[3] - offset[1], - ]; + for (let frame = 0; frame < data.frames; frame++) { + const [texture, size, coords] = mkTexture(r, tile, frame, data.frames); - const builder = new SpriteViewBuilder(r, texture); - builder.addSprite(anchorCoords, [0, 0, 1, 1]); - return builder.build(); + const offset = vec2.mul(vec2.create(), data.anchor, size); + r.snapToGrid(offset, offset); + + const anchorCoords: SpriteCoords = [ + coords[0] - offset[0], + coords[1] - offset[1], + coords[2] - offset[0], + coords[3] - offset[1], + ]; + + const builder = new SpriteViewBuilder(r, texture); + builder.addSprite(anchorCoords, [0, 0, 1, 1]); + sprites.push(builder.build()); + } + + return sprites; } |