summaryrefslogtreecommitdiffstats
path: root/src/controller
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/controller
parent38e90fc2e309d394832eee471d4cb9877ae908e2 (diff)
downloadrpgedit-4443ca205812f4da4aa0ed1bf4897e731107616b.tar
rpgedit-4443ca205812f4da4aa0ed1bf4897e731107616b.zip
Move immutable parts of EntityContext into a new EntityView class
Diffstat (limited to 'src/controller')
-rw-r--r--src/controller/entitycontext.ts39
1 files changed, 6 insertions, 33 deletions
diff --git a/src/controller/entitycontext.ts b/src/controller/entitycontext.ts
index 6932227..434bf9b 100644
--- a/src/controller/entitycontext.ts
+++ b/src/controller/entitycontext.ts
@@ -1,49 +1,36 @@
-import { EntityAnimation, EntityData } from '../model/data/entity';
-
-import { loadEntity } from '../view/entity';
+import { EntityView } from '../view/entity';
import { Renderer } from '../view/renderer/renderer';
-import { SpriteView } from '../view/sprite';
import { Collidable } from '../math/collision';
-import { getJSON } from '../util';
import { CollidableGroup, mkCollision } from './collision';
import { vec2 } from 'gl-matrix';
export class EntityContext implements CollidableGroup {
public static async load(renderer: Renderer, name: string): Promise<EntityContext> {
- const entity = new EntityData(await getJSON(`resources/entity/${name}.json`));
-
return new EntityContext(
renderer,
name,
- await loadEntity(renderer, entity),
- mkCollision(entity.collision),
- entity.animation,
+ await EntityView.load(renderer, name),
);
}
public readonly pos: vec2 = vec2.create();
- private readonly totalTime: number;
+ private readonly collision: Collidable[];
private constructor(
private readonly renderer: Renderer,
private readonly name: string,
- private readonly sprites: SpriteView[],
- private readonly collision: Collidable[],
- private readonly animation?: EntityAnimation,
+ private readonly view: EntityView,
) {
- if (animation)
- this.totalTime = animation.sequence.reduce((a, s) => a + s[0], 0);
- else
- this.totalTime = 0;
+ this.collision = mkCollision(view.data.collision);
}
public render(time: number) {
this.renderer.setTranslation(this.pos);
- this.getSprite(time).render();
+ this.view.renderByTime(time);
}
public getTranslation(): vec2 {
@@ -57,18 +44,4 @@ export class EntityContext implements CollidableGroup {
public interact() {
alert(`You've interacted with ${this.name}!`);
}
-
- private getSprite(time: number): SpriteView {
- time %= this.totalTime;
-
- if (this.animation) {
- for (const [len, sprite] of this.animation.sequence) {
- time -= len;
- if (time < 0)
- return this.sprites[sprite];
- }
- }
-
- return this.sprites[0];
- }
}