随机出发
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){
|
for(var i in settings){
|
||||||
target[i] = params[i]||settings[i];
|
target[i] = params[i]||settings[i];
|
||||||
}
|
}
|
||||||
|
return target;
|
||||||
};
|
};
|
||||||
_extend(this,settings,params);
|
_extend(this,settings,params);
|
||||||
var $canvas = document.getElementById(id);
|
var $canvas = document.getElementById(id);
|
||||||
@ -46,6 +47,7 @@ function Game(id,params){
|
|||||||
index:0, //对象索引
|
index:0, //对象索引
|
||||||
frames:1, //速度等级,内部计算器times多少帧变化一次
|
frames:1, //速度等级,内部计算器times多少帧变化一次
|
||||||
times:0, //刷新画布计数(用于循环动画状态判断)
|
times:0, //刷新画布计数(用于循环动画状态判断)
|
||||||
|
timeout:0, //倒计时(用于过程动画状态判断)
|
||||||
control:{}, //控制缓存,到达定位点时处理
|
control:{}, //控制缓存,到达定位点时处理
|
||||||
update:function(){}, //更新参数信息
|
update:function(){}, //更新参数信息
|
||||||
draw:function(){} //绘制
|
draw:function(){} //绘制
|
||||||
@ -111,18 +113,13 @@ function Game(id,params){
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
//寻址算法
|
//寻址算法
|
||||||
Map.prototype.finder = function(param){
|
Map.prototype.finder = function(params){
|
||||||
var defaults = {
|
var defaults = {
|
||||||
map:null,
|
map:null,
|
||||||
start:{},
|
start:{},
|
||||||
end:{}
|
end:{}
|
||||||
};
|
};
|
||||||
var options = (function(target, params) {
|
var options = _extend({},defaults,params);
|
||||||
for (var prop in params) {
|
|
||||||
target[prop] = params[prop];
|
|
||||||
}
|
|
||||||
return target;
|
|
||||||
})(defaults,param);
|
|
||||||
var result = [];
|
var result = [];
|
||||||
if(options.map[options.start.y][options.start.x]||options.map[options.end.y][options.end.x]){ //当起点或终点设置在墙上
|
if(options.map[options.start.y][options.start.x]||options.map[options.end.y][options.end.x]){ //当起点或终点设置在墙上
|
||||||
return [];
|
return [];
|
||||||
@ -220,6 +217,9 @@ function Game(id,params){
|
|||||||
if(item.location){
|
if(item.location){
|
||||||
item.coord = item.location.position2coord(item.x,item.y);
|
item.coord = item.location.position2coord(item.x,item.y);
|
||||||
}
|
}
|
||||||
|
if(item.timeout){
|
||||||
|
item.timeout--;
|
||||||
|
}
|
||||||
item.update();
|
item.update();
|
||||||
}
|
}
|
||||||
item.draw(_context);
|
item.draw(_context);
|
||||||
|
77
index.js
77
index.js
@ -112,9 +112,8 @@
|
|||||||
var dx = item.x-player.x;
|
var dx = item.x-player.x;
|
||||||
var dy = item.y-player.y;
|
var dy = item.y-player.y;
|
||||||
if(dx*dx+dy*dy<750){
|
if(dx*dx+dy*dy<750){
|
||||||
stage.status = 2;
|
stage.status = 3;
|
||||||
stage.timeout = 30;
|
stage.timeout = 30;
|
||||||
player.status = 3;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}else if(stage.status=3){
|
}else if(stage.status=3){
|
||||||
@ -257,7 +256,7 @@
|
|||||||
draw:function(context){
|
draw:function(context){
|
||||||
context.fillStyle = '#FC3';
|
context.fillStyle = '#FC3';
|
||||||
context.beginPath();
|
context.beginPath();
|
||||||
if(this.status<3){
|
if(stage.status<3){
|
||||||
if(this.times%2){
|
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);
|
context.arc(this.x,this.y,this.width/2,(.5*this.orientation+.20)*Math.PI,(.5*this.orientation-.20)*Math.PI,false);
|
||||||
}else{
|
}else{
|
||||||
@ -284,43 +283,51 @@
|
|||||||
type:2,
|
type:2,
|
||||||
frames:10,
|
frames:10,
|
||||||
speed:1,
|
speed:1,
|
||||||
|
timeout:Math.floor(Math.random()*120),
|
||||||
update:function(){
|
update:function(){
|
||||||
if(!this.coord.offset){
|
console.log(this.timeout);
|
||||||
var new_map = JSON.parse(JSON.stringify(map.data));
|
if(!this.timeout){
|
||||||
var items = stage.getItemsByType(2);
|
if(!this.coord.offset){
|
||||||
var index = this.index;
|
var new_map = JSON.parse(JSON.stringify(map.data));
|
||||||
items.forEach(function(item){
|
var items = stage.getItemsByType(2);
|
||||||
if(item.index!=index){
|
var index = this.index;
|
||||||
new_map[item.coord.y][item.coord.x]=1;
|
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){ //是否转变方向
|
if(this.vector){
|
||||||
this.coord.x = this.vector.x;
|
this.x += this.speed*_COS[this.orientation];
|
||||||
this.coord.y = this.vector.y;
|
this.y += this.speed*_SIN[this.orientation];
|
||||||
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.x += this.speed*_COS[this.orientation];
|
|
||||||
this.y += this.speed*_SIN[this.orientation];
|
|
||||||
},
|
},
|
||||||
draw:function(context){
|
draw:function(context){
|
||||||
context.fillStyle = this.color;
|
context.fillStyle = this.color;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user