var fireArcherLegData = {
    arrows : null,
    archers : null,
    isFire : false
}

function ArrowLeg () {
    this.sprite = null;
    this.orientation = null;
    this.cible = new Point();
    this.speed = 3;
    this.maxSpeed = 3;
    this.hit = false;
}

function prepareFireArcherLeg (columns) {
    fireArcherLegData.archers = new Array();
    fireArcherLegData.arrows = new Array();
    mLog('prepare arrows Legendary');
    for (i=0; i<columns.length; i++) {
        if (columns[i].archer != null && columns[i].archer.lvl == 3 && columns[i].archer.alive) {
            fireArcherLegData.archers.push(columns[i].archer);
            createArrowLeg(columns[i].archer);
        }
    }
    
    fireArcherLegData.isFire = true;
    if (fireArcherLegData.archers.length != 0)
        sound_arrow_leg.play();
}


function createArrowLeg(archer) {
    arrow = new Arrow();
    start = new Point();
    start.x = archer.sprite.x+20;
    start.y = archer.sprite.y+20;
    arrow.sprite = game.add.sprite(start.x,start.y,'Arrow3');
    arrow.sprite.anchor.setTo(0.5, 0.5);
    arrow.cible.x = Math.floor((Math.random() * (BoundTarget.br_x - BoundTarget.tl_x)) + BoundTarget.tl_x);
    arrow.cible.y = Math.floor((Math.random() * (BoundTarget.br_y - BoundTarget.tl_y)) + BoundTarget.tl_y);
    //game.add.sprite(arrow.cible.x, arrow.cible.y, 'debug_croix');
    oppose = arrow.cible.y - start.y;
    hypotenuse = Math.sqrt(Math.pow(arrow.cible.x - start.x,2) + Math.pow(arrow.cible.y - start.y, 2));
    arrow.sprite.rotation = Math.asin(oppose/hypotenuse);
    if (arrow.cible.x < start.x)
        arrow.sprite.rotation = Math.PI - arrow.sprite.rotation;
    fireArcherLegData.arrows.push(arrow);
}

//return true if there left no arrow.
function updateArrowsLeg() {
    mustContinue = false;
    for (i =0; i<fireArcherLegData.archers.length; i++) {
        if (fireArcherLegData.arrows[i].hit == false) {
            mustContinue = true;
            fireArcherLegData.arrows[i].sprite.x = fireArcherLegData.arrows[i].sprite.x + fireArcherLegData.arrows[i].speed * Math.cos(fireArcherLegData.arrows[i].sprite.rotation);
            fireArcherLegData.arrows[i].sprite.y = fireArcherLegData.arrows[i].sprite.y + fireArcherLegData.arrows[i].speed * Math.sin(fireArcherLegData.arrows[i].sprite.rotation);
            distance = Math.sqrt(Math.pow(fireArcherLegData.arrows[i].cible.x - fireArcherLegData.arrows[i].sprite.x,2) + Math.pow(fireArcherLegData.arrows[i].cible.y - fireArcherLegData.arrows[i].sprite.y, 2));
            if (distance < 75)
                fireArcherLegData.arrows[i].speed = fireArcherLegData.arrows[i].maxSpeed -1;
            if (distance < 50)
                fireArcherLegData.arrows[i].speed = fireArcherLegData.arrows[i].maxSpeed -2;
            if (distance < 30) {
                fireArcherLegData.arrows[i].sprite.crop(new Phaser.Rectangle(0,0,30+distance, 8));
            }
            if (distance < 3) {
                fireArcherLegData.arrows[i].speed = 0;
                fireArcherLegData.arrows[i].hit = true;
                fireArcherLegData.arrows[i].sprite.crop(new Phaser.Rectangle(0,0,30, 8));
                gameData.dragonLife = gameData.dragonLife - 3;
                distCrop = 565 / 20 * gameData.dragonLife;
                if (distCrop < 0)
                    distCrop = 0;
                gameData.life_barre.crop(new Phaser.Rectangle(0,0,distCrop,34));
            }
            if (distance > 7000) {
                fireArcherData.arrows[i].speed = 0;
                fireArcherData.arrows[i].hit = true;
                gameData.dragonLife = gameData.dragonLife - 3;
                distCrop = 565 / 20 * gameData.dragonLife;
                if (distCrop < 0)
                    distCrop = 0;
                gameData.life_barre.crop(new Phaser.Rectangle(0,0,distCrop,34));
            }
        }
    }
    fireArcherLegData.isFire = mustContinue;
    return !mustContinue;
}

function exist_leg(columns) {
    for (i =0; i<columns.length; i++)
        if (columns[i].archer != null && columns[i].destroyed == false && columns[i].archer.lvl == 3)
            return true;
    return false;
}