summaryrefslogtreecommitdiffstats
path: root/src/view/entity.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/view/entity.ts')
-rw-r--r--src/view/entity.ts27
1 files changed, 13 insertions, 14 deletions
diff --git a/src/view/entity.ts b/src/view/entity.ts
index 83cd955..3b30e3c 100644
--- a/src/view/entity.ts
+++ b/src/view/entity.ts
@@ -10,25 +10,24 @@ export async function loadEntity(
data: EntityData,
): Promise<SpriteView[]> {
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 sprites: SpriteView[] = [];
-
- for (let frame = 0; frame < data.frames; frame++) {
- const [texture, coords] = mkTexture(r, tile, frame, data.frames);
+ const offset = vec2.mul(vec2.create(), frameSize, data.anchor);
+ r.snapToGrid(offset, offset);
- const offset = vec2.fromValues(coords[2] - coords[0], coords[3] - coords[1]);
- vec2.mul(offset, offset, data.anchor);
- r.snapToGrid(offset, offset);
+ const coords: SpriteCoords = [
+ -offset[0],
+ -offset[1],
+ -offset[0] + frameSize[0],
+ -offset[1] + frameSize[1],
+ ];
- const anchorCoords: SpriteCoords = [
- coords[0] - offset[0],
- coords[1] - offset[1],
- coords[2] - offset[0],
- coords[3] - offset[1],
- ];
+ const sprites: SpriteView[] = [];
+ for (let frame = 0; frame < data.frames; frame++) {
const builder = new SpriteViewBuilder(r, texture);
- builder.addSprite(anchorCoords, [0, 0, 1, 1]);
+ builder.addSprite(coords, [0, frame / data.frames, 1, (frame + 1) / data.frames]);
sprites.push(builder.build());
}