updated 多余的条件判断、代码去除

This commit is contained in:
mumuy 2016-05-13 14:29:46 +08:00
parent f1cac66518
commit 0144d9b226

80
game.js
View File

@ -108,9 +108,9 @@ function Game(id,params){
stage:null, //布景 stage:null, //布景
x_length:0, //二维数组x轴长度 x_length:0, //二维数组x轴长度
y_length:0, //二维数组y轴长度 y_length:0, //二维数组y轴长度
frames:1, //速度等级,内部计算器times多少帧变化一次 frames:1, //速度等级,内部计算器times多少帧变化一次
times:0, //刷新画布计数(用于循环动画状态判断) times:0, //刷新画布计数(用于循环动画状态判断)
cache:false, //是否静态(如静态则设置缓存) cache:false, //是否静态(如静态则设置缓存)
update:function(){}, //更新地图数据 update:function(){}, //更新地图数据
draw:function(){}, //绘制地图 draw:function(){}, //绘制地图
}; };
@ -155,15 +155,14 @@ function Game(id,params){
type:'path' type:'path'
}; };
var options = _extend({},defaults,params); var options = _extend({},defaults,params);
var result = [];
if(options.map[options.start.y][options.start.x]||options.map[options.end.y][options.end.x]){ //当起点或终点设置在墙上 if(options.map[options.start.y][options.start.x]||options.map[options.end.y][options.end.x]){ //当起点或终点设置在墙上
return []; return [];
} }
var finded = false; var finded = false;
var result = [];
var y_length = options.map.length; var y_length = options.map.length;
var x_length = options.map[0].length; var x_length = options.map[0].length;
var steps = []; //步骤的映射 var steps = []; //步骤的映射
var steps_length = 0;
for(var y=y_length;y--;){ for(var y=y_length;y--;){
steps[y] = []; steps[y] = [];
for(var x=x_length;x--;){ for(var x=x_length;x--;){
@ -208,13 +207,12 @@ function Game(id,params){
} }
} }
}; };
for(var i=0,len=list.length;i<len;i++){ list.forEach(function(current){
var current = list[i]; next(current,{y:current.y+1,x:current.x});
next(current,{y:current.y+1,x:current.x});
next(current,{y:current.y,x:current.x+1}); next(current,{y:current.y,x:current.x+1});
next(current,{y:current.y-1,x:current.x}); next(current,{y:current.y-1,x:current.x});
next(current,{y:current.y,x:current.x-1}); next(current,{y:current.y,x:current.x-1});
} });
if(!finded&&new_list.length){ if(!finded&&new_list.length){
_render(new_list); _render(new_list);
} }
@ -340,44 +338,40 @@ function Game(id,params){
if(stage.timeout){ if(stage.timeout){
stage.timeout--; stage.timeout--;
} }
if(stage.update()!=false){ //update返回false,则不绘制 if(stage.update()!=false){ //update返回false,则不绘制
if(stage.maps.length){ stage.maps.forEach(function(map){
stage.maps.forEach(function(map){ if(!(f%map.frames)){
if(!(f%map.frames)){ map.times = f/map.frames; //计数器
map.times = f/map.frames; //计数器 }
} if(map.cache){
map.update(); if(!map.imageData){
if(map.cache){ _context.save();
if(!map.imageData){
_context.save();
map.draw(_context);
map.imageData = _context.getImageData(0,0,_.width,_.height);
_context.restore();
}else{
_context.putImageData(map.imageData,0,0);
}
}else{
map.draw(_context); map.draw(_context);
map.imageData = _context.getImageData(0,0,_.width,_.height);
_context.restore();
}else{
_context.putImageData(map.imageData,0,0);
} }
}); }else{
} map.update();
if(stage.items.length){ map.draw(_context);
stage.items.forEach(function(item){ }
if(!(f%item.frames)){ });
item.times = f/item.frames; //计数器 stage.items.forEach(function(item){
if(!(f%item.frames)){
item.times = f/item.frames; //计数器
}
if(stage.status==1&&item.status!=2){ //对象及布景状态都不处于暂停状态
if(item.location){
item.coord = item.location.position2coord(item.x,item.y);
} }
if(stage.status==1&&item.status!=2){ //对象及布景状态都处于正常状态下 if(item.timeout){
if(item.location){ item.timeout--;
item.coord = item.location.position2coord(item.x,item.y);
}
if(item.timeout){
item.timeout--;
}
item.update();
} }
item.draw(_context); item.update();
}); }
} item.draw(_context);
});
} }
_hander = requestAnimationFrame(fn); _hander = requestAnimationFrame(fn);
}; };