From 2b3d86f97904a3dea16b4742c6d611969ad5a01f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=B5=A9=E4=B9=90?= Date: Wed, 6 Jan 2016 00:29:33 +0800 Subject: [PATCH] =?UTF-8?q?NPC=E8=B7=9F=E8=B8=AA=E8=A1=8C=E4=B8=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game.js | 49 ++++++++++++++++++++++++------------------------- index.js | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/game.js b/game.js index 65d2be0..03fd748 100644 --- a/game.js +++ b/game.js @@ -29,7 +29,7 @@ function Game(id,options){ width:20, //宽 height:20, //高 type:0, //对象类型,0表示普通对象(不与地图绑定),1表示玩家控制对象,2表示程序控制对象 - status:1, //对象状态,1表示正常,0表示隐藏 + status:1, //对象状态,1表示正常,0表示隐藏,2表示暂停 orientation:0, //当前定位方向,0表示上,1表示右,2表示下,3表示左 vector:{}, //目标坐标 coord:{}, //如果对象与地图绑定,获得坐标值 @@ -109,8 +109,8 @@ function Game(id,options){ Map.prototype.finder = function(param){ var defaults = { map:this.data, - start:[0,0], - end:[0,0] + start:{}, + end:{} }; var options = (function(target, params) { for (var prop in params) { @@ -119,40 +119,39 @@ function Game(id,options){ return target; })(defaults,param); 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 []; } var finded = false; - var length_x = options.map.length; - var steps = new Array(length_x); //步骤的映射 - for(var x=length_x;x--;){ - var length_y = options.map[x].length; - steps[x] = new Array(length_y); - for(var y=length_y;y--;){ - steps[x][y] = 0; + var _self = this; + var y_length = options.map.length; + var x_length = options.map[0].length; + var steps = []; //步骤的映射 + for(var y=y_length;y--;){ + steps[y] = []; + for(var x=x_length;x--;){ + steps[y][x] = 0; } } var _render = function(list){ var new_list = []; var next = function(from,to){ - var x = to[0],y = to[1]; - if(typeof steps[x]!= 'undefined'&&typeof steps[x][y] != 'undefined'&&!options.map[x][y]&&!finded){ //当前点是否可以走 - if(x==options.end[0]&&y==options.end[1]){ - steps[x][y] = from; + if(_self.get(to.x,to.y)==0&&!finded){ //当前点是否可以走 + if(to.x==options.end.x&&to.y==options.end.y){ + steps[to.y][to.x] = from; finded = true; - }else if(!steps[x][y]){ - steps[x][y] = from; + }else if(!steps[to.y][to.x]){ + steps[to.y][to.x] = from; new_list.push(to); } } - }; + }; for(var i=0,len=list.length;ithis.coord.x){ + this.orientation = 1; + }else if(this.vector.xthis.coord.y){ + this.orientation = 2; + }else if(this.vector.y