
var direccion=1;
var regenDolar=0;
var regenCorona=0;
var vida=5;
var puntuaje=0;

var GameState = {
     init: function () {
    // make the game occuppy all available space, but respecting
    // aspect ratio – with letterboxing if needed
    this.game.scale.forcePortrait=true;
    this.game.scale.pageAlignHorizontally = true;
    this.game.scale.pageAlignVertically = true;
    document.getElementById("anuncios").style.height = "0px";
    },

    preload : function(){
        
        var me = this;

        me.game.load.audio('clip','resources/audio/dinero.ogg');
        me.game.load.audio('backTrack','resources/audio/back.ogg');
        me.game.load.audio('ataca','resources/audio/ataca.ogg');
        
        me.load.image('bg', 'resources/images/back1.png');
        me.load.spritesheet('player', 'resources/images/player.png',243,227);
        me.load.image('mony', 'resources/images/billete.png');
        me.load.image('corona', 'resources/images/corona.png');

       
    },

    create  : function(){
        var me = this;

        //me.game.input.touch.start();
        //me.game.input.touch.onTouchStart(cambio);
        me.game.input.onTap.add(cambio);

        me.game.add.image(0, 0, 'bg');

        
        me.audioDinero=me.game.add.audio('clip');
        me.audioDinero.volume=0.5;

        me.audioAtaca=me.game.add.audio('ataca');
        me.audioAtaca.volume=0.5;

        me.backTrack=me.game.add.audio('backTrack');
        me.backTrack.loop=true;
        me.backTrack.volume=0.3;
        me.backTrack.play();

        
        me.player = me.game.add.sprite(0, 0, 'player');
        me.player.animations.add('derecha',[0], 1, false);
        me.player.animations.add('izquierda',[1],1,false);
        me.player.width*=0.3;
        me.player.height*=0.3;
        me.player.y=me.world.height-me.player.height+5;
        me.game.physics.arcade.enable(me.player);
        
        
    
        me.dolares=me.game.add.group();
        me.dolares.name="dolar";
        me.dolares.create(-20,-100,'mony');
        me.dolares.create(150,-100,'mony');
        me.dolares.width*=0.5;
        me.dolares.height*=0.5;
        //me.dolares.scale.set(0.5);

        me.espana=me.game.add.group();
        me.espana.create(-20,-100,'corona');
        me.espana.create(600,-100,'corona');
        //me.espana.scale.set(0.5);
        me.espana.width*=0.5;
        me.espana.height*=0.5;

        me.txtVida = me.game.add.text(20, 100, 'Vida: '+vida, {font: "20px Trebuchet MS", align: "left"});
        me.txtVida.anchor.set(0.5);
        me.txtVida.x*=2;
        me.txtVida.fill="Red";

        me.txtPuntuaje = me.game.add.text(31, 75, 'Puntaje: '+puntuaje, {font: "20px Trebuchet MS", align: "left"});
        me.txtPuntuaje.anchor.set(0.5);
        me.txtPuntuaje.x*=2;
        me.txtPuntuaje.fill="Green";
        

        me.dolares.setAll('outOfBoundsKill', true);
        me.espana.setAll('outOfBoundsKill', true);

    },

    update  : function(){
        var me = this;
        me.game.physics.arcade.enable(me.dolares);
        me.game.physics.arcade.enable(me.espana);

         if(vida<=0){
        console.log('perdiste!!!');
        vida=5;
        
        MyGame.state.restart();
        me.backTrack.stop();
        MyGame.state.start('over');
        }

        me.txtPuntuaje.text="Puntuaje: "+puntuaje;
        me.txtVida.text="Vida: "+vida;

        
        regenCorona+=1;
        regenDolar+=1;

        

        if(me.player.x<=-10){
            me.player.x=-9;
        }
        if(me.player.x+me.player.width>=me.world.width+10){
            me.player.x=(me.world.width+9)-me.player.width;
        }

        if(direccion) {
            me.player.animations.play('derecha');
            me.player.x+=4;
        }
        else {
            me.player.animations.play('izquierda');
            me.player.x-=4;
        }

        
        var largo=me.dolares.length;



        //Acciones de los dolares
        for (var i = 1; i < largo; i++) {
            var cortar=0;
            me.dolares.children[i].y+=2;

            me.game.physics.arcade.overlap(me.player, me.dolares.children[i], function () {
                me.dolares.removeChildAt(i);
                //console.log('dinero');
                me.audioDinero.play();
                puntuaje+=1;
                cortar=1;
            })

            if(cortar) break;

            if(me.dolares.children[i].y>=me.world.height*2){
                me.dolares.removeChildAt(i);
                break;
            }

            if(regenDolar>60){
            me.dolares.create(aleatorio()*2,0,'mony');
            regenDolar=0;
            }
        }

        largo=me.espana.length;
        if(largo==1){
            me.espana.create(aleatorio()*2,0,'corona');
        }

        //acciones de las coronas
        for (var i = 1; i < largo; i++) {
            var cortar2=0;
            me.espana.children[i].y+=5;

            me.game.physics.arcade.overlap(me.player, me.espana.children[i], function () {
                me.espana.removeChildAt(i);
                //console.log('españa te ataca');
                me.audioAtaca.play();
                vida-=1;
                cortar2=1;
            })

            if(cortar2) break;

            if(me.espana.children[i].y>=me.world.height*2){
                me.espana.removeChildAt(i);
                break;
            }

            if(regenCorona>150){
            me.espana.create(aleatorio()*2,0,'corona');
            regenCorona=0;
            }
        }


        

        

        
    },
    pause : function(){
        me.txtPuntuaje.text="Pausa: "+puntuaje;
    }
}
