2020-02-29 13:45:56 +01:00
|
|
|
var Container = require("./Container"),
|
2020-03-21 17:22:28 +01:00
|
|
|
TileSpriteXML = require("./TileSpriteXML")
|
2020-02-29 13:45:56 +01:00
|
|
|
;
|
2019-12-31 15:33:27 +01:00
|
|
|
|
2020-01-17 13:26:50 +01:00
|
|
|
class TileMapXML extends Container {
|
2020-03-21 17:22:28 +01:00
|
|
|
constructor(tiles, mapW, mapH, texture, xml) {
|
|
|
|
super(texture);
|
|
|
|
this.mapW = mapW;
|
|
|
|
this.mapH = mapH;
|
|
|
|
this.tileW = xml.array[tiles[0]].width;
|
|
|
|
this.tileH = xml.array[tiles[0]].height;
|
|
|
|
this.w = mapW * this.tileW;
|
|
|
|
this.h = mapH * this.tileH;
|
2020-01-17 21:11:15 +01:00
|
|
|
|
2020-03-21 17:22:28 +01:00
|
|
|
this.children = tiles.map((frame, i) => {
|
|
|
|
const s = new TileSpriteXML(texture, xml, frame);
|
|
|
|
s.frame = frame;
|
|
|
|
s.pos.x = i % mapW * this.tileW;
|
|
|
|
s.pos.y = Math.floor(i / mapW) * this.tileH;
|
|
|
|
return s;
|
|
|
|
});
|
|
|
|
}
|
2020-04-19 19:44:52 +02:00
|
|
|
|
|
|
|
pixelToMapPos(pos) {
|
|
|
|
const { tileW, tileH } = this;
|
|
|
|
return {
|
|
|
|
x: Math.floor(pos.x / tileW),
|
|
|
|
y: Math.floor(pos.y / tileH)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
mapToPixelPos(mapPos) {
|
|
|
|
const { tileW, tileH } = this;
|
|
|
|
return {
|
|
|
|
x: mapPos.x * tileW,
|
|
|
|
y: mapPos.y * tileH
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
tileAtMapPos(mapPos) {
|
|
|
|
return this.children[mapPos.y * this.mapW + mapPos.x];
|
|
|
|
}
|
|
|
|
|
|
|
|
tileAtPixelPos(pos) {
|
|
|
|
return this.tileAtMapPos(this.pixelToMapPos(pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
setFrameAtMapPos(mapPos, frame) {
|
|
|
|
const tile = this.tileAtMapPos(mapPos);
|
|
|
|
tile.frame = frame;
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
setFrameAtPixelPos(pos, frame) {
|
|
|
|
return this.setFrameAtMapPos(this.pixelToMapPos(pos), frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
tilesAtCorners(bounds, xo = 0, yo = 0) {
|
|
|
|
return [
|
|
|
|
[bounds.x, bounds.y], // Top-left
|
|
|
|
[bounds.x + bounds.w, bounds.y], // Top-right
|
|
|
|
[bounds.x, bounds.y + bounds.h], // Bottom-left
|
|
|
|
[bounds.x + bounds.w, bounds.y + bounds.h] // Bottom-right
|
|
|
|
].map(([x, y]) =>
|
|
|
|
this.tileAtPixelPos({
|
|
|
|
x: x + xo,
|
|
|
|
y: y + yo
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2019-12-31 15:33:27 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 13:45:56 +01:00
|
|
|
module.exports = TileMapXML;
|