autotileEdges

This commit is contained in:
oc 2018-12-31 01:18:43 +08:00
parent 2a98e74847
commit 43a1876ae9
4 changed files with 53 additions and 18 deletions

View File

@ -387,22 +387,24 @@ actions.prototype.onmove = function (loc) {
var x = parseInt(loc.x / loc.size), y = parseInt(loc.y / loc.size); var x = parseInt(loc.x / loc.size), y = parseInt(loc.y / loc.size);
var pos={'x':x,'y':y}; if ((core.status.stepPostfix||[]).length>0) {
var pos0=core.status.stepPostfix[core.status.stepPostfix.length-1]; var pos={'x':x,'y':y};
var directionDistance=[pos.y-pos0.y,pos0.x-pos.x,pos0.y-pos.y,pos.x-pos0.x]; var pos0=core.status.stepPostfix[core.status.stepPostfix.length-1];
var max=0,index=4; var directionDistance=[pos.y-pos0.y,pos0.x-pos.x,pos0.y-pos.y,pos.x-pos0.x];
for(var ii=0;ii<4;ii++){ var max=0,index=4;
if(directionDistance[ii]>max){ for(var ii=0;ii<4;ii++){
index=ii; if(directionDistance[ii]>max){
max=directionDistance[ii]; 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.interval.onDownInterval = null;
// core.status.holdingPath=0; // core.status.holdingPath=0;
if(core.status.stepPostfix.length>0){ if ((core.status.stepPostfix||[]).length>0) {
var stepPostfix = []; var stepPostfix = [];
var direction={'0':{'1':'down','-1':'up'},'-1':{'0':'left'},'1':{'0':'right'}}; var direction={'0':{'1':'down','-1':'up'},'-1':{'0':'left'},'1':{'0':'right'}};
for(var ii=1;ii<core.status.stepPostfix.length;ii++){ for(var ii=1;ii<core.status.stepPostfix.length;ii++){

View File

@ -377,10 +377,10 @@ control.prototype.resetStatus = function(hero, hard, floorId, route, maps, value
// 清除游戏数据 // 清除游戏数据
core.clearStatus(); core.clearStatus();
core.status.played = true;
// 初始化status // 初始化status
core.status = core.clone(core.initStatus); core.status = core.clone(core.initStatus);
core.status.played = true;
// 初始化maps // 初始化maps
core.status.floorId = floorId; core.status.floorId = floorId;
core.status.maps = core.clone(maps); core.status.maps = core.clone(maps);

View File

@ -40,6 +40,10 @@ loader.prototype.load = function (callback) {
// 加载autotile // 加载autotile
core.material.images.autotile = {}; core.material.images.autotile = {};
core.loader.loadImages(Object.keys(core.material.icons.autotile), core.material.images.autotile, function () { core.loader.loadImages(Object.keys(core.material.icons.autotile), core.material.images.autotile, function () {
setTimeout(function () {
core.maps.makeAutotileEdges();
});
// 加载tilesets // 加载tilesets
core.material.images.tilesets = {}; core.material.images.tilesets = {};

View File

@ -627,7 +627,7 @@ maps.prototype.drawAutotile = function(ctx, mapArr, block, size, left, top, stat
} }
var getAutotileAroundId = function(currId, x, y) { var getAutotileAroundId = function(currId, x, y) {
if(x<0 || y<0 || x>=mapArr[0].length || y>=mapArr.length) return 1; if(x<0 || y<0 || x>=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 checkAround = function(x, y){ // 得到周围四个32*32块周围每块都包含当前块的1/4不清楚的话画下图你就明白的数组索引
var currId = mapArr[y][x]; 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) { maps.prototype.noPassExists = function (x, y, floorId) {
var block = core.getBlock(x,y,floorId); var block = core.getBlock(x,y,floorId);