随机出发

This commit is contained in:
郑浩乐 2016-01-11 08:15:26 +08:00
parent d1024d3e06
commit 6a170fabcb
2 changed files with 49 additions and 42 deletions

14
game.js
View File

@ -13,6 +13,7 @@ function Game(id,params){
for(var i in settings){
target[i] = params[i]||settings[i];
}
return target;
};
_extend(this,settings,params);
var $canvas = document.getElementById(id);
@ -46,6 +47,7 @@ function Game(id,params){
index:0, //对象索引
frames:1, //速度等级,内部计算器times多少帧变化一次
times:0, //刷新画布计数(用于循环动画状态判断)
timeout:0, //倒计时(用于过程动画状态判断)
control:{}, //控制缓存,到达定位点时处理
update:function(){}, //更新参数信息
draw:function(){} //绘制
@ -111,18 +113,13 @@ function Game(id,params){
};
};
//寻址算法
Map.prototype.finder = function(param){
Map.prototype.finder = function(params){
var defaults = {
map:null,
start:{},
end:{}
};
var options = (function(target, params) {
for (var prop in params) {
target[prop] = params[prop];
}
return target;
})(defaults,param);
var options = _extend({},defaults,params);
var result = [];
if(options.map[options.start.y][options.start.x]||options.map[options.end.y][options.end.x]){ //当起点或终点设置在墙上
return [];
@ -220,6 +217,9 @@ function Game(id,params){
if(item.location){
item.coord = item.location.position2coord(item.x,item.y);
}
if(item.timeout){
item.timeout--;
}
item.update();
}
item.draw(_context);

View File

@ -112,9 +112,8 @@
var dx = item.x-player.x;
var dy = item.y-player.y;
if(dx*dx+dy*dy<750){
stage.status = 2;
stage.status = 3;
stage.timeout = 30;
player.status = 3;
}
});
}else if(stage.status=3){
@ -257,7 +256,7 @@
draw:function(context){
context.fillStyle = '#FC3';
context.beginPath();
if(this.status<3){
if(stage.status<3){
if(this.times%2){
context.arc(this.x,this.y,this.width/2,(.5*this.orientation+.20)*Math.PI,(.5*this.orientation-.20)*Math.PI,false);
}else{
@ -284,7 +283,10 @@
type:2,
frames:10,
speed:1,
timeout:Math.floor(Math.random()*120),
update:function(){
console.log(this.timeout);
if(!this.timeout){
if(!this.coord.offset){
var new_map = JSON.parse(JSON.stringify(map.data));
var items = stage.getItemsByType(2);
@ -302,6 +304,7 @@
if(this.path.length){
this.vector = this.path[0];
}
if(this.vector){
if(this.vector.change){ //是否转变方向
this.coord.x = this.vector.x;
this.coord.y = this.vector.y;
@ -319,8 +322,12 @@
this.orientation = 3;
}
}
}
if(this.vector){
this.x += this.speed*_COS[this.orientation];
this.y += this.speed*_SIN[this.orientation];
}
}
},
draw:function(context){
context.fillStyle = this.color;