updated 主角移动

This commit is contained in:
郑浩乐 2016-01-04 17:03:04 +08:00
parent b61eb83c41
commit 5c95bb333d
2 changed files with 76 additions and 21 deletions

14
game.js
View File

@ -36,6 +36,7 @@ function Game(id,options){
y:0 y:0
}, },
speed:1, //速度等级,内部计算器times多少帧变化一次 speed:1, //速度等级,内部计算器times多少帧变化一次
control:{}, //控制缓存,到达定位点时处理
update:function(){}, //更新参数信息 update:function(){}, //更新参数信息
draw:function(){} //绘制 draw:function(){} //绘制
}; };
@ -83,17 +84,20 @@ function Game(id,options){
return -1; return -1;
}; };
//地图坐标转画布坐标 //地图坐标转画布坐标
Map.prototype.coord2position = function(x,y){ Map.prototype.coord2position = function(cx,cy){
return { return {
x:this.x+x*this.size+this.size/2, x:this.x+cx*this.size+this.size/2,
y:this.y+y*this.size+this.size/2 y:this.y+cy*this.size+this.size/2
}; };
}; };
//画布坐标转地图坐标 //画布坐标转地图坐标
Map.prototype.position2coord = function(x,y){ 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 { return {
x:Math.floor((x-this.x)/this.size-.5), x:Math.floor((x-this.x)/this.size),
y:Math.floor((y-this.y)/this.size-.5) y:Math.floor((y-this.y)/this.size),
offset:Math.sqrt(fx*fx+fy*fy)
}; };
}; };
//布景对象构造器 //布景对象构造器

View File

@ -175,34 +175,32 @@
context.closePath(); context.closePath();
break; break;
default: default:
if(Math.floor(code/1000)){ var arr = String.prototype.split.call(code,'');
if(+arr.pop()){
context.beginPath(); context.beginPath();
context.moveTo(pos.x,pos.y); 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.stroke();
context.closePath(); context.closePath();
} }
code %= 1000; if(+arr.pop()){
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)){
context.beginPath(); context.beginPath();
context.moveTo(pos.x,pos.y); context.moveTo(pos.x,pos.y);
context.lineTo(pos.x,pos.y+this.size/2); context.lineTo(pos.x,pos.y+this.size/2);
context.stroke(); context.stroke();
context.closePath(); context.closePath();
} }
code %= 10; if(+arr.pop()){
if(Math.floor(code/1)){
context.beginPath(); context.beginPath();
context.moveTo(pos.x,pos.y); 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.stroke();
context.closePath(); context.closePath();
} }
@ -222,6 +220,59 @@
height:30, height:30,
orientation:3, orientation:3,
speed:10, 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){ draw:function(context){
context.fillStyle = '#FC3'; context.fillStyle = '#FC3';
context.beginPath(); context.beginPath();
@ -261,7 +312,7 @@
} }
}); });
stage.bind('keydown',function(e){ stage.bind('keydown',function(e){
player.orientation = MAP_ORIENTATION[e.keyCode]; player.control = {orientation:MAP_ORIENTATION[e.keyCode]};
}); });
})(); })();
game.init(); game.init();