1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var index_1 = require("./../index");
|
14 | var Module_1 = require("./../Module");
|
15 | var Tool_1 = require("./../Tool");
|
16 | var Entity_1 = require("./../Entity");
|
17 |
|
18 |
|
19 |
|
20 | var Tilemap = (function (_super) {
|
21 | __extends(Tilemap, _super);
|
22 | function Tilemap() {
|
23 |
|
24 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
25 | |
26 |
|
27 |
|
28 |
|
29 | _this.walls = [];
|
30 | |
31 |
|
32 |
|
33 | _this.grid = {};
|
34 | |
35 |
|
36 |
|
37 | _this.backgroundContainers = [];
|
38 | |
39 |
|
40 |
|
41 | _this.decoratorContainers = [];
|
42 | return _this;
|
43 | }
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 | Tilemap.prototype.setData = function (data) {
|
50 | var _this = this;
|
51 | var loader = new index_1.PIXI.loaders.Loader();
|
52 | this.removeData();
|
53 | this.props.width = 0;
|
54 | this.props.height = 0;
|
55 | this.tilewidth = data.tilewidth;
|
56 | this.tileheight = data.tileheight;
|
57 | this.grid = data.grid;
|
58 |
|
59 | data.grid.forEach(function (layer) { return layer.forEach(function (line) {
|
60 | _this.props.width = line.length > _this.props.width ? line.length : _this.props.width;
|
61 | }); });
|
62 | this.props.width *= this.tilewidth;
|
63 | this.props.height = data.grid[0].length * this.tileheight;
|
64 | if (data.backgrounds) {
|
65 | Tool_1.Assets.getTexture(data.backgrounds.map(function (background) { return background.path; }), function (textures) { return _this._loadBackgrounds(data.backgrounds, textures); });
|
66 | }
|
67 | Tool_1.Assets.getImage(data.path, function (image) { return _this._loadGrids(data.grid, image); });
|
68 | if (data.decorators) {
|
69 | Tool_1.Assets.getTexture(Object.keys(data.decorators.data).map(function (key) { return data.decorators.data[key]; }), function (textures) { return _this._loadDecorators(data.decorators, textures); });
|
70 | }
|
71 | this._loadWalls(data.walls, data.debug);
|
72 | };
|
73 | |
74 |
|
75 |
|
76 | Tilemap.prototype.removeData = function () {
|
77 | var _this = this;
|
78 | [].concat(this.gridContainer || [], this.backgroundContainers, this.decoratorContainers).forEach(function (container) {
|
79 | _this.container.removeChild(container);
|
80 | container.destroy(true);
|
81 | });
|
82 | this.gridContainer = null;
|
83 | this.backgroundContainers = [];
|
84 | this.decoratorContainers = [];
|
85 | };
|
86 |
|
87 | |
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | Tilemap.prototype._loadGrids = function (grid, image) {
|
94 | var canvas = document.createElement("canvas"), ctx = canvas.getContext("2d");
|
95 | var _a = this.props, width = _a.width, height = _a.height, _b = this, tilewidth = _b.tilewidth, tileheight = _b.tileheight;
|
96 | canvas.width = width;
|
97 | canvas.height = height;
|
98 | grid.forEach(function (layer) { return layer.forEach(function (line, y) { return line.forEach(function (tile, x) {
|
99 | ctx.drawImage(image, Math.floor(tile * tilewidth) % image.width, Math.floor(tile * tilewidth / image.width) * tileheight, tilewidth, tileheight, x * tilewidth, y * tileheight, tilewidth, tileheight);
|
100 | }); }); });
|
101 | this.gridContainer = index_1.PIXI.Sprite.from(canvas);
|
102 | this.container.addChildAt(this.gridContainer, 1);
|
103 | };
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | Tilemap.prototype._loadBackgrounds = function (backgrounds, textures) {
|
111 | var _this = this;
|
112 | if (!backgrounds) {
|
113 | return null;
|
114 | }
|
115 | this.backgroundContainers = backgrounds.reverse().map(function (background, index) {
|
116 | var backgroundContainer = new index_1.PIXI.Sprite(textures.find(function (texture) { return texture.baseTexture.imageUrl === background.path; }));
|
117 | _this.container.addChildAt(backgroundContainer, 0);
|
118 | if (background.offset) {
|
119 | backgroundContainer.x = background.offset.x;
|
120 | backgroundContainer.y = background.offset.y;
|
121 | }
|
122 | if (typeof background.ratio !== "undefined" && background.ratio !== 1) {
|
123 | backgroundContainer.scale.x = background.ratio;
|
124 | backgroundContainer.scale.y = background.ratio;
|
125 | }
|
126 | return backgroundContainer;
|
127 | });
|
128 | };
|
129 | |
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | Tilemap.prototype._loadDecorators = function (decorators, textures) {
|
136 | var _this = this;
|
137 | if (!decorators || (decorators && !decorators.items)) {
|
138 | return null;
|
139 | }
|
140 | this.decoratorContainers = decorators.items.map(function (item) {
|
141 | var decoratorContainer = new index_1.PIXI.Sprite(textures.find(function (texture) { return texture.baseTexture.imageUrl === decorators.data[item[0]]; }));
|
142 | decoratorContainer.x = item[1];
|
143 | decoratorContainer.y = item[2];
|
144 | return decoratorContainer;
|
145 | }).forEach(function (decoratorContainer) { return _this.container.addChild(decoratorContainer); });
|
146 | };
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | Tilemap.prototype._loadWalls = function (wallDatas, debug) {
|
154 | var _this = this;
|
155 | if (wallDatas === void 0) { wallDatas = []; }
|
156 | if (debug === void 0) { debug = false; }
|
157 | this.walls = wallDatas.map(function (wallData) {
|
158 | var box = wallData[0], x = wallData[1], y = wallData[2], width = wallData[3], height = wallData[4], directionConstraint = wallData[5];
|
159 | return _this.spawn(new Entity_1.Wall(), x, y, { box: box, width: width, height: height, directionConstraint: directionConstraint, debug: debug });
|
160 | });
|
161 | };
|
162 | return Tilemap;
|
163 | }(Module_1.Module));
|
164 | exports.Tilemap = Tilemap;
|