﻿//*******************************************************************************
// Author: Harry Guillermo Mendoza
// Email: hguillermo@yahoo.com
// All copyrights are reserved
// Date: 08/22/2008
// Web site: http://www.harryguillermo.com
//*******************************************************************************
var pacmans=[];function InitPacmans(){pacmans.push(new Pacman('pacman','yellow',14,23,4))};function Pacman(pacmanName,color,x,y,numLifes){this.tickAcum=0;this.pacmanTick=120;this.numLifes=numLifes;this.xDeltaPacmanTick=16;this.yDeltaPacmanTick=16;this.PacmanName=pacmanName;this.iniX=x;this.iniY=y;this.x=x;this.y=y;this.currentDir='left';this.isMouthOpen=true;this.SetStartPosition(this.x,this.y);var div=document.createElement('div');div.id=this.PacmanName;div.style.position='absolute';div.style.width=32;div.style.height=32;div.style.top=this.yScreen+'px';div.style.left=this.xScreen+'px';div.style.border=0;div.style.zIndex=920;this.arrImages=[];this.arrDirection=['u','d','r','l'];this.arrState=['o','c'];for(var i=0;i<this.arrDirection.length;i++){for(var j=0;j<this.arrState.length;j++){var img=this.CreateImageElement(div,this.arrDirection[i],this.arrState[j]);this.arrImages.push(img)}}this.lastImageShowed=this.arrImages[0];var divContent=document.getElementById('content');divContent.appendChild(div);this.Pacman=div};Pacman.prototype.CreateImageElement=function(container,direction,state){var source='pacman/pacman/pac_'+state+'_'+direction+'.gif';var img=this.CreateImage(source);container.appendChild(img);return img};Pacman.prototype.CreateImage=function(source){var img=document.createElement('img');img.border=0;img.src=source;img.style.position='absolute';img.style.width=32;img.style.height=32;img.style.top=0;img.style.left=0;img.style.visibility='hidden';return img};Pacman.prototype.SetTick=function(value){this.pacmanTick=value};Pacman.prototype.GetTick=function(){return this.pacmanTick};Pacman.prototype.GetX=function(){return this.x};Pacman.prototype.GetY=function(){return this.y};Pacman.prototype.SetCurrentLifes=function(value){this.numLifes=value};Pacman.prototype.UpdateCurrentLifes=function(value){this.numLifes+=value};Pacman.prototype.AddCurrentLifes=function(){this.numLifes--};Pacman.prototype.GetCurrentLifes=function(){return this.numLifes};Pacman.prototype.SetDefaultPosition=function(){this.xScreen=(this.iniX-1)*this.xDeltaPacmanTick+16;this.yScreen=(this.iniY-1)*this.yDeltaPacmanTick+72;this.x=this.iniX;this.y=this.iniY;this.currentDir='left';this.isMouthOpen=true;this.Pacman.style.top=this.yScreen+'px';this.Pacman.style.left=this.xScreen+'px';var img=this.GetImageState(this.currentDir);img.style.visibility='visible'};Pacman.prototype.SetStartPosition=function(x,y){this.xScreen=(x-1)*this.xDeltaPacmanTick+16;this.yScreen=(y-1)*this.yDeltaPacmanTick+72;this.x=x;this.y=y};Pacman.prototype.Hide=function(){this.Pacman.style.visibility='hidden';this.lastImageShowed.style.visibility='hidden'};Pacman.prototype.Show=function(){this.Pacman.style.visibility='visible'};Pacman.prototype.Move=function(){var dx=0,dy=0;var difx=this.xDeltaPacmanTick;var dify=this.yDeltaPacmanTick;if(this.x==0&&this.y==14){this.x=27;this.y=14;this.xScreen=(this.x-1)*this.xDeltaPacmanTick+16;this.yScreen=(this.y-1)*this.yDeltaPacmanTick+72}else if(this.x==27&&this.y==14){this.x=0;this.y=14;this.xScreen=(this.x-1)*this.xDeltaPacmanTick+16;this.yScreen=(this.y-1)*this.yDeltaPacmanTick+72}else{switch(this.currentDir){case'up':dx=0;dy=-1*dify;break;case'down':dx=0;dy=dify;break;case'left':dx=-1*dify;dy=0;break;case'right':dx=dify;dy=0;break}this.xScreen+=dx;this.yScreen+=dy}this.Pacman.style.top=this.yScreen+'px';this.Pacman.style.left=this.xScreen+'px';var img=this.GetImageState(this.currentDir);img.style.visibility='visible';this.isMouthOpen=!this.isMouthOpen};Pacman.prototype.GetImageState=function(currentDir){this.lastImageShowed.style.visibility='hidden';var currentImage='';switch(currentDir){case'up':currentImage=this.isMouthOpen?this.arrImages[0]:this.arrImages[0+1];break;case'down':currentImage=this.isMouthOpen?this.arrImages[2]:this.arrImages[2+1];break;case'right':currentImage=this.isMouthOpen?this.arrImages[4]:this.arrImages[4+1];break;case'left':currentImage=this.isMouthOpen?this.arrImages[6]:this.arrImages[6+1];break}this.lastImageShowed=currentImage;return currentImage};Pacman.prototype.Update=function(tick){this.tickAcum+=tick;if(this.tickAcum<this.pacmanTick)return;this.tickAcum=0;var nextStep='';var newX=0;var newY=0;var x=this.x;var y=this.y;var tempCurrent=this.currentDir;this.currentDir=input.GetLastKeyPressed();switch(this.currentDir){case'up':nextStep=mazze[y-1][x];newX=x;newY=y-1;break;case'down':nextStep=mazze[y+1][x];newX=x;newY=y+1;break;case'left':nextStep=mazze[y][x-1];newX=x-1;newY=y;break;case'right':nextStep=mazze[y][x+1];newX=x+1;newY=y;break}if(nextStep=='..'){this.x=newX;this.y=newY;this.Move()}else{this.currentDir=tempCurrent;switch(this.currentDir){case'up':nextStep=mazze[y-1][x];newX=x;newY=y-1;break;case'down':nextStep=mazze[y+1][x];newX=x;newY=y+1;break;case'left':nextStep=mazze[y][x-1];newX=x-1;newY=y;break;case'right':nextStep=mazze[y][x+1];newX=x+1;newY=y;break}if(nextStep=='..'){this.x=newX;this.y=newY;this.Move()}return}};