- 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
import Vector2d from "./../../../math/vector2.js";
import pool from "./../../../system/pooling.js";
import TMXRenderer from "./TMXRenderer.js";
/**
* @classdesc
* an Orthogonal Map Renderder
* @augments TMXRenderer
*/
export default class TMXOrthogonalRenderer extends TMXRenderer {
/**
* @param {TMXTileMap} map - the TMX map
*/
constructor(map) {
super(
map.cols,
map.rows,
map.tilewidth,
map.tileheight
);
}
/**
* return true if the renderer can render the specified layer
* @ignore
*/
canRender(layer) {
return (
(layer.orientation === "orthogonal") &&
super.canRender(layer)
);
}
/**
* return the tile position corresponding to the specified pixel
* @ignore
*/
pixelToTileCoords(x, y, v) {
let ret = v || new Vector2d();
return ret.set(
x / this.tilewidth,
y / this.tileheight
);
}
/**
* return the pixel position corresponding of the specified tile
* @ignore
*/
tileToPixelCoords(x, y, v) {
let ret = v || new Vector2d();
return ret.set(
x * this.tilewidth,
y * this.tileheight
);
}
/**
* fix the position of Objects to match
* the way Tiled places them
* @ignore
*/
adjustPosition(obj) {
// only adjust position if obj.gid is defined
if (typeof(obj.gid) === "number") {
// Tiled objects origin point is "bottom-left" in Tiled,
// "top-left" in melonJS)
obj.y -= obj.height;
}
}
/**
* draw the tile map
* @ignore
*/
drawTile(renderer, x, y, tmxTile) {
let tileset = tmxTile.tileset;
// draw the tile
tileset.drawTile(
renderer,
tileset.tileoffset.x + x * this.tilewidth,
tileset.tileoffset.y + (y + 1) * this.tileheight - tileset.tileheight,
tmxTile
);
}
/**
* draw the tile map
* @ignore
*/
drawTileLayer(renderer, layer, rect) {
let incX = 1, incY = 1;
// get top-left and bottom-right tile position
let start = this.pixelToTileCoords(
Math.max(rect.pos.x - (layer.maxTileSize.width - layer.tilewidth), 0),
Math.max(rect.pos.y - (layer.maxTileSize.height - layer.tileheight), 0),
pool.pull("Vector2d")
).floorSelf();
let end = this.pixelToTileCoords(
rect.pos.x + rect.width + this.tilewidth,
rect.pos.y + rect.height + this.tileheight,
pool.pull("Vector2d")
).ceilSelf();
//ensure we are in the valid tile range
end.x = end.x > this.cols ? this.cols : end.x;
end.y = end.y > this.rows ? this.rows : end.y;
switch (layer.renderorder) {
case "right-up" :
// swapping start.y and end.y
end.y = start.y + (start.y = end.y) - end.y;
incY = -1;
break;
case "left-down" :
// swapping start.x and end.x
end.x = start.x + (start.x = end.x) - end.x;
incX = -1;
break;
case "left-up" :
// swapping start.x and end.x
end.x = start.x + (start.x = end.x) - end.x;
// swapping start.y and end.y
end.y = start.y + (start.y = end.y) - end.y;
incX = -1;
incY = -1;
break;
default: // right-down
break;
}
// main drawing loop
for (let y = start.y; y !== end.y; y += incY) {
for (let x = start.x; x !== end.x; x += incX) {
let tmxTile = layer.cellAt(x, y, false);
if (tmxTile) {
this.drawTile(renderer, x, y, tmxTile);
}
}
}
pool.push(start);
pool.push(end);
}
}