updated 主角移动
This commit is contained in:
parent
b61eb83c41
commit
5c95bb333d
14
game.js
14
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)
|
||||
};
|
||||
};
|
||||
//布景对象构造器
|
||||
|
83
index.js
83
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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user