updated 添加能量豆功能
This commit is contained in:
parent
5f6c155b3a
commit
ab45455c7b
83
game.js
83
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);
|
||||
}
|
||||
|
291
index.js
291
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; j<this.y_length; j++){
|
||||
for(var i=0; i<this.x_length; i++){
|
||||
if(!this.get(i,j)){
|
||||
var pos = this.coord2position(i,j);
|
||||
context.fillStyle = "#F5F5DC";
|
||||
context.fillRect(pos.x-2,pos.y-2,4,4);
|
||||
if(goods[i+','+j]){
|
||||
context.beginPath();
|
||||
context.arc(pos.x,pos.y,3+this.times%2,0,2*Math.PI,true);
|
||||
context.fill();
|
||||
context.closePath();
|
||||
}else{
|
||||
context.fillRect(pos.x-2,pos.y-2,4,4);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -288,7 +306,7 @@
|
||||
height:30,
|
||||
draw:function(context){
|
||||
for(var i=0;i<_LIFE-1;i++){
|
||||
var x=this.x+36*i,y=this.y;
|
||||
var x=this.x+40*i,y=this.y;
|
||||
context.fillStyle = '#FFE600';
|
||||
context.beginPath();
|
||||
context.arc(x,y,this.width/2,.15*Math.PI,-.15*Math.PI,false);
|
||||
@ -298,8 +316,147 @@
|
||||
}
|
||||
}
|
||||
});
|
||||
//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(){
|
||||
var new_map;
|
||||
if(this.status==3&&!this.timeout){
|
||||
this.status = 1;
|
||||
}
|
||||
if(!this.coord.offset){ //到达坐标中心时计算
|
||||
if(this.status==1){
|
||||
if(!this.timeout){ //定时器
|
||||
new_map = JSON.parse(JSON.stringify(map.data).replace(/2/g,0));
|
||||
var index = this.index;
|
||||
items.forEach(function(item){
|
||||
if(item.index!=index&&item.status==1){ //NPC将其它所有还处于正常状态的NPC当成一堵墙
|
||||
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];
|
||||
}
|
||||
}
|
||||
}else if(this.status==3){
|
||||
new_map = JSON.parse(JSON.stringify(map.data).replace(/2/g,0));
|
||||
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:player.coord,
|
||||
end:this.coord,
|
||||
type:'next'
|
||||
});
|
||||
if(this.path.length){
|
||||
this.vector = this.path[Math.floor(Math.random()*this.path.length)];
|
||||
}
|
||||
}else if(this.status==4){
|
||||
new_map = JSON.parse(JSON.stringify(map.data).replace(/2/g,0));
|
||||
this.path = map.finder({
|
||||
map:new_map,
|
||||
start:this.coord,
|
||||
end:this._params.coord
|
||||
});
|
||||
if(this.path.length){
|
||||
this.vector = this.path[0];
|
||||
}else{
|
||||
this.status = 1;
|
||||
}
|
||||
}
|
||||
//是否转变方向
|
||||
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.x += this.speed*_COS[this.orientation];
|
||||
this.y += this.speed*_SIN[this.orientation];
|
||||
},
|
||||
draw:function(context){
|
||||
var isSick = false;
|
||||
if(this.status==3){
|
||||
isSick = this.timeout>80||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.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){
|
||||
context.fillStyle = 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';
|
||||
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();
|
||||
}
|
||||
});
|
||||
}
|
||||
//事件绑定
|
||||
stage.bind('keydown',function(e){
|
||||
switch(e.keyCode){
|
||||
|
Loading…
x
Reference in New Issue
Block a user