NPC跟踪行为
This commit is contained in:
parent
7e8d45f058
commit
2b3d86f979
49
game.js
49
game.js
@ -29,7 +29,7 @@ function Game(id,options){
|
|||||||
width:20, //宽
|
width:20, //宽
|
||||||
height:20, //高
|
height:20, //高
|
||||||
type:0, //对象类型,0表示普通对象(不与地图绑定),1表示玩家控制对象,2表示程序控制对象
|
type:0, //对象类型,0表示普通对象(不与地图绑定),1表示玩家控制对象,2表示程序控制对象
|
||||||
status:1, //对象状态,1表示正常,0表示隐藏
|
status:1, //对象状态,1表示正常,0表示隐藏,2表示暂停
|
||||||
orientation:0, //当前定位方向,0表示上,1表示右,2表示下,3表示左
|
orientation:0, //当前定位方向,0表示上,1表示右,2表示下,3表示左
|
||||||
vector:{}, //目标坐标
|
vector:{}, //目标坐标
|
||||||
coord:{}, //如果对象与地图绑定,获得坐标值
|
coord:{}, //如果对象与地图绑定,获得坐标值
|
||||||
@ -109,8 +109,8 @@ function Game(id,options){
|
|||||||
Map.prototype.finder = function(param){
|
Map.prototype.finder = function(param){
|
||||||
var defaults = {
|
var defaults = {
|
||||||
map:this.data,
|
map:this.data,
|
||||||
start:[0,0],
|
start:{},
|
||||||
end:[0,0]
|
end:{}
|
||||||
};
|
};
|
||||||
var options = (function(target, params) {
|
var options = (function(target, params) {
|
||||||
for (var prop in params) {
|
for (var prop in params) {
|
||||||
@ -119,40 +119,39 @@ function Game(id,options){
|
|||||||
return target;
|
return target;
|
||||||
})(defaults,param);
|
})(defaults,param);
|
||||||
var result = [];
|
var result = [];
|
||||||
if(options.map[options.start[0]][options.start[1]]||options.map[options.end[0]][options.end[1]]){ //当起点或终点设置在墙上
|
if(options.map[options.start.y][options.start.x]||options.map[options.end.y][options.end.x]){ //当起点或终点设置在墙上
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
var finded = false;
|
var finded = false;
|
||||||
var length_x = options.map.length;
|
var _self = this;
|
||||||
var steps = new Array(length_x); //步骤的映射
|
var y_length = options.map.length;
|
||||||
for(var x=length_x;x--;){
|
var x_length = options.map[0].length;
|
||||||
var length_y = options.map[x].length;
|
var steps = []; //步骤的映射
|
||||||
steps[x] = new Array(length_y);
|
for(var y=y_length;y--;){
|
||||||
for(var y=length_y;y--;){
|
steps[y] = [];
|
||||||
steps[x][y] = 0;
|
for(var x=x_length;x--;){
|
||||||
|
steps[y][x] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var _render = function(list){
|
var _render = function(list){
|
||||||
var new_list = [];
|
var new_list = [];
|
||||||
var next = function(from,to){
|
var next = function(from,to){
|
||||||
var x = to[0],y = to[1];
|
if(_self.get(to.x,to.y)==0&&!finded){ //当前点是否可以走
|
||||||
if(typeof steps[x]!= 'undefined'&&typeof steps[x][y] != 'undefined'&&!options.map[x][y]&&!finded){ //当前点是否可以走
|
if(to.x==options.end.x&&to.y==options.end.y){
|
||||||
if(x==options.end[0]&&y==options.end[1]){
|
steps[to.y][to.x] = from;
|
||||||
steps[x][y] = from;
|
|
||||||
finded = true;
|
finded = true;
|
||||||
}else if(!steps[x][y]){
|
}else if(!steps[to.y][to.x]){
|
||||||
steps[x][y] = from;
|
steps[to.y][to.x] = from;
|
||||||
new_list.push(to);
|
new_list.push(to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
for(var i=0,len=list.length;i<len;i++){
|
for(var i=0,len=list.length;i<len;i++){
|
||||||
var current = list[i];
|
var current = list[i];
|
||||||
var x = current[0],y=current[1];
|
next(current,{y:current.y+1,x:current.x});
|
||||||
next(current,[x+1,y]);
|
next(current,{y:current.y,x:current.x+1});
|
||||||
next(current,[x,y+1]);
|
next(current,{y:current.y-1,x:current.x});
|
||||||
next(current,[x-1,y]);
|
next(current,{y:current.y,x:current.x-1});
|
||||||
next(current,[x,y-1]);
|
|
||||||
}
|
}
|
||||||
if(!finded&&new_list.length){
|
if(!finded&&new_list.length){
|
||||||
_render(new_list);
|
_render(new_list);
|
||||||
@ -161,9 +160,9 @@ function Game(id,options){
|
|||||||
_render([options.start]);
|
_render([options.start]);
|
||||||
if(finded){
|
if(finded){
|
||||||
var current=options.end;
|
var current=options.end;
|
||||||
while(current[0]!=options.start[0]||current[1]!=options.start[1]){
|
while(current.x!=options.start.x||current.y!=options.start.y){
|
||||||
result.unshift(current);
|
result.unshift(current);
|
||||||
current=steps[current[0]][current[1]];
|
current=steps[current.y][current.x];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
37
index.js
37
index.js
@ -348,10 +348,39 @@
|
|||||||
type:2,
|
type:2,
|
||||||
speed:10,
|
speed:10,
|
||||||
update:function(){
|
update:function(){
|
||||||
this.path = map.finder({
|
if(!this.coord.offset){
|
||||||
start:[this.coord.y,this.coord.x],
|
this.path = map.finder({
|
||||||
end:[player.coord.y,player.coord.x]
|
start:this.coord,
|
||||||
});
|
end:player.coord
|
||||||
|
});
|
||||||
|
if(this.path.length){
|
||||||
|
this.vector = this.path[0];
|
||||||
|
}
|
||||||
|
if(this.vector.x>this.coord.x){
|
||||||
|
this.orientation = 1;
|
||||||
|
}else if(this.vector.x<this.coord.x){
|
||||||
|
this.orientation = 3;
|
||||||
|
}else if(this.vector.y>this.coord.y){
|
||||||
|
this.orientation = 2;
|
||||||
|
}else if(this.vector.y<this.coord.y){
|
||||||
|
this.orientation = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var steps = 1;
|
||||||
|
switch(this.orientation){
|
||||||
|
case 0:
|
||||||
|
this.y-=steps;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
this.x+=steps;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
this.y+=steps;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
this.x-=steps;
|
||||||
|
break;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
draw:function(context){
|
draw:function(context){
|
||||||
context.fillStyle = '#F00';
|
context.fillStyle = '#F00';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user