- 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
import Vector2d from "./../../../math/vector2.js";
import pool from "./../../../system/pooling.js";
import TMXRenderer from "./TMXRenderer.js";
import TMXLayer from "./../TMXLayer.js";
/**
* @classdesc
* an Isometric Map Renderder
* @augments TMXRenderer
*/
export default class TMXIsometricRenderer extends TMXRenderer {
/**
* @param {TMXTileMap} map - the TMX map
*/
constructor(map) {
super(
map.cols,
map.rows,
map.tilewidth,
map.tileheight
);
this.hTilewidth = this.tilewidth / 2;
this.hTileheight = this.tileheight / 2;
this.originX = this.rows * this.hTilewidth;
}
/**
* return true if the renderer can render the specified layer
* @ignore
*/
canRender(layer) {
return (
(layer.orientation === "isometric") &&
super.canRender(layer)
);
}
/**
* return the bounding rect for this map renderer
* @ignore
*/
getBounds(layer) {
let bounds = layer instanceof TMXLayer ? pool.pull("Bounds") : this.bounds;
bounds.setMinMax(
0, 0,
(this.cols + this.rows) * (this.tilewidth / 2),
(this.cols + this.rows) * (this.tileheight / 2)
);
return bounds;
}
/**
* return the tile position corresponding to the specified pixel
* @ignore
*/
pixelToTileCoords(x, y, v) {
let ret = v || new Vector2d();
return ret.set(
(y / this.tileheight) + ((x - this.originX) / this.tilewidth),
(y / this.tileheight) - ((x - this.originX) / this.tilewidth)
);
}
/**
* return the pixel position corresponding of the specified tile
* @ignore
*/
tileToPixelCoords(x, y, v) {
let ret = v || new Vector2d();
return ret.set(
(x - y) * this.hTilewidth + this.originX,
(x + y) * this.hTileheight
);
}
/**
* fix the position of Objects to match
* the way Tiled places them
* @ignore
*/
adjustPosition(obj) {
let tileX = obj.x / this.hTilewidth;
let tileY = obj.y / this.tileheight;
let isoPos = pool.pull("Vector2d");
this.tileToPixelCoords(tileX, tileY, isoPos);
obj.x = isoPos.x;
obj.y = isoPos.y;
pool.push(isoPos);
}
/**
* draw the tile map
* @ignore
*/
drawTile(renderer, x, y, tmxTile) {
let tileset = tmxTile.tileset;
// draw the tile
tileset.drawTile(
renderer,
((this.cols - 1) * tileset.tilewidth + (x - y) * tileset.tilewidth >> 1),
(-tileset.tilewidth + (x + y) * tileset.tileheight >> 2),
tmxTile
);
}
/**
* draw the tile map
* @ignore
*/
drawTileLayer(renderer, layer, rect) {
// cache a couple of useful references
let tileset = layer.tileset;
// get top-left and bottom-right tile position
let rowItr = this.pixelToTileCoords(
rect.pos.x - tileset.tilewidth,
rect.pos.y - tileset.tileheight,
pool.pull("Vector2d")
).floorSelf();
let tileEnd = this.pixelToTileCoords(
rect.pos.x + rect.width + tileset.tilewidth,
rect.pos.y + rect.height + tileset.tileheight,
pool.pull("Vector2d")
).ceilSelf();
let rectEnd = this.tileToPixelCoords(tileEnd.x, tileEnd.y, pool.pull("Vector2d"));
// Determine the tile and pixel coordinates to start at
let startPos = this.tileToPixelCoords(rowItr.x, rowItr.y, pool.pull("Vector2d"));
startPos.x -= this.hTilewidth;
startPos.y += this.tileheight;
/* Determine in which half of the tile the top-left corner of the area we
* need to draw is. If we're in the upper half, we need to start one row
* up due to those tiles being visible as well. How we go up one row
* depends on whether we're in the left or right half of the tile.
*/
let inUpperHalf = startPos.y - rect.pos.y > this.hTileheight;
let inLeftHalf = rect.pos.x - startPos.x < this.hTilewidth;
if (inUpperHalf) {
if (inLeftHalf) {
rowItr.x--;
startPos.x -= this.hTilewidth;
}
else {
rowItr.y--;
startPos.x += this.hTilewidth;
}
startPos.y -= this.hTileheight;
}
// Determine whether the current row is shifted half a tile to the right
let shifted = inUpperHalf ^ inLeftHalf;
// initialize the columItr vector
let columnItr = rowItr.clone();
// main drawing loop
for (let y = startPos.y * 2; y - this.tileheight * 2 < rectEnd.y * 2; y += this.tileheight) {
columnItr.setV(rowItr);
for (let x = startPos.x; x < rectEnd.x; x += this.tilewidth) {
let tmxTile = layer.cellAt(columnItr.x, columnItr.y);
// render if a valid tile position
if (tmxTile) {
tileset = tmxTile.tileset;
// offset could be different per tileset
let offset = tileset.tileoffset;
// draw our tile
tileset.drawTile(
renderer,
offset.x + x,
offset.y + y / 2 - tileset.tileheight,
tmxTile
);
}
// Advance to the next column
columnItr.x++;
columnItr.y--;
}
// Advance to the next row
if (!shifted) {
rowItr.x++;
startPos.x += this.hTilewidth;
shifted = true;
}
else {
rowItr.y++;
startPos.x -= this.hTilewidth;
shifted = false;
}
}
pool.push(columnItr);
pool.push(rowItr);
pool.push(tileEnd);
pool.push(rectEnd);
pool.push(startPos);
}
}