diff --git a/libs/actions.js b/libs/actions.js index e4878e48..3972b51e 100644 --- a/libs/actions.js +++ b/libs/actions.js @@ -387,22 +387,24 @@ actions.prototype.onmove = function (loc) { var x = parseInt(loc.x / loc.size), y = parseInt(loc.y / loc.size); - var pos={'x':x,'y':y}; - var pos0=core.status.stepPostfix[core.status.stepPostfix.length-1]; - var directionDistance=[pos.y-pos0.y,pos0.x-pos.x,pos0.y-pos.y,pos.x-pos0.x]; - var max=0,index=4; - for(var ii=0;ii<4;ii++){ - if(directionDistance[ii]>max){ - index=ii; - max=directionDistance[ii]; + if ((core.status.stepPostfix||[]).length>0) { + var pos={'x':x,'y':y}; + var pos0=core.status.stepPostfix[core.status.stepPostfix.length-1]; + var directionDistance=[pos.y-pos0.y,pos0.x-pos.x,pos0.y-pos.y,pos.x-pos0.x]; + var max=0,index=4; + for(var ii=0;ii<4;ii++){ + if(directionDistance[ii]>max){ + index=ii; + max=directionDistance[ii]; + } + } + pos=[{'x':0,'y':1},{'x':-1,'y':0},{'x':0,'y':-1},{'x':1,'y':0},false][index] + if(pos){ + pos.x+=pos0.x; + pos.y+=pos0.y; + core.status.stepPostfix.push(pos); + core.fillPosWithPoint(pos); } - } - pos=[{'x':0,'y':1},{'x':-1,'y':0},{'x':0,'y':-1},{'x':1,'y':0},false][index] - if(pos){ - pos.x+=pos0.x; - pos.y+=pos0.y; - core.status.stepPostfix.push(pos); - core.fillPosWithPoint(pos); } } @@ -422,7 +424,7 @@ actions.prototype.onup = function () { core.interval.onDownInterval = null; // core.status.holdingPath=0; - if(core.status.stepPostfix.length>0){ + if ((core.status.stepPostfix||[]).length>0) { var stepPostfix = []; var direction={'0':{'1':'down','-1':'up'},'-1':{'0':'left'},'1':{'0':'right'}}; for(var ii=1;ii=mapArr[0].length || y>=mapArr.length) return 1; - else return mapArr[y][x]==currId ? 1:0; + else return core.material.autotileEdges[currId].indexOf(mapArr[y][x])>=0; } var checkAround = function(x, y){ // 得到周围四个32*32块(周围每块都包含当前块的1/4,不清楚的话画下图你就明白)的数组索引 var currId = mapArr[y][x]; @@ -681,6 +681,35 @@ maps.prototype.drawAutotile = function(ctx, mapArr, block, size, left, top, stat } } +////// 为autotile判定边界 ////// +maps.prototype.makeAutotileEdges = function () { + var autotileIds = Object.keys(core.material.images.autotile); + core.material.autotileEdges = {}; + + var canvas = document.createElement("canvas"), ctx = canvas.getContext('2d'); + canvas.width = canvas.height = 32; + + autotileIds.forEach(function (t) { + var n = core.maps.getNumberById(t); + core.material.autotileEdges[n] = [n]; + + ctx.clearRect(0,0,32,32); + ctx.drawImage(core.material.images.autotile[t], 0, 0, 32, 32, 0, 0, 32, 32); + var data = canvas.toDataURL("image/png"); + + autotileIds.forEach(function (t2) { + if (t==t2) return; + var n2 = core.maps.getNumberById(t2); + + ctx.clearRect(0,0,32,32); + ctx.drawImage(core.material.images.autotile[t2], 32, 0, 32, 32, 0, 0, 32, 32); + if (data == canvas.toDataURL("image/png")) { + core.material.autotileEdges[n].push(n2); + } + }); + }); +} + ////// 某个点是否不可通行 ////// maps.prototype.noPassExists = function (x, y, floorId) { var block = core.getBlock(x,y,floorId);