var HalfJueguego={
pinpong:function(){
this.config={
gameCampo:{alto:300,ancho:400},
ids_game:{barra_player:'player',enemigo:'enemigo',bola:'bola',campo:'game'},
events:{onPoint:function(){},onMoveBola:function(){},onChocaBola:function(){}},
dificultad:'invencible'
};
this.dificultades={facil:[1,.7],normal:[1,.9],dificil:[1.5,1.2],muydificil:[1.5,2],invencible:[3,3]};
this.runGame=true;
this.gameInterval=false;
this.angulo=1;
this.subir=false;
this.bajar=false;
this.aplayer=true;
this.keykdown={kup:false,kdown:false};
this.addEvent=function(type,fn) {
if (this.addEventListener){
this.addEventListener( type, fn, false );
}else if(this.attachEvent){
var elwin=this;
var f=function(){fn.call(elwin,window.event);};
this.attachEvent('on'+type,f);
this[fn.toString()+type]=f;
}else{
this['on'+type]=fn;
}
return this;
};
this.set={
elwin:this,
dificultad:function(dif){
var d=this.elwin.dificultades[dif];
this.elwin.config.dificultad=!d?'facil':dif;
},
pause:function(p){
this.elwin.runGame=p;
}
};
this.getPosision=function(el){
var valueT=0,valueL=0,elm=el;
do{
valueL+=parseInt(elm.offsetLeft);
valueT+=parseInt(elm.offsetTop);
elm=elm.offsetParent;
}while(elm);
return [valueL,valueT];
};
this.chocaCon=function(elemento,achocar){
var pos_el=this.getPosision(elemento),pos_ac=this.getPosision(achocar);
for(var i=pos_ac[1],total_i=pos_ac[1]+achocar.offsetHeight;i<total_i;i++){
if(i==pos_el[1]){
for(var j=pos_ac[0],total_j=parseInt(pos_ac[0])+parseInt(achocar.offsetWidth);j<total_j;j++){
if(j==pos_el[0])
return [i,j,pos_ac];
}
}
}
return false;
};
this.mover_bola=function(lado,pos){
var bola_dom=document.getElementById(this.config.ids_game.bola),enemigo=document.getElementById(this.config.ids_game.enemigo),barra_player=document.getElementById(this.config.ids_game.barra_player);
if(!lado){
bola_dom.style.top=this.sube?(parseInt(bola_dom.style.top)-1)+"px":(this.bajar?parseInt(bola_dom.style.top)+1+"px":bola_dom.style.top);
bola_dom.style.left=this.aplayer?parseInt(bola_dom.style.left)+this.angulo+"px":parseInt(bola_dom.style.left)-this.angulo+"px";
this.config.events.onMoveBola([this.sube,this.bajar,this.angulo]);
return;
}else if(lado=='pared-top'){
this.sube=false;
this.bajar=true;
}else if(lado=='pared-bottom'){
this.sube=true;
this.bajar=false;
}else if(lado=='enemigo'){
this.aplayer=true;
if(this.angulo!=1){
if(pos[0]-pos[2][1]>barra_player.offsetHeight/2){
this.sube=false;
this.bajar=true;
}else{
this.sube=true;
this.bajar=false;
}
}
}else if(lado=='player'){
this.aplayer=false;
if(this.keykdown.kup||this.keykdown.kdown){
this.angulo=this.rand(2,7);
this[this.keykdown.kup?'sube':'bajar']=false;
this[this.keykdown.kup?'bajar':'sube']=true;
return;
}
if(this.angulo!=1){
if(pos[0]-pos[2][1]>barra_player.offsetHeight/2){
this.sube=false;
this.bajar=true;
}else{
this.sube=true;
this.bajar=false;
}
}
}
this.config.events.onChocaBola(lado);
};
this.rand=function(lo,hi){
return Math.floor((Math.random()*(hi-lo))+lo);
};
this.newGame=function(){
var bola_dom=document.getElementById(this.config.ids_game.bola),barra_player=document.getElementById(this.config.ids_game.barra_player),enemigo=document.getElementById(this.config.ids_game.enemigo);
bola_dom.style.top=(this.config.gameCampo.alto/2)+"px";
bola_dom.style.left=(this.config.gameCampo.ancho/2)+"px";
barra_player.style.top=(this.config.gameCampo.alto/2-barra_player.offsetHeight/2)+"px";
barra_player.style.left=(this.config.gameCampo.ancho-barra_player.offsetWidth*2)+"px";
enemigo.style.top=(this.config.gameCampo.alto/2-enemigo.offsetHeight/2)+"px";
enemigo.style.left=enemigo.offsetWidth*2+"px";
this.angulo=1;
this.sube=false;
this.bajar=false;
this.aplayer=true;
};
this.retry=function(){
this.newGame();
};
this.enemigo_mover=function(){
if(this.aplayer)
return;
var bola_dom=document.getElementById(this.config.ids_game.bola),bola_pos=this.getPosision(bola_dom),enemigo=document.getElementById(this.config.ids_game.enemigo),enemigo_pos=this.getPosision(enemigo),d=this.dificultades[this.config.dificultad];
if(bola_pos[1]>(enemigo_pos[1]+enemigo.offsetHeight/2)){
if(parseInt(enemigo.style.top)<this.config.gameCampo.alto-enemigo.offsetHeight)
enemigo.style.top=parseInt(enemigo.style.top)+d[0]+"px";
}else{
if(parseInt(enemigo.style.top)>0)
enemigo.style.top=parseInt(enemigo.style.top)-d[1]+"px";
}
};
this.init=function(){
var elwin=this;
if(this.gameInterval)
clearInterval(this.gameInterval);
this.newGame();
this.addEvent.call(document,'keyup',function(){
elwin.keykdown={kkup:false,kkdown:false};
});
this.addEvent.call(document,'keydown',function(e){
var e=!e?event:e,keyCode=e.keyCode||e.which,barra_player=document.getElementById(elwin.config.ids_game.barra_player);
if(keyCode==40){
elwin.keykdown={kup:true,kdown:false};
}else if(keyCode==38){
elwin.keykdown={kup:false,kdown:true};
}
});
this.gameInterval=setInterval(function(){
if(!elwin.runGame)
return;
var bola_dom=document.getElementById(elwin.config.ids_game.bola),bola_pos=elwin.getPosision(bola_dom),game_dom=document.getElementById(elwin.config.ids_game.campo),game_pos=elwin.getPosision(game_dom),barra_player=document.getElementById(elwin.config.ids_game.barra_player),enemigo=document.getElementById(elwin.config.ids_game.enemigo);
elwin.mover_bola();
elwin.enemigo_mover();
if(elwin.keykdown.kup || elwin.keykdown.kdown){
if(elwin.keykdown.kup){
if(parseInt(barra_player.style.top)<elwin.config.gameCampo.alto-barra_player.offsetHeight)
barra_player.style.top=parseInt(barra_player.style.top)+1+"px";
}else if(elwin.keykdown.kdown){
if(parseInt(barra_player.style.top)>0)
barra_player.style.top=parseInt(barra_player.style.top)-1+"px";
}
}
if(elwin.config.gameCampo.ancho-barra_player.offsetWidth*6+game_pos[0]<bola_pos[0]){
var choca_barra_player=elwin.chocaCon(bola_dom,barra_player);
if(choca_barra_player[0])
return elwin.mover_bola('player',choca_barra_player);
}else if(enemigo.offsetWidth*6+game_pos[0]>bola_pos[0]){
var choca_barra_enemigo=elwin.chocaCon(bola_dom,enemigo);
if(choca_barra_enemigo[0])
return elwin.mover_bola('enemigo',choca_barra_enemigo);
}
if(bola_pos[1]<=game_pos[1])
elwin.mover_bola('pared-top',bola_pos,0);
else if(bola_pos[1]>=elwin.config.gameCampo.alto+game_pos[1])
elwin.mover_bola('pared-bottom',bola_pos,0);
else if(bola_pos[0]-game_pos[0]<=0){
elwin.config.events.onPoint('humano');
return elwin.retry();
}else if(bola_pos[0]>=elwin.config.gameCampo.ancho+game_pos[0]){
elwin.config.events.onPoint('computadora');
return elwin.retry();
}
},10);
};
}
};