
var game = new Phaser.Game(1024, 768, Phaser.CANVAS, 'easter-fight', {preload: preload, create: create, update: update});

var power1 = 0;
var power2 = 0;
var counter = 3;
var readyFlag = false;
var startFlag = false;
var startedFlag = false;
var start1Flag = true;
var start2Flag = true;
var startedTurtle = 0;
var eggs = 20;

function preload() {

    game.load.image('background', 'assets/images/background.png');
    game.load.image('title', 'assets/images/title.png');
    game.load.image('one', 'assets/images/one.png');
    game.load.image('two', 'assets/images/two.png');
    game.load.image('three', 'assets/images/three.png');
    game.load.image('fight', 'assets/images/fight.png');
    game.load.image('bang1', 'assets/images/bang.png');
    game.load.image('bang2', 'assets/images/bang.png');
    game.load.image('player1wins', 'assets/images/player1wins.png');
    game.load.image('player2wins', 'assets/images/player2wins.png');
    game.load.image('max1', 'assets/images/max.png');
    game.load.image('max2', 'assets/images/max.png');
    game.load.image('min1', 'assets/images/min.png');
    game.load.image('min2', 'assets/images/min.png');
    game.load.image('player1', 'assets/images/player1.png');
    game.load.image('player2', 'assets/images/player2.png');
    game.load.image('a', 'assets/buttons/a.png');
    game.load.image('z', 'assets/buttons/z.png');
    game.load.image('up', 'assets/buttons/up.png');
    game.load.image('left', 'assets/buttons/left.png');
    game.load.image('turtle', 'assets/images/turtle.png');

    i = Math.floor((Math.random() * eggs) + 1);
    game.load.spritesheet('egg1', 'assets/eggs/' + i + '.png', 177, 236, 2);
    i = Math.floor((Math.random() * eggs) + 1);
    game.load.spritesheet('egg2', 'assets/eggs/' + i + '.png', 177, 236, 2);

    game.load.image('meter1', 'assets/images/meter.png');
    game.load.image('meter1fill', 'assets/images/meterfill.png');

    game.load.image('meter2', 'assets/images/meter.png');
    game.load.image('meter2fill', 'assets/images/meterfill.png');

    game.load.spritesheet('buttonStart', 'assets/buttons/start.png', 210, 69, 2);
    game.load.spritesheet('buttonBack', 'assets/buttons/back.png', 210, 69, 2);
    game.load.spritesheet('buttonAgain', 'assets/buttons/again.png', 210, 69, 2);

}

function create() {
    background = game.add.sprite(0, 0, 'background');

    title = game.add.sprite(game.world.centerX, 20, 'title');
    title.anchor.setTo(0.5, 0);

    meter1fill = game.add.sprite(56, 505, 'meter1fill');
    meter1fill.anchor.setTo(0, 1);
    meter1 = game.add.sprite(56, 505, 'meter1');
    meter1.anchor.setTo(0, 1);
    max1 = game.add.sprite(70, 33, 'max1');
    min1 = game.add.sprite(70, 517, 'min1');

    player1 = game.add.sprite(9, 579, 'player1');
    a = game.add.sprite(22, 676, 'a');
    z = game.add.sprite(110, 676, 'z');

    meter2fill = game.add.sprite(884, 505, 'meter2fill');
    meter2fill.anchor.setTo(0, 1);
    meter2 = game.add.sprite(884, 505, 'meter2');
    meter2.anchor.setTo(0, 1);
    max2 = game.add.sprite(897, 33, 'max2');
    min2 = game.add.sprite(897, 517, 'min2');

    player2 = game.add.sprite(831, 579, 'player2');
    up = game.add.sprite(839, 676, 'up');
    left = game.add.sprite(927, 676, 'left');

    buttonStart = game.add.button(game.world.centerX, game.world.centerY - 13, 'buttonStart', actionOnStart, this, 1, 0, 0);
    buttonStart.anchor.setTo(0.5, 0.5);

    buttonBack = game.add.button(game.world.centerX, game.world.centerY + 62, 'buttonBack', actionOnBack, this, 1, 0, 0);
    buttonBack.anchor.setTo(0.5, 0.5);


    egg1 = game.add.sprite(202, 486, 'egg1');

    egg2 = game.add.sprite(822, 486, 'egg2');
    egg2.anchor.setTo(1, 0);


    power1Key = game.input.keyboard.addKey(65);
    start1Key = game.input.keyboard.addKey(90);

    power2Key = game.input.keyboard.addKey(Phaser.KeyCode.UP);
    start2Key = game.input.keyboard.addKey(Phaser.KeyCode.LEFT);
}

