From 6a170fabcbe9c261296b1fdcc102d10fe8284e80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=B5=A9=E4=B9=90?= Date: Mon, 11 Jan 2016 08:15:26 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9A=8F=E6=9C=BA=E5=87=BA=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game.js | 14 +++++------ index.js | 77 ++++++++++++++++++++++++++++++-------------------------- 2 files changed, 49 insertions(+), 42 deletions(-) diff --git a/game.js b/game.js index 00e5505..15af67f 100644 --- a/game.js +++ b/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); diff --git a/index.js b/index.js index 5d9937e..10bdaf7 100644 --- a/index.js +++ b/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.xthis.coord.y){ + this.orientation = 1; + }else if(this.vector.ythis.coord.x){ - this.orientation = 0; - }else if(this.vector.xthis.coord.y){ - this.orientation = 1; - }else if(this.vector.y