随机出发
This commit is contained in:
parent
d1024d3e06
commit
6a170fabcb
14
game.js
14
game.js
@ -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);
|
||||
|
77
index.js
77
index.js
@ -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,43 +283,51 @@
|
||||
type:2,
|
||||
frames:10,
|
||||
speed:1,
|
||||
timeout:Math.floor(Math.random()*120),
|
||||
update:function(){
|
||||
if(!this.coord.offset){
|
||||
var new_map = JSON.parse(JSON.stringify(map.data));
|
||||
var items = stage.getItemsByType(2);
|
||||
var index = this.index;
|
||||
items.forEach(function(item){
|
||||
if(item.index!=index){
|
||||
new_map[item.coord.y][item.coord.x]=1;
|
||||
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);
|
||||
var index = this.index;
|
||||
items.forEach(function(item){
|
||||
if(item.index!=index){
|
||||
new_map[item.coord.y][item.coord.x]=1;
|
||||
}
|
||||
});
|
||||
this.path = map.finder({
|
||||
map:new_map,
|
||||
start:this.coord,
|
||||
end:player.coord
|
||||
});
|
||||
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;
|
||||
var pos = map.coord2position(this.coord.x,this.coord.y);
|
||||
this.x = pos.x;
|
||||
this.y = pos.y;
|
||||
}
|
||||
if(this.vector.x>this.coord.x){
|
||||
this.orientation = 0;
|
||||
}else if(this.vector.x<this.coord.x){
|
||||
this.orientation = 2;
|
||||
}else if(this.vector.y>this.coord.y){
|
||||
this.orientation = 1;
|
||||
}else if(this.vector.y<this.coord.y){
|
||||
this.orientation = 3;
|
||||
}
|
||||
}
|
||||
});
|
||||
this.path = map.finder({
|
||||
map:new_map,
|
||||
start:this.coord,
|
||||
end:player.coord
|
||||
});
|
||||
if(this.path.length){
|
||||
this.vector = this.path[0];
|
||||
}
|
||||
if(this.vector.change){ //是否转变方向
|
||||
this.coord.x = this.vector.x;
|
||||
this.coord.y = this.vector.y;
|
||||
var pos = map.coord2position(this.coord.x,this.coord.y);
|
||||
this.x = pos.x;
|
||||
this.y = pos.y;
|
||||
}
|
||||
if(this.vector.x>this.coord.x){
|
||||
this.orientation = 0;
|
||||
}else if(this.vector.x<this.coord.x){
|
||||
this.orientation = 2;
|
||||
}else if(this.vector.y>this.coord.y){
|
||||
this.orientation = 1;
|
||||
}else if(this.vector.y<this.coord.y){
|
||||
this.orientation = 3;
|
||||
if(this.vector){
|
||||
this.x += this.speed*_COS[this.orientation];
|
||||
this.y += this.speed*_SIN[this.orientation];
|
||||
}
|
||||
}
|
||||
this.x += this.speed*_COS[this.orientation];
|
||||
this.y += this.speed*_SIN[this.orientation];
|
||||
},
|
||||
draw:function(context){
|
||||
context.fillStyle = this.color;
|
||||
|
Loading…
x
Reference in New Issue
Block a user