From ab45455c7b457b48ce5bd3df8a26bfc7367ce9ba Mon Sep 17 00:00:00 2001 From: mumuy Date: Wed, 4 May 2016 21:45:51 +0800 Subject: [PATCH] =?UTF-8?q?updated=20=E6=B7=BB=E5=8A=A0=E8=83=BD=E9=87=8F?= =?UTF-8?q?=E8=B1=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game.js | 83 +++++++++++----- index.js | 291 +++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 241 insertions(+), 133 deletions(-) diff --git a/game.js b/game.js index 1bc59d9..4a2747a 100644 --- a/game.js +++ b/game.js @@ -59,7 +59,7 @@ function Game(id,params){ height:20, //高 type:0, //对象类型,0表示普通对象(不与地图绑定),1表示玩家控制对象,2表示程序控制对象 color:'#F00', //标识颜色 - status:1, //对象状态,0表示隐藏,1表示正常,2表示暂停 + status:1, //对象状态,0表示未激活/结束,1表示正常,2表示暂停,3表示临时,4表示异常 orientation:0, //当前定位方向,0表示右,1表示下,2表示左,3表示上 speed:0, //移动速度 //地图相关 @@ -108,6 +108,8 @@ function Game(id,params){ stage:null, //布景 x_length:0, //二维数组x轴长度 y_length:0, //二维数组y轴长度 + frames:1, //速度等级,内部计算器times多少帧变化一次 + times:0, //刷新画布计数(用于循环动画状态判断) update:function(){}, //更新地图数据 draw:function(){}, //绘制地图 }; @@ -148,7 +150,8 @@ function Game(id,params){ var defaults = { map:null, start:{}, - end:{} + end:{}, + type:'path' }; var options = _extend({},defaults,params); var result = []; @@ -159,30 +162,48 @@ function Game(id,params){ var y_length = options.map.length; var x_length = options.map[0].length; var steps = []; //步骤的映射 + var steps_length = 0; for(var y=y_length;y--;){ steps[y] = []; for(var x=x_length;x--;){ steps[y][x] = 0; } } - var _render = function(list){ + var _getValue = function(x,y){ //获取地图上的值 + if(options.map[y]&&typeof options.map[y][x]!='undefined'){ + return options.map[y][x]; + } + return -1; + }; + var _next = function(to){ //判定是非可走,可走放入列表 + var value = _getValue(to.x,to.y); + if(value<1){ + if(value==-1){ + to.x = (to.x+x_length)%x_length; + to.y = (to.y+y_length)%y_length; + to.change = 1; + } + if(!steps[to.y][to.x]){ + result.push(to); + } + } + }; + var _render = function(list){//找线路 var new_list = []; var next = function(from,to){ - if(!finded){ - var value = options.map[to.y]&&typeof options.map[to.y][to.x] !='undefined'?options.map[to.y][to.x]:-1; - if(value!=1){ //当前点是否可以走 - if(value==-1){ - to.x = (to.x+x_length)%x_length; - to.y = (to.y+y_length)%y_length; - to.change = 1; - } - if(to.x==options.end.x&&to.y==options.end.y){ - steps[to.y][to.x] = from; - finded = true; - }else if(!steps[to.y][to.x]){ - steps[to.y][to.x] = from; - new_list.push(to); - } + var value = _getValue(to.x,to.y); + if(value<1){ //当前点是否可以走 + if(value==-1){ + to.x = (to.x+x_length)%x_length; + to.y = (to.y+y_length)%y_length; + to.change = 1; + } + if(to.x==options.end.x&&to.y==options.end.y){ + steps[to.y][to.x] = from; + finded = true; + }else if(!steps[to.y][to.x]){ + steps[to.y][to.x] = from; + new_list.push(to); } } }; @@ -199,11 +220,18 @@ function Game(id,params){ }; _render([options.start]); if(finded){ - var current=options.end; - while(current.x!=options.start.x||current.y!=options.start.y){ - result.unshift(current); - current=steps[current.y][current.x]; - } + var current=options.end; + if(options.type=='path'){ + while(current.x!=options.start.x||current.y!=options.start.y){ + result.unshift(current); + current=steps[current.y][current.x]; + } + }else if(options.type=='next'){ + _next({x:current.x+1,y:current.y}); + _next({x:current.x,y:current.y+1}); + _next({x:current.x-1,y:current.y}); + _next({x:current.x,y:current.y-1}); + } } return result; }; @@ -211,7 +239,7 @@ function Game(id,params){ var Stage = function(params){ this._params = params||{}; this._settings = { - status:0, //布景状态,0表示未激活,1表示正常,2表示暂停,3表示中断或异常,4表示结束 + status:0, //布景状态,0表示未激活/结束,1表示正常,2表示暂停,3表示临时,4表示异常 maps:[], //地图队列 audio:[], //音频资源 images:[], //图片资源 @@ -312,7 +340,10 @@ function Game(id,params){ } if(stage.update()!=false){ //update返回false,则不绘制 if(stage.maps.length){ - stage.maps.forEach(function(map,index){ + stage.maps.forEach(function(map){ + if(!(f%map.frames)){ + map.times = f/map.frames; //计数器 + } map.update(); map.draw(_context); }); @@ -322,7 +353,7 @@ function Game(id,params){ if(!(f%item.frames)){ item.times = f/item.frames; //计数器 } - if(stage.status==1&&item.status==1){ //对象及布景状态都处于正常状态下 + if(stage.status==1&&item.status!=2){ //对象及布景状态都处于正常状态下 if(item.location){ item.coord = item.location.position2coord(item.x,item.y); } diff --git a/index.js b/index.js index cb9a13c..34d4bcd 100644 --- a/index.js +++ b/index.js @@ -104,24 +104,28 @@ })(); //游戏主程序 (function(){ - var stage = game.createStage({ + var stage,map,goods,beans,player,times; + stage = game.createStage({ update:function(){ var stage = this; - if(stage.status==1){ - var player = stage.getItemsByType(1)[0]; - var items = stage.getItemsByType(2); - items.forEach(function(item){ //物体检测 + if(stage.status==1){ //场景正常运行 + items.forEach(function(item){ var dx = item.x-player.x; var dy = item.y-player.y; - if(dx*dx+dy*dy<750){ - stage.status = 3; - stage.timeout = 30; + if(dx*dx+dy*dy<750&&item.status!=4){ //物体检测 + if(item.status==3){ + item.status = 4; + _SCORE += 10; + }else{ + stage.status = 3; + stage.timeout = 30; + } } }); - if(JSON.stringify(goods.data).indexOf(0)<0){ + if(JSON.stringify(beans.data).indexOf(0)<0){ //当没有物品的时候,进入结束画面 game.nextStage(); } - }else if(stage.status==3){ + }else if(stage.status==3){ //场景暂停状态 if(!stage.timeout){ _LIFE--; if(_LIFE){ @@ -135,7 +139,7 @@ } }); //绘制地图 - var map = stage.createMap({ + map = stage.createMap({ x:60, y:10, data:_DATA, @@ -232,17 +236,31 @@ } }); //物品地图 - var goods = stage.createMap({ + goods = { + '1,3':1, + '26,3':1, + '1,23':1, + '26,23':1 + }; + beans = stage.createMap({ x:60, y:10, data:_DATA, + frames:8, draw:function(context){ for(var j=0; jthis.coord.x){ + this.orientation = 0; + }else if(this.vector.xthis.coord.y){ + this.orientation = 1; + }else if(this.vector.y80||this.times%2?true:false; + } + if(this.status!=4){ + context.fillStyle = isSick?'#BABABA':this.color; + context.beginPath(); + context.arc(this.x,this.y,this.width*.5,0,Math.PI,true); + switch(this.times%2){ + case 0: + context.lineTo(this.x-this.width*.5,this.y+this.height*.4); + context.quadraticCurveTo(this.x-this.width*.4,this.y+this.height*.5,this.x-this.width*.2,this.y+this.height*.3); + context.quadraticCurveTo(this.x,this.y+this.height*.5,this.x+this.width*.2,this.y+this.height*.3); + context.quadraticCurveTo(this.x+this.width*.4,this.y+this.height*.5,this.x+this.width*.5,this.y+this.height*.4); + break; + case 1: + context.lineTo(this.x-this.width*.5,this.y+this.height*.3); + context.quadraticCurveTo(this.x-this.width*.25,this.y+this.height*.5,this.x,this.y+this.height*.3); + context.quadraticCurveTo(this.x+this.width*.25,this.y+this.height*.5,this.x+this.width*.5,this.y+this.height*.3); + break; + } + context.fill(); + context.closePath(); + } + context.fillStyle = '#FFF'; + if(isSick){ + context.beginPath(); + context.arc(this.x-this.width*.15,this.y-this.height*.21,this.width*.08,0,2*Math.PI,false); + context.arc(this.x+this.width*.15,this.y-this.height*.21,this.width*.08,0,2*Math.PI,false); + context.fill(); + context.closePath(); + }else{ + context.beginPath(); + context.arc(this.x-this.width*.15,this.y-this.height*.21,this.width*.12,0,2*Math.PI,false); + context.arc(this.x+this.width*.15,this.y-this.height*.21,this.width*.12,0,2*Math.PI,false); + context.fill(); + context.closePath(); + context.fillStyle = '#000'; + context.beginPath(); + context.arc(this.x-this.width*(.15-.04*_COS[this.orientation]),this.y-this.height*(.21-.04*_SIN[this.orientation]),this.width*.07,0,2*Math.PI,false); + context.arc(this.x+this.width*(.15+.04*_COS[this.orientation]),this.y-this.height*(.21-.04*_SIN[this.orientation]),this.width*.07,0,2*Math.PI,false); + context.fill(); + context.closePath(); + } + } + }); + } + items = stage.getItemsByType(2); //主角 - var player = stage.createItem({ + player = stage.createItem({ width:30, height:30, type:1, @@ -326,9 +483,17 @@ this.y -= map.size*(map.y_length-1)*_SIN[this.orientation]; } }else{ - if(!goods.get(this.coord.x,this.coord.y)){ + if(!beans.get(this.coord.x,this.coord.y)){ //吃豆 _SCORE++; - goods.set(this.coord.x,this.coord.y,1); + beans.set(this.coord.x,this.coord.y,1); + if(goods[this.coord.x+','+this.coord.y]){ //吃到能量豆 + items.forEach(function(item){ + if(item.status==1){ //如果NPC为正常状态,则置为临时状态 + item.timeout = 450; + item.status = 3; + } + }); + } } this.x += this.speed*_COS[this.orientation]; this.y += this.speed*_SIN[this.orientation]; @@ -337,13 +502,13 @@ draw:function(context){ context.fillStyle = '#FFE600'; context.beginPath(); - if(stage.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{ context.arc(this.x,this.y,this.width/2,(.5*this.orientation+.01)*Math.PI,(.5*this.orientation-.01)*Math.PI,false); } - }else{ + }else{ //玩家被吃 if(stage.timeout) { context.arc(this.x,this.y,this.width/2,(.5*this.orientation+1-.02*stage.timeout)*Math.PI,(.5*this.orientation-1+.02*stage.timeout)*Math.PI,false); } @@ -353,94 +518,6 @@ context.fill(); } }); - //NPC - for(var i=0;i<4;i++){ - stage.createItem({ - width:30, - height:30, - orientation:3, - color:_COLOR[i], - location:map, - coord:{x:12+i,y:14}, - vector:{x:12+i,y:14}, - type:2, - frames:10, - speed:1, - timeout:Math.floor(Math.random()*120), - update:function(){ - if(!this.coord.offset){ - if(!this.timeout){ - var new_map = JSON.parse(JSON.stringify(map.data).replace(/2/g,0)); - 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.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.y