summaryrefslogtreecommitdiffstats
path: root/src/view
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2018-11-21 19:50:59 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2018-11-21 19:50:59 +0100
commit4443ca205812f4da4aa0ed1bf4897e731107616b (patch)
tree8331d53736ef74cbe9af5e88a8b5f0c06b1d43e8 /src/view
parent38e90fc2e309d394832eee471d4cb9877ae908e2 (diff)
downloadrpgedit-4443ca205812f4da4aa0ed1bf4897e731107616b.tar
rpgedit-4443ca205812f4da4aa0ed1bf4897e731107616b.zip
Move immutable parts of EntityContext into a new EntityView class
Diffstat (limited to 'src/view')
-rw-r--r--src/view/entity.ts87
1 files changed, 62 insertions, 25 deletions
diff --git a/src/view/entity.ts b/src/view/entity.ts
index 3b30e3c..ec91503 100644
--- a/src/view/entity.ts
+++ b/src/view/entity.ts
@@ -3,33 +3,70 @@ import { Renderer } from './renderer/renderer';
import { SpriteCoords, SpriteView, SpriteViewBuilder } from './sprite';
import { loadImage, mkTexture } from './util/image';
+import { getJSON } from '../util';
+
import { vec2 } from 'gl-matrix';
-export async function loadEntity(
- r: Renderer,
- 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 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());
+export class EntityView {
+ public static async load(r: Renderer, name: string): Promise<EntityView> {
+ const data = new EntityData(await getJSON(`resources/entity/${name}.json`));
+ 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 new EntityView(
+ data,
+ sprites,
+ );
+ }
+
+ private readonly totalTime: number;
+
+ private constructor(
+ public readonly data: EntityData,
+ public readonly sprites: SpriteView[],
+ ) {
+ if (data.animation)
+ this.totalTime = data.animation.sequence.reduce((a, s) => a + s[0], 0);
+ else
+ this.totalTime = 0;
+ }
+
+ public getSpriteByTime(time: number): SpriteView {
+ time %= this.totalTime;
+
+ if (this.data.animation) {
+ for (const [len, sprite] of this.data.animation.sequence) {
+ time -= len;
+ if (time < 0)
+ return this.sprites[sprite];
+ }
+ }
+
+ return this.sprites[0];
+ }
+
+ public renderByTime(time: number) {
+ this.getSpriteByTime(time).render();
}
- return sprites;
}