function actionOnBack() {
    window.location.href = "../index.html"
}

function actionOnStart() {

    readyFlag = true;
    buttonStart.visible = false;
    buttonBack.visible = false;

    one = game.add.sprite(game.world.centerX, game.world.centerY, 'one');
    one.anchor.setTo(0.5, 0.5);
    one.alpha = 1;
    e = game.add.tween(one.scale).to({x: 2, y: 2, alpha: 0}, 700, "Linear");
    e.start();
    e = game.add.tween(one).to({alpha: 0}, 700, "Linear");
    e.onComplete.add(twoTween, this);
    e.start();



}

function twoTween() {
    two = game.add.sprite(game.world.centerX, game.world.centerY, 'two');
    two.anchor.setTo(0.5, 0.5);
    two.alpha = 1;
    e = game.add.tween(two.scale).to({x: 2, y: 2, alpha: 0}, 700, "Linear");
    e.start();
    e = game.add.tween(two).to({alpha: 0}, 700, "Linear");
    e.onComplete.add(threeTween, this);
    e.start();

}

function threeTween() {
    three = game.add.sprite(game.world.centerX, game.world.centerY, 'three');
    three.anchor.setTo(0.5, 0.5);
    three.alpha = 1;
    e = game.add.tween(three.scale).to({x: 2, y: 2, alpha: 0}, 700, "Linear");
    e.start();
    e = game.add.tween(three).to({alpha: 0}, 700, "Linear");
    e.onComplete.add(fightTween, this);
    e.start();

}

function fightTween() {


    fight = game.add.sprite(game.world.centerX, game.world.centerY, 'fight');
    fight.anchor.setTo(0.5, 0.5);
    fight.alpha = 1;
    e = game.add.tween(fight.scale).to({x: 2, y: 2, alpha: 0}, 700, "Linear");
    e.start();
    e = game.add.tween(fight).to({alpha: 0}, 700, "Linear");
    e.start();

    startFlag = true;
}

function start1() {
    startedTurtle = 1;
    tween = game.add.tween(egg1);
    tween.to({x: 620, y: 200, angle: 90}, 400, "Linear");
    tween.onComplete.add(moveTween1, this);
    tween.start();
}

function moveTween1() {
    e = game.add.tween(egg1);
    e.to({x: 823, y: 350, angle: 180}, 300, Phaser.Easing.In);
    e.onComplete.add(hitTween1, this);
    e.start();
}

function hitTween1() {
    e = game.add.tween(egg1);
    e.to({y: 487}, 500, Phaser.Easing.Bounce.Out);
    e.start();

    bang1 = game.add.sprite(653, 400, 'bang1');
    bang1.alpha = 0;
    b = game.add.tween(bang1);
    b.to({alpha: 0}, 100, Phaser.Easing.Bounce.Out);
    b.onComplete.add(bangTween1, this);
    b.start();
}

function bangTween1() {
    e = game.add.tween(bang1);
    e.to({alpha: 1}, 300, Phaser.Easing.Bounce.Out);
    e.onComplete.add(win1Tween, this);
    e.start();
}

function win1Tween() {
    bang1.alpha = 0;
    checkWinner();
}


//player2
function start2() {
    startedTurtle = 2;
    tween = game.add.tween(egg2);
    tween.to({x: 420, y: 200, angle: -90}, 400, "Linear");
    tween.onComplete.add(moveTween2, this);
    tween.start();
}

