summaryrefslogtreecommitdiffstats
path: root/src/view/MapView.ts
blob: b366d287a9d2591900e7ba4c494ccbe201fdfe20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';


import * as util from '../util';
import EntityPosition from '../model/EntityPosition';
import MapData from '../model/MapData';


const tileSize = 32;

const body = document.getElementsByTagName('body')[0];


function loadImage(url: string): Promise<HTMLImageElement> {
        return new Promise(function(resolve, reject) {
                var img = new Image();
                img.addEventListener('load', () => { resolve(img); });
                img.addEventListener('error', () => { reject(Error('Failed to load ' + url)); });
                img.src = url;
        });
}

function loadImages(imgs: {[key: string]: string}): Promise<{[key: string]: HTMLImageElement}> {
        return util.mapPromises(_.mapValues(imgs, loadImage));
}

function loadTiles(tiles: {[key: string]: {file: string}}): Promise<{[key: string]: HTMLImageElement}> {
        return loadImages(_.mapValues(tiles, (t) => `resources/sprite/tile/${t.file}.png`));
}

function loadEntities(entities: EntityPosition[]): Promise<{[key: string]: HTMLImageElement}> {
        var p: {[key: string]: Promise<HTMLImageElement>} = {};

        entities.forEach(e => {
                p[e.entity.name] = loadImage(`resources/sprite/entity/${e.entity.name}.png`);
        });

        return util.mapPromises(p);
}


export default class MapView {
        redrawPending: boolean = false;

        canvas: HTMLCanvasElement;
        ctx: CanvasRenderingContext2D;

        tiles: {[key: string]: HTMLImageElement};
        entitySprites: {[key: string]: HTMLImageElement};

        constructor(private map: MapData, private entities: {[key: string]: EntityPosition}) {
                this.canvas = document.createElement('canvas');
                this.canvas.style.position = 'absolute';
                body.appendChild(this.canvas);

                this.ctx = this.canvas.getContext('2d');

                window.addEventListener('resize', () => this.setSize());
                this.setSize();

                var tilesReady = loadTiles(map.tiles).then((tiles) => {
                        this.tiles = tiles;
                });

                var entitiesReady = loadEntities(this.getEntities()).then((entities) => {
                        this.entitySprites = entities;
                });

                Promise.all([tilesReady, entitiesReady]).then(() => {
                        this.redraw();
                });
        }

        getEntities(): EntityPosition[] {
                return _.valuesIn<EntityPosition>(this.entities);
        }

        setSize() {
                var e = document.documentElement;
                this.canvas.width = window.innerWidth || e.clientWidth || body.clientWidth;
                this.canvas.height = window.innerHeight || e.clientHeight || body.clientHeight;

                this.redraw()
        }

        drawTile(x: number, y: number, tile: HTMLImageElement) {
                if (!tile)
                        return;

                this.ctx.drawImage(tile, x, y);
        }

        drawEntity(e: EntityPosition) {
                var sprite = this.entitySprites[e.entity.name];
                if (!sprite)
                        return;

                this.ctx.drawImage(
                        sprite,
                        e.direction*tileSize, 0,
                        tileSize, tileSize,
                        e.position.x*tileSize, e.position.y*tileSize,
                        tileSize, tileSize
                );
        }

        draw() {
                this.redrawPending = false;

                this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);

                if (!this.tiles || !this.entitySprites)
                        return;

                this.map.layers.forEach((layer) => {
                        let y = 0;

                        layer.forEach((row) => {
                                let x = 0;

                                for (let tile in row) {
                                        this.drawTile(x, y, this.tiles[row[tile]]);
                                        x += tileSize;
                                }

                                y += tileSize;
                        });
                });

                this.getEntities().forEach(e => {
                        this.drawEntity(e);
                });
        }

        redraw() {
                if (this.redrawPending)
                        return;

                this.redrawPending = true;
                window.requestAnimationFrame(() => this.draw());
        }
}