#28 新增tiledMapRenderer用于渲染tiledmap
This commit is contained in:
@@ -33,6 +33,81 @@ module es {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取x/y坐标处的TmxLayerTile。注意,这些是平铺坐标而不是世界坐标!
|
||||
* @param x
|
||||
* @param y
|
||||
*/
|
||||
public getTile(x: number, y: number): TmxLayerTile {
|
||||
return this.tiles[x + y * this.width];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回平铺空间中的矩形列表,其中任何非空平铺组合为边界区域
|
||||
*/
|
||||
public getCollisionRectangles(): Rectangle[] {
|
||||
let checkedIndexes = [];
|
||||
let rectangles = [];
|
||||
let startCol = -1;
|
||||
let index = -1;
|
||||
|
||||
for (let y = 0; y < this.map.height; y ++){
|
||||
for (let x = 0; x< this.map.width; x ++){
|
||||
index = y * this.map.width + x;
|
||||
let tile = this.getTile(x, y);
|
||||
if (tile && !checkedIndexes[index]){
|
||||
if (startCol < 0)
|
||||
startCol = x;
|
||||
checkedIndexes[index] = true;
|
||||
}else if(tile || checkedIndexes[index]){
|
||||
if (startCol >= 0){
|
||||
rectangles.push(this.findBoundsRect(startCol, x, y, checkedIndexes));
|
||||
startCol = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (startCol >= 0){
|
||||
rectangles.push(this.findBoundsRect(startCol, this.map.width, y, checkedIndexes));
|
||||
startCol = -1;
|
||||
}
|
||||
}
|
||||
|
||||
return rectangles;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在startX和endX之间的tile周围找到最大的边界矩形,从startY开始,尽可能向下
|
||||
* @param startX
|
||||
* @param endX
|
||||
* @param startY
|
||||
* @param checkedIndexes
|
||||
*/
|
||||
public findBoundsRect(startX: number, endX: number, startY: number, checkedIndexes?: boolean[]){
|
||||
let index = -1;
|
||||
for (let y = startY + 1; y < this.map.height; y ++){
|
||||
for (let x = startX; x < endX; x ++){
|
||||
index = y * this.map.width + x;
|
||||
let tile = this.getTile(x, y);
|
||||
if (tile || checkedIndexes[index]){
|
||||
// 再次将我们到目前为止在这一行中访问过的所有内容设置为false,因为它不会包含在矩形中,应该再次进行检查
|
||||
for (let _x = startX; _x < x; _x++){
|
||||
index = y * this.map.width + _x;
|
||||
checkedIndexes[index] = false;
|
||||
}
|
||||
|
||||
return new Rectangle(startX * this.map.tileWidth, startY * this.map.tileHeight,
|
||||
(endX - startX) * this.map.tileWidth, (y - startY) * this.map.tileHeight);
|
||||
}
|
||||
|
||||
checkedIndexes[index] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return new Rectangle(startX * this.map.tileWidth, startY * this.map.tileHeight,
|
||||
(endX - startX) * this.map.tileWidth, (this.map.height - startY) * this.map.tileHeight);
|
||||
}
|
||||
}
|
||||
|
||||
export class TmxLayerTile {
|
||||
|
||||
Reference in New Issue
Block a user