- 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
import { TMX_CLEAR_BIT_MASK } from "./constants";
/**
* @classdesc
* an object containing all tileset
*/
export default class TMXTilesetGroup {
constructor() {
this.tilesets = [];
this.length = 0;
}
/**
* add a tileset to the tileset group
* @param {TMXTileset} tileset
*/
add(tileset) {
this.tilesets.push(tileset);
this.length++;
}
/**
* return the tileset at the specified index
* @param {number} i
* @returns {TMXTileset} corresponding tileset
*/
getTilesetByIndex(i) {
return this.tilesets[i];
}
/**
* return the tileset corresponding to the specified id <br>
* will throw an exception if no matching tileset is found
* @param {number} gid
* @returns {TMXTileset} corresponding tileset
*/
getTilesetByGid(gid) {
let invalidRange = -1;
// clear the gid of all flip/rotation flags
gid &= TMX_CLEAR_BIT_MASK;
// cycle through all tilesets
for (let i = 0, len = this.tilesets.length; i < len; i++) {
// return the corresponding tileset if matching
if (this.tilesets[i].contains(gid)) {
return this.tilesets[i];
}
// typically indicates a layer with no asset loaded (collision?)
if (this.tilesets[i].firstgid === this.tilesets[i].lastgid &&
gid >= this.tilesets[i].firstgid) {
// store the id if the [firstgid .. lastgid] is invalid
invalidRange = i;
}
}
// return the tileset with the invalid range
if (invalidRange !== -1) {
return this.tilesets[invalidRange];
}
else {
throw new Error("no matching tileset found for gid " + gid);
}
}
}