From 5c95bb333dab6b9afd6b69b6ef2dedec15f23438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=B5=A9=E4=B9=90?= <89932980@qq.com> Date: Mon, 4 Jan 2016 17:03:04 +0800 Subject: [PATCH] =?UTF-8?q?updated=20=E4=B8=BB=E8=A7=92=E7=A7=BB=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game.js | 14 ++++++---- index.js | 83 +++++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 76 insertions(+), 21 deletions(-) diff --git a/game.js b/game.js index 55179ec..8e02983 100644 --- a/game.js +++ b/game.js @@ -36,6 +36,7 @@ function Game(id,options){ y:0 }, speed:1, //速度等级,内部计算器times多少帧变化一次 + control:{}, //控制缓存,到达定位点时处理 update:function(){}, //更新参数信息 draw:function(){} //绘制 }; @@ -83,17 +84,20 @@ function Game(id,options){ return -1; }; //地图坐标转画布坐标 - Map.prototype.coord2position = function(x,y){ + Map.prototype.coord2position = function(cx,cy){ return { - x:this.x+x*this.size+this.size/2, - y:this.y+y*this.size+this.size/2 + x:this.x+cx*this.size+this.size/2, + y:this.y+cy*this.size+this.size/2 }; }; //画布坐标转地图坐标 Map.prototype.position2coord = function(x,y){ + var fx = (x-this.x)%this.size-this.size/2; + var fy = (y-this.y)%this.size-this.size/2; return { - x:Math.floor((x-this.x)/this.size-.5), - y:Math.floor((y-this.y)/this.size-.5) + x:Math.floor((x-this.x)/this.size), + y:Math.floor((y-this.y)/this.size), + offset:Math.sqrt(fx*fx+fy*fy) }; }; //布景对象构造器 diff --git a/index.js b/index.js index 8ea3d9f..b36b6bc 100644 --- a/index.js +++ b/index.js @@ -175,34 +175,32 @@ context.closePath(); break; default: - if(Math.floor(code/1000)){ + var arr = String.prototype.split.call(code,''); + if(+arr.pop()){ context.beginPath(); context.moveTo(pos.x,pos.y); - context.lineTo(pos.x,pos.y-this.size/2); + context.lineTo(pos.x-this.size/2,pos.y); context.stroke(); context.closePath(); } - code %= 1000; - if(Math.floor(code/100)){ - context.beginPath(); - context.moveTo(pos.x,pos.y); - context.lineTo(pos.x+this.size/2,pos.y); - context.stroke(); - context.closePath(); - } - code %= 100; - if(Math.floor(code/10)){ + if(+arr.pop()){ context.beginPath(); context.moveTo(pos.x,pos.y); context.lineTo(pos.x,pos.y+this.size/2); context.stroke(); context.closePath(); } - code %= 10; - if(Math.floor(code/1)){ + if(+arr.pop()){ context.beginPath(); context.moveTo(pos.x,pos.y); - context.lineTo(pos.x-this.size/2,pos.y); + context.lineTo(pos.x+this.size/2,pos.y); + context.stroke(); + context.closePath(); + } + if(+arr.pop()){ + context.beginPath(); + context.moveTo(pos.x,pos.y); + context.lineTo(pos.x,pos.y-this.size/2); context.stroke(); context.closePath(); } @@ -222,6 +220,59 @@ height:30, orientation:3, speed:10, + update:function(){ + var coord = map.position2coord(this.x,this.y); + var inPlace = !coord.offset; + if(inPlace){ + if(this.control.orientation){ + switch(this.control.orientation){ + case 0: + if(!map.get(coord.x,coord.y-1)){ + this.orientation = this.control.orientation; + } + break; + case 1: + if(!map.get(coord.x+1,coord.y)){ + this.orientation = this.control.orientation; + } + break; + case 2: + if(!map.get(coord.x,coord.y+1)){ + this.orientation = this.control.orientation; + } + break; + case 3: + if(!map.get(coord.x-1,coord.y)){ + this.orientation = this.control.orientation; + } + break; + } + } + this.control.orientation = null; + } + switch(this.orientation){ + case 0: + if(!(map.get(coord.x,coord.y-1)&&inPlace)){ + this.y-=1; + } + break; + case 1: + if(!(map.get(coord.x+1,coord.y)&&inPlace)){ + this.x+=1; + } + break; + case 2: + if(!(map.get(coord.x,coord.y+1)&&inPlace)){ + this.y+=1; + } + break; + case 3: + if(!(map.get(coord.x-1,coord.y)&&inPlace)){ + this.x-=1; + } + break; + } + }, draw:function(context){ context.fillStyle = '#FC3'; context.beginPath(); @@ -261,7 +312,7 @@ } }); stage.bind('keydown',function(e){ - player.orientation = MAP_ORIENTATION[e.keyCode]; + player.control = {orientation:MAP_ORIENTATION[e.keyCode]}; }); })(); game.init();