function moveTween2() {
    e = game.add.tween(egg2);
    e.to({x: 202, y: 350, angle: -180}, 300, Phaser.Easing.In);
    e.onComplete.add(hitTween2, this);
    e.start();
}

function hitTween2() {
    e = game.add.tween(egg2);
    e.to({y: 487}, 500, Phaser.Easing.Bounce.Out);
    e.start();

    bang2 = game.add.sprite(202, 400, 'bang2');
    bang2.alpha = 0;
    b = game.add.tween(bang2);
    b.to({alpha: 0}, 100, Phaser.Easing.Bounce.Out);
    b.onComplete.add(bangTween2, this);
    b.start();
}

function bangTween2() {
    e = game.add.tween(bang2);
    e.to({alpha: 1}, 300, Phaser.Easing.Bounce.Out);
    e.onComplete.add(win2Tween, this);
    e.start();
}

function win2Tween() {
    bang2.alpha = 0;
    checkWinner();
}

function checkWinner() {
    p1 = Math.abs(50 - power1);
    p2 = Math.abs(50 - power2);
    if (p1 < p2) {
        winPlayer1();
    } else if (p2 < p1) {
        winPlayer2();
    } else {
        if (startedTurtle == 1) {
            winPlayer1();
        } else {
            winPlayer2();
        }
    }

    showEndButtons();
}

function showEndButtons() {
    buttonBack.visible = true;
    buttonAgain = game.add.button(game.world.centerX, game.world.centerY - 13, 'buttonAgain', actionOnAgain, this, 1, 0, 0);
    buttonAgain.anchor.setTo(0.5, 0.5);
}

function actionOnAgain() {
    window.location.reload();
}

function winPlayer1() {
    egg2.frame = 1;

    player1wins = game.add.sprite(game.world.centerX, 144, 'player1wins');
    player1wins.anchor.setTo(0.5, 0);

    showTurtle();

}

function showTurtle() {
    if (startedTurtle == 1) {
        turtle = game.add.sprite(180, 320, 'turtle');
    } else {
        turtle = game.add.sprite(610, 320, 'turtle');
    }
}

function winPlayer2() {
    egg1.frame = 1;

    player2wins = game.add.sprite(game.world.centerX, 144, 'player2wins');
    player2wins.anchor.setTo(0.5, 0);

    showTurtle();
}

function update() {

    if (!readyFlag) {
        return;
    }

    if (!startedFlag) {
        if (power1Key.isDown)
        {
            if (power1 < 100) {
                delta = Math.floor((Math.random() * 10) + 1);
                power1 += delta;
            } else {
                power1 = 100;
            }
        } else {
            if (power1 > 0) {
                delta = Math.floor((Math.random() * 5) + 1);
                power1 -= delta;
            } else {
                power1 = 0;
            }
        }
        p1height = 430 - Math.floor(430 * power1 / 100);
        cropRect = new Phaser.Rectangle(0, p1height, 83, 430);
        meter1fill.crop(cropRect);


        if (power2Key.isDown)
        {
            if (power2 < 100) {
                delta = Math.floor((Math.random() * 10) + 1);
                power2 += delta;
            } else {
                power2 = 100;
            }
        } else {
            if (power2 > 0) {
                delta = Math.floor((Math.random() * 5) + 1);
                power2 -= delta;
            } else {
                power2 = 0;
            }
        }
        p2height = 430 - Math.floor(430 * power2 / 100);
        cropRect = new Phaser.Rectangle(0, p2height, 83, 430);
        meter2fill.crop(cropRect);
    }



    if (start1Key.isDown)
    {
        if (startFlag && !startedFlag && start1Flag) {
            startedFlag = true;
            start1();
        } else {
            start1Flag = false;
        }
    } else {
        start1Flag = true;
    }

    if (start2Key.isDown)
    {
        if (startFlag && !startedFlag && start2Flag) {
            startedFlag = true;
            start2();
        } else {
            start2Flag = false;
        }
    } else {
        start2Flag = true;
    }
}



