// inteligent bad guy , really bad 
var IBadGuy = new Class({
    Extends: BadGuy,
    initialize: function(){
        this.parent();
        this.HTMLIncarnation.setStyle('background-image',"url('img/ibadguy.png')");
        this.points = 100;
        this.pathToHero=[];
        this.delayBeat=-1; // no delay against the hero
        this.lookForHeroCounter=0;
        this.lookForHeroInterval=Math.round(3000/game.speed); // how many times to move my body before i look again where the hero is; 3000 is 3 seconds
    },
    badGuyMove: function(){
        if(parseInt(Math.sqrt(Math.pow(this.myZone.x-game.hero.myZone.x,2)+Math.pow(this.myZone.y-game.hero.myZone.y,2)))>400){
            this.parent();
            return;
        }
        if(this.hitWorldBoundary==true){
            this.sense=this.sense*(-1);
            this.hitWorldBoundary=false;
        }
        if(this.hitTheWall && this.pathToHero.length==0){
            /////// if we hit the wall we turn back or we change direction
            var randy=Math.round(Math.random()*8);
            if(randy>3){
                this.sense=this.sense*(-1); /// change the sense in the same direction - this is preferred
            } else {
                if(this.currentDirection=='left')
                    this.currentDirection='top';
                else
                    this.currentDirection='left';
            }
        } else {
            if(this.lookForHeroCounter>this.lookForHeroInterval){
                this.lookForHeroCounter=0;
                var aStarPath = new AStarPath(this.myZone,game.hero.myZone);
                this.pathToHero = aStarPath.computeAStarPath();
                this.lookForHeroInterval=Math.round(3000/game.speed)+Math.round(Math.random() * 30);
                this.nextZone=null;
                this.nextZoneIndex=0;
            }
            this.lookForHeroCounter++;
            if(this.pathToHero.length>0){
	          if(!$defined(this.nextZone)){
                  this.nextZone = this.pathToHero[0].zone;
                  this.nextZoneIndex=0;
              } else {
                  if(this.nextZone.x==this.myZone.x && this.nextZone.y==this.myZone.y){
                      this.nextZoneIndex++;
                      if($defined(this.pathToHero[this.nextZoneIndex]))
                        this.nextZone=this.pathToHero[this.nextZoneIndex].zone;
                  } else {
                      if(this.nextZone.x==this.myZone.x){
                          var modulo=this.x%game.standardWidth;
                          var maxDivider=parseInt(this.x/game.standardWidth);
                          //if(this.currentDirection=='left' && (this.x>(4/10)*game.standardWidth && this.x<(6/10)*game.standardWidth)){
                          if(this.currentDirection=='left'){
                              if( maxDivider%2==0 && !(modulo>(4/10)*game.standardWidth && modulo<(6/10)*game.standardWidth)){
                                  if(this.hitTheWall)
                                        this.currentDirection='top';
                              } else {
                                  this.currentDirection='top';
                                  this.sense = 1*(this.nextZone.y>this.myZone.y?1:-1);
                              }
                          } else
                                  this.sense = 1*(this.nextZone.y>this.myZone.y?1:-1);
                      }
                      if(this.nextZone.y==this.myZone.y){
                          var modulo=this.y%game.standardHeight;
                          var maxDivider=parseInt(this.y/game.standardHeight);
                          //if(this.currentDirection=='top' && (this.y>(4/10)*game.standardHeight && this.y<(6/10)*game.standardHeight)){
                          if(this.currentDirection=='top'){
                              if( maxDivider%2==0 && !(modulo>(4/10)*game.standardHeight && modulo<(6/10)*game.standardHeight)){
                                      if(this.hitTheWall)
                                        this.currentDirection='left';
                              } else {
                                  this.currentDirection='left';
                                  this.sense = 1*(this.nextZone.x>this.myZone.x?1:-1);
                                }
                          } else
                                  this.sense = 1*(this.nextZone.x>this.myZone.x?1:-1);
                      }
                      if(this.nextZone.y!=this.myZone.y && this.nextZone.x!=this.myZone.x){

                      }
                  }
              }
        }
        }
    }
});

