summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2018-11-17 12:38:14 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2018-11-17 12:38:14 +0100
commit0db19bd11958cbead28cc5aad94d80a277f80290 (patch)
tree66359ab8d31b6c5ef6d606ee2904bd16560ff6dc /src
parente52d57b39666cac088b51a78367537f41ed3d50f (diff)
downloadrpgedit-0db19bd11958cbead28cc5aad94d80a277f80290.tar
rpgedit-0db19bd11958cbead28cc5aad94d80a277f80290.zip
Add MapView class with common render() method
Diffstat (limited to 'src')
-rw-r--r--src/controller/gamecontext.ts14
-rw-r--r--src/view/map.ts21
2 files changed, 21 insertions, 14 deletions
diff --git a/src/controller/gamecontext.ts b/src/controller/gamecontext.ts
index 1595b41..4fcc883 100644
--- a/src/controller/gamecontext.ts
+++ b/src/controller/gamecontext.ts
@@ -4,9 +4,8 @@ import { EntityContext } from './entitycontext';
import { MapData } from '../model/data/map';
import { ButtonCode, GameInputHandler } from '../view/input/gameinput';
-import { loadMap } from '../view/map';
+import { MapView } from '../view/map';
import { Renderer } from '../view/renderer/renderer';
-import { SpriteView } from '../view/sprite';
import { Collidable } from '../math/collision';
import { Movement } from '../math/line';
@@ -39,9 +38,9 @@ export class GameContext implements CollidableGroup {
);
}
- private static async loadMap(renderer: Renderer, name: string): Promise<[SpriteView[], Collidable[]]> {
+ private static async loadMap(renderer: Renderer, name: string): Promise<[MapView, Collidable[]]> {
const map = new MapData(await getJSON(`resources/map/${name}.json`));
- return [await loadMap(renderer, map), mkCollision(map.collision)];
+ return [await MapView.load(renderer, map), mkCollision(map.collision)];
}
private readonly initTime: number = performance.now();
@@ -61,7 +60,7 @@ export class GameContext implements CollidableGroup {
private constructor(
private readonly renderer: Renderer,
- private readonly mapView: SpriteView[],
+ private readonly mapView: MapView,
private readonly player: EntityContext,
private readonly entities: EntityContext[],
private readonly collision: Collidable[],
@@ -168,10 +167,7 @@ export class GameContext implements CollidableGroup {
this.renderer.setCenter(this.player.pos);
this.renderer.clear();
- for (const layer of this.mapView)
- layer.render();
-
- for (const r of [...this.entities, this.player])
+ for (const r of [this.mapView, ...this.entities, this.player])
r.render(time);
window.requestAnimationFrame(this.render);
diff --git a/src/view/map.ts b/src/view/map.ts
index f5d6eba..4e7fb20 100644
--- a/src/view/map.ts
+++ b/src/view/map.ts
@@ -2,7 +2,7 @@ import { Renderer } from './renderer/renderer';
import { SpriteCoords, SpriteView, SpriteViewBuilder } from './sprite';
import { loadImage, mkTexture } from './util/image';
-import { MapData, MapLayer } from '../model/data/map';
+import { MapData } from '../model/data/map';
import { nextPowerOf2 } from '../util';
@@ -68,9 +68,20 @@ function buildMapLayer(r: Renderer, tileset: Tileset, layer: number[][]): Sprite
return builder.build();
}
-export async function loadMap(r: Renderer, map: MapData): Promise<SpriteView[]> {
- const tiles = await loadTiles(map.tiles);
- const tileset = mkTileset(r, tiles);
+export class MapView {
+ public static async load(r: Renderer, map: MapData): Promise<MapView> {
+ const tiles = await loadTiles(map.tiles);
+ const tileset = mkTileset(r, tiles);
- return map.layers.map((layer) => buildMapLayer(r, tileset, layer.tiles));
+ const layers = map.layers.map((layer) => buildMapLayer(r, tileset, layer.tiles));
+ return new MapView(layers);
+ }
+
+ private constructor(private layers: SpriteView[]) {
+ }
+
+ public render(time: number) {
+ for (const layer of this.layers)
+ layer.render();
+ }
}