
    // in loc de new Bomb , pentru performanta as putea
    // sa fac la init vreo 5 bombe, si sa le fac sa apara si sa exlpodeze si sa dispara
    // dupa cum e nevoie
var Bomb = new Class({
    Extends: DynaObject,
    initialize: function(x,y,hero){
        this.parent(GAMEUNITTYPE.BOMB);
        this.bombExplosionLength=2;
        var alignedX=x+game.standardWidth/2;
        var alignedY=y+game.standardHeight/2;
        if(alignedX%game.standardWidth!=0)
            alignedX=util.findMaximumDivider(alignedX,game.standardWidth);
        if(alignedY%game.standardHeight!=0)
            alignedY=util.findMaximumDivider(alignedY,game.standardHeight);
        this.comeIntoExistence(game.world,alignedX,alignedY);
        this.cancelFlag=this.explode.delay(game.bombTimeOut,this);
        this.hero=hero;
        game.activeBombs.push(this);
        var soundSuffix=4+Math.round(Math.random()*2);
        game.sound.playSound('bomb'+soundSuffix);
    },
    setExplosionLength: function(explosionLength){
    	this.bombExplosionLength = explosionLength;
    },
    canExplodeIntoZone: function(zone){
        return (zone.freeZone || (!zone.freeZone && !zone.hasType(GAMEUNITTYPE.WALL)));
    },
    explode: function(){
            //this.HTMLIncarnation.style.backgroundColor='#ff0000';//this.HTMLIncarnation.setStyle('background-color','#ff0000');
            this.HTMLIncarnation.style.backgroundImage="url('img/bomb-explosion2.png')";
            this.explodedZones = [];
            this.oldX=this.x;
            this.oldY=this.y;
            this.newX=this.x;this.newY=this.y;this.newWidth=game.standardWidth;this.newHeight=game.standardHeight;
            var stopMovingLeft=false;var stopMovingRight=false;var stopMovingTop=false;var stopMovingBottom=false;
            for(var i=0;i<this.bombExplosionLength;i++){
                
                var movedLeft=false;
                var movedUp=false;
                
                if(!stopMovingLeft && this.newX-game.standardWidth/2>0){
                  var leftZone = util.getZoneAtPoint(this.newX-game.standardWidth/2,this.y);
                  if(this.canExplodeIntoZone(leftZone)){
                      this.newX=this.newX-game.standardWidth;
                      this.explodedZones.push(leftZone);
                      movedLeft=true;
                      if(leftZone.hasType(GAMEUNITTYPE.FAKEWALL)){
                          stopMovingLeft=true;
                      }
                  }
                }
                if(!stopMovingRight && this.newX+(this.newWidth+(movedLeft?1:0)*game.standardWidth)+game.standardWidth/2<game.worldWidth){
                  var rightZone = util.getZoneAtPoint(this.newX+(this.newWidth+(movedLeft?1:0)*game.standardWidth)+game.standardWidth/2,this.y);
                  if(this.canExplodeIntoZone(rightZone)){
                      this.newWidth+=(movedLeft?2:1)*game.standardWidth;
                      this.explodedZones.push(rightZone);
                      if(rightZone.hasType(GAMEUNITTYPE.FAKEWALL)){
                          stopMovingRight = true;
                      }
                  }
                }
                if(!stopMovingTop && this.newY-game.standardHeight/2>0){
                  var topZone = util.getZoneAtPoint(this.x,this.newY-game.standardHeight/2);
                  if(this.canExplodeIntoZone(topZone)){
                      this.newY=this.newY-game.standardHeight;
                      this.explodedZones.push(topZone);
                      movedUp=true;
                      if(topZone.hasType(GAMEUNITTYPE.FAKEWALL)){
                          stopMovingTop=true;
                      }
                  }
                }
                if(!stopMovingBottom && this.newY+(this.newHeight+(movedUp?1:0)*game.standardHeight)+game.standardHeight/2<game.worldHeight){
                  var bottomZone = util.getZoneAtPoint(this.x,this.newY+(this.newHeight+(movedUp?1:0)*game.standardHeight)+game.standardHeight/2);
                  if(this.canExplodeIntoZone(bottomZone)){
                      this.newHeight+=(movedUp?2:1)*game.standardHeight;
                      this.explodedZones.push(bottomZone);
                      if(bottomZone.hasType(GAMEUNITTYPE.FAKEWALL)){
                          stopMovingBottom=true;
                      }
                  }
                }
            }
            var horizEffect=false;
            if(this.newX!=this.x || this.newWidth!=game.standardWidth){
              var horiz = new Fx.Morph(this.HTMLIncarnation, {duration:game.bombEffectDuration});
              horiz.addEvent('onComplete',function(){this.removeAshes();}.bind(this));
              horiz.start({
                            'width': [game.standardWidth,this.newWidth],
                            'left': [this.x,this.newX]
                          });
              horizEffect=true;
            }
            var verticalEffect=false;
            if(this.newY!=this.y || this.newHeight!=game.standardHeight){
              this.clonedBomb = this.HTMLIncarnation.clone(false);
              this.clonedBomb.injectBefore(this.HTMLIncarnation);
              var vertical = new Fx.Morph(this.clonedBomb, {duration:game.bombEffectDuration});
              if(!horizEffect)
                vertical.addEvent('onComplete',function(){this.removeAshes();}.bind(this));
              vertical.start({
                            'height': [game.standardHeight,this.newHeight],
                            'top': [this.y,this.newY]
                          });
                verticalEffect=true;
            }
            if(!horizEffect && !verticalEffect){
                this.terminate();
            }
    },
    terminate: function(){
        if($defined(this.clonedBomb)){
          this.clonedBomb.dispose();
          this.clonedBomb.destroy();
        }
        $clear(this.cancelFlag);
        util.removeElementFromArray(this,game.activeBombs);
        this.parent();
    },
    removeAshes: function(){
        this.terminate();
        this.hero.returnBomb();
        var heroHit=false;
      for(var i=0;i<this.explodedZones.length;i++){
                var elements=this.explodedZones[i].getElements();
                for(var k=0;k<elements.length;k++){
                    if(elements[k].type!=GAMEUNITTYPE.BOMB && !(elements[k].type==GAMEUNITTYPE.GOODY && elements[k].subtype==GOODYSUBTYPE.EXIT))
                      elements[k].terminate();
                  if(elements[k]==game.hero)
                      heroHit=true;
                }
            }
       if($defined(game.hero) && !heroHit){
           if(this.newX!=this.x || this.newWidth!=game.standardWidth){
           if(!(game.hero.x+game.standardWidth<=this.newX || game.hero.x>=this.newX+this.newWidth || game.hero.y+game.standardHeight<=this.oldY || game.hero.y>=this.oldY+game.standardHeight))
              game.hero.terminate();
           }
       }
       if($defined(game.hero) && !heroHit){
              if(this.newY!=this.y || this.newHeight!=game.standardHeight){
                if(!(game.hero.y+game.standardHeight<=this.newY || game.hero.y>=this.newY+this.newHeight || game.hero.x+game.standardWidth<=this.oldX || game.hero.x>=this.oldX+game.standardWidth))
                    game.hero.terminate();
              }
          }
       for(var i=0;i<game.heroes.length;i++){
           var hero = game.heroes[i];
           if($defined(hero)){
             if(this.newX!=this.x || this.newWidth!=game.standardWidth){
             if(!(hero.x+game.standardWidth<=this.newX || hero.x>=this.newX+this.newWidth || hero.y+game.standardHeight<=this.oldY || hero.y>=this.oldY+game.standardHeight))
                hero.terminate();
             }
            }
            if($defined(hero)){
              if(this.newY!=this.y || this.newHeight!=game.standardHeight){
                if(!(hero.y+game.standardHeight<=this.newY || hero.y>=this.newY+this.newHeight || hero.x+game.standardWidth<=this.oldX || hero.x>=this.oldX+game.standardWidth))
                    hero.terminate();
              }
            }
       }
    }
});
    
