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

var BoundTarget = {
    tl_x : 640,
    tl_y : 110,
    br_x : 875,
    br_y : 180
}

function Point () {
    this.x = 0;
    this.y = 0;
}

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

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


function createArrow(archer) {
    arrow = new Arrow();
    start = new Point();
    start.x = archer.sprite.x+20;
    start.y = archer.sprite.y+20;
    if (archer.lvl == 1)
        arrow.sprite = game.add.sprite(start.x,start.y,'Arrow1');
    else if (archer.lvl == 2)
        arrow.sprite = game.add.sprite(start.x,start.y,'Arrow2');
    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;
    arrow.hit = false;
    fireArcherData.arrows.push(arrow);
}

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