fix autotile

This commit is contained in:
dljgs1 2019-08-05 18:27:33 +08:00
parent acd5832db2
commit e9cc2e979e

View File

@ -938,13 +938,91 @@ maps.prototype._drawFloorImage = function (ctx, name, type, image, offsetX, widt
}
////// 绘制Autotile //////
maps.prototype._drawAutotile = function (ctx, mapArr, block, size, left, top, status) {
var xx = block.x, yy = block.y;
var autotile = core.material.images['autotile'][block.event.id];
status = status || 0;
status %= parseInt(autotile.width / 96);
var done = {};
function drawAutotile(canvas, x, y, size, autotile, index) {
var isGrass = function(x,y){
if(core.maps._drawAutotile_getAutotileAroundId(mapArr[yy][xx],x,y,mapArr)){
return 1;
}else{
return 0;
}
}
var iG = [];
[-1,0,1].forEach(function(_x){
iG[_x] = [];
[-1,0,1].forEach(function(_y){
iG[_x][_y] = isGrass(xx + _x, yy + _y);
})});
if(iG[-1][-1] + iG[0][-1] + iG[0][0] + iG[-1][0] == 3 && !iG[-1][-1]){
this._drawAutotile_render(ctx, xx * size + left, yy * size + top, size, autotile, status, 16);
done[0] = true;
}
if(iG[0][-1] + iG[1][-1] + iG[1][0] + iG[0][0] == 3 && !iG[1][-1]){
this._drawAutotile_render(ctx, xx * size + left + size/2, yy * size + top, size, autotile, status, 17);
done[1] = true;
}
if(iG[0][0] + iG[1][0] + iG[1][1] + iG[0][1] == 3 && !iG[1][1]){
this._drawAutotile_render(ctx, xx * size + left+size/2, yy * size + top + size/2, size, autotile, status, 18);
done[3] = true;
}
if(iG[0-1][0] + iG[0][0] + iG[0][1] + iG[-1][1] == 3 && !iG[-1][1]){
this._drawAutotile_render(ctx, xx * size + left, yy * size + top + size/2, size, autotile, status, 19);
done[2] = true;
}
var _id = iG[0][-1] + 2 * iG[-1][0] + 4 * iG[0][1] + 8 * iG[1][0];
this._drawAutotile_render(ctx, xx * size, yy * size, size, autotile, status, _id, done);
/*
var _id = isGrass(xx, yy - 1) + 2 * isGrass(xx - 1, yy) + 4 * isGrass(xx, yy + 1) + 8 * isGrass(xx + 1, yy);
grass(_id, xx, yy);
if(isGrass(xx-1, yy-1) + isGrass(xx, yy - 1) + isGrass(xx, yy) + isGrass(xx - 1, yy) == 3){
if (!isGrass(xx - 1, yy - 1)) {
drawAutotile(ctx, xx * size + left, yy * size + top, size, autotile, 16);
}
}
if(isGrass(xx, yy - 1) + isGrass(xx + 1, yy - 1) + isGrass(xx + 1, yy) + isGrass(xx, yy) == 3){
if (!isGrass(xx + 1, yy - 1)) {
drawAutotile(ctx, xx * size + left + size/2, yy * size + top, size, autotile, 17);
}
}
if(isGrass(xx, yy) + isGrass(xx + 1, yy) + isGrass(xx + 1, yy + 1) + isGrass(xx, yy + 1) == 3){
if (!isGrass(xx + 1, yy + 1)) {
drawAutotile(ctx, xx * size + left+size/2, yy * size + top + size/2, size, autotile, 18);
}
}
if(isGrass(xx-1, yy) + isGrass(xx, yy) + isGrass(xx, yy + 1) + isGrass(xx - 1, yy + 1) == 3){
if (!isGrass(xx - 1, yy + 1)) {
drawAutotile(ctx, xx * size + left, yy * size + top + size/2, size, autotile, 19);
}
}
if (isGrass(xx, yy) + isGrass(xx + 1, yy) + isGrass(xx + 1, yy + 1) + isGrass(xx, yy + 1) != 3){
}
else{
if (!isGrass(xx, yy)) {
drawAutotile(ctx, xx * 32 + 32, yy * 32 + 32, 32, autotile, 16);
}
if (!isGrass(xx + 1, yy)) {
drawAutotile(ctx, xx * 32 + 16, yy * 32 + 32, 32, autotile, 17);
}
if (!isGrass(xx + 1, yy + 1)) {
drawAutotile(ctx, xx * 32 + 16, yy * 32 + 16, 32, autotile, 18);
}
if (!isGrass(xx, yy + 1)) {
drawAutotile(ctx, xx * 32 + 32, yy * 32 + 16, 32, autotile, 19);
}
}
*/
}
maps.prototype._drawAutotile_render = function(canvas, x, y, size, autotile, status, index, done) {
var data = [];
switch (index) {
case 0:
@ -1017,11 +1095,16 @@ maps.prototype._drawAutotile = function (ctx, mapArr, block, size, left, top, st
data.push([96 * status + 2 * 32, 16, 16, 16, x, y, size / 2, size / 2]);
break;
}
if(index>=16){
if(index>=16){ // 拐角直接绘制
canvas.drawImage(autotile, data[0][0], data[0][1], data[0][2], data[0][3], data[0][4], data[0][5], size/2, size/2);
return;
}else{ // 非拐角要根据是否已经绘制进行切分后绘制
this._drawAutotile_renderCut(canvas, autotile, x, y, size, data, done);
}
}
maps.prototype._drawAutotile_renderCut = function(canvas, autotile, x, y, size, data, done){
var drawData = [];
done = done || {};
if(data.length == 2){
var idx = 0;
var cut = 0;
@ -1045,7 +1128,6 @@ maps.prototype._drawAutotile = function (ctx, mapArr, block, size, left, top, st
if(!done[idx])drawData[idx] = [data[i][0], data[i][1]];
if(!done[idx + 2])drawData[idx + 2] = [data[i][0], parseInt(data[i][1]) + 16];
}
}
}else{
if(!done[0])drawData[0] = [data[0][0], data[0][1]];
@ -1053,48 +1135,12 @@ maps.prototype._drawAutotile = function (ctx, mapArr, block, size, left, top, st
if(!done[2])drawData[2] = [data[0][0], data[0][1] + 16];
if(!done[3])drawData[3] = [data[0][0] + 16, data[0][1] + 16];
}
[0,1,2,3].forEach(function(i){
var dt = drawData[i];if(!dt)return;
for(var i = 0; i<4; i++){
var dt = drawData[i];if(!dt)continue;
canvas.drawImage(autotile, dt[0], dt[1], 16, 16, x + (i % 2) * size / 2, y + parseInt(i / 2) * size / 2, size/2, size/2);
});
};
}
}
var isGrass = function(x,y){
if(core.maps._drawAutotile_getAutotileAroundId(mapArr[yy][xx],x,y,mapArr)){
return 1;
}else{
return 0;
}
}
var grass = function (ii, x, y) {
//ctx.clearRect(x * size, y * size, size, size);
drawAutotile(ctx, x * size, y * size, size, autotile, ii);
}
var iG = [];
[-1,0,1].forEach(function(_x){
iG[_x] = [];
[-1,0,1].forEach(function(_y){
iG[_x][_y] = isGrass(xx + _x, yy + _y);
})});
var _id = iG[0][-1] + 2 * iG[-1][0] + 4 * iG[0][1] + 8 * iG[1][0];
if(iG[-1][-1] + iG[0][-1] + iG[0][0] + iG[-1][0] == 3 && !iG[-1][-1]){
drawAutotile(ctx, xx * size + left, yy * size + top, size, autotile, 16);
done[0] = true;
}
if(iG[0][-1] + iG[1][-1] + iG[1][0] + iG[0][0] == 3 && !iG[1][-1]){
drawAutotile(ctx, xx * size + left + size/2, yy * size + top, size, autotile, 17);
done[1] = true;
}
if(iG[0][0] + iG[1][0] + iG[1][1] + iG[0][1] == 3 && !iG[1][1]){
drawAutotile(ctx, xx * size + left+size/2, yy * size + top + size/2, size, autotile, 18);
done[3] = true;
}
if(iG[0-1][0] + iG[0][0] + iG[0][1] + iG[-1][1] == 3 && !iG[-1][1]){
drawAutotile(ctx, xx * size + left, yy * size + top + size/2, size, autotile, 19);
done[2] = true;
}
grass(_id, xx, yy);
}
maps.prototype._drawAutotile_drawBlockByIndex = function (ctx, dx, dy, autotileImg, index, size, status) {
//index为autotile的图块索引1-48