- 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
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
import Vector2d from "./../../math/vector2.js";
import { renderer } from "./../../video/video.js";
import * as fileUtil from "./../../utils/file.js";
import timer from "./../../system/timer.js";
import { getTMX, getImage } from "./../../loader/loader.js";
/**
* @classdesc
* a TMX Tile Set Object
*/
export default class TMXTileset {
/**
* @param {object} tileset - tileset data in JSON format ({@link http://docs.mapeditor.org/en/stable/reference/tmx-map-format/#tileset})
*/
constructor(tileset) {
// tile properties (collidable, etc..)
this.TileProperties = [];
// hold reference to each tile image
this.imageCollection = [];
this.firstgid = this.lastgid = +tileset.firstgid;
// check if an external tileset is defined
if (typeof(tileset.source) !== "undefined") {
let src = tileset.source;
let ext = fileUtil.getExtension(src);
if (ext === "tsx" || ext === "json") {
// load the external tileset (TSX/JSON)
tileset = getTMX(fileUtil.getBasename(src));
if (!tileset) {
throw new Error(src + " external TSX/JSON tileset not found");
}
}
}
this.name = tileset.name;
this.tilewidth = +tileset.tilewidth;
this.tileheight = +tileset.tileheight;
this.spacing = +tileset.spacing || 0;
this.margin = +tileset.margin || 0;
// set tile offset properties (if any)
this.tileoffset = new Vector2d();
/**
* Tileset contains animated tiles
* @type {boolean}
*/
this.isAnimated = false;
/**
* true if the tileset is a "Collection of Image" Tileset
* @type {boolean}
*/
this.isCollection = false;
/**
* the tileset class
* @type {boolean}
*/
this.class = tileset.class;
/**
* Tileset animations
* @private
*/
this.animations = new Map();
/**
* Remember the last update timestamp to prevent too many animation updates
* @private
*/
this._lastUpdate = 0;
let tiles = tileset.tiles;
for (let i in tiles) {
if (tiles.hasOwnProperty(i)) {
if ("animation" in tiles[i]) {
this.isAnimated = true;
this.animations.set(tiles[+i].animation[0].tileid, {
dt : 0,
idx : 0,
frames : tiles[+i].animation,
cur : tiles[+i].animation[0]
});
}
// set tile properties, if any
if ("properties" in tiles[i]) {
if (Array.isArray(tiles[i].properties)) { // JSON (new format)
let tileProperty = {};
for (let j in tiles[i].properties) {
tileProperty[tiles[i].properties[j].name] = tiles[i].properties[j].value;
}
this.setTileProperty(+tiles[i].id + this.firstgid, tileProperty);
} else { // XML format
this.setTileProperty(+i + this.firstgid, tiles[i].properties);
}
}
if ("image" in tiles[i]) {
let image = getImage(tiles[i].image);
if (!image) {
throw new Error("melonJS: '" + tiles[i].image + "' file for tile '" + (+i + this.firstgid) + "' not found!");
}
this.imageCollection[+i + this.firstgid] = image;
}
}
}
this.isCollection = this.imageCollection.length > 0;
let offset = tileset.tileoffset;
if (offset) {
this.tileoffset.x = +offset.x;
this.tileoffset.y = +offset.y;
}
// set tile properties, if any (JSON old format)
let tileInfo = tileset.tileproperties;
if (tileInfo) {
for (let i in tileInfo) {
if (tileInfo.hasOwnProperty(i)) {
this.setTileProperty(+i + this.firstgid, tileInfo[i]);
}
}
}
// if not a tile image collection
if (this.isCollection === false) {
// get the global tileset texture
this.image = getImage(tileset.image);
if (!this.image) {
throw new Error("melonJS: '" + tileset.image + "' file for tileset '" + this.name + "' not found!");
}
// create a texture atlas for the given tileset
this.texture = renderer.cache.get(this.image, {
framewidth : this.tilewidth,
frameheight : this.tileheight,
margin : this.margin,
spacing : this.spacing
});
this.atlas = this.texture.getAtlas();
// calculate the number of tiles per horizontal line
let hTileCount = +tileset.columns || Math.round(this.image.width / (this.tilewidth + this.spacing));
let vTileCount = Math.round(this.image.height / (this.tileheight + this.spacing));
if (tileset.tilecount % hTileCount > 0) {
++vTileCount;
}
// compute the last gid value in the tileset
this.lastgid = this.firstgid + (((hTileCount * vTileCount) - 1) || 0);
if (tileset.tilecount && this.lastgid - this.firstgid + 1 !== +tileset.tilecount) {
console.warn(
"Computed tilecount (" + (this.lastgid - this.firstgid + 1) +
") does not match expected tilecount (" + tileset.tilecount + ")"
);
}
}
}
/**
* return the tile image from a "Collection of Image" tileset
* @param {number} gid
* @returns {Image} corresponding image or undefined
*/
getTileImage(gid) {
return this.imageCollection[gid];
}
/**
* set the tile properties
* @ignore
*/
setTileProperty(gid, prop) {
// set the given tile id
this.TileProperties[gid] = prop;
}
/**
* return true if the gid belongs to the tileset
* @param {number} gid
* @returns {boolean}
*/
contains(gid) {
return gid >= this.firstgid && gid <= this.lastgid;
}
/**
* Get the view (local) tile ID from a GID, with animations applied
* @param {number} gid - Global tile ID
* @returns {number} View tile ID
*/
getViewTileId(gid) {
let localId = gid - this.firstgid;
if (this.animations.has(localId)) {
// return the current corresponding tile id if animated
return this.animations.get(localId).cur.tileid;
}
return localId;
}
/**
* return the properties of the specified tile
* @param {number} tileId
* @returns {object}
*/
getTileProperties(tileId) {
return this.TileProperties[tileId];
}
// update tile animations
update(dt) {
let duration = 0,
now = timer.getTime(),
result = false;
if (this._lastUpdate !== now) {
this._lastUpdate = now;
this.animations.forEach((anim) => {
anim.dt += dt;
duration = anim.cur.duration;
while (anim.dt >= duration) {
anim.dt -= duration;
anim.idx = (anim.idx + 1) % anim.frames.length;
anim.cur = anim.frames[anim.idx];
duration = anim.cur.duration;
result = true;
}
});
}
return result;
}
// draw the x,y tile
drawTile(renderer, dx, dy, tmxTile) {
// check if any transformation is required
if (tmxTile.flipped) {
renderer.save();
// apply the tile current transform
renderer.translate(dx, dy);
renderer.transform(tmxTile.currentTransform);
// reset both values as managed through transform();
dx = dy = 0;
}
// check if the tile has an associated image
if (this.isCollection === true) {
// draw the tile
renderer.drawImage(
this.imageCollection[tmxTile.tileId],
0, 0,
tmxTile.width, tmxTile.height,
dx, dy,
tmxTile.width, tmxTile.height
);
} else {
// use the tileset texture
let offset = this.atlas[this.getViewTileId(tmxTile.tileId)].offset;
// draw the tile
renderer.drawImage(
this.image,
offset.x, offset.y,
this.tilewidth, this.tileheight,
dx, dy,
this.tilewidth + renderer.uvOffset, this.tileheight + renderer.uvOffset
);
}
if (tmxTile.flipped) {
// restore the context to the previous state
renderer.restore();
}
}
}