
//Grupos de enemigos


Target = function(game){
	this.game = game;
}

Target.prototype = {

	init: function(image){
		this.osciloscopioBg = image;
	},
	Add: function(id){
		this.type = id;
		this.crash_sound = this.game.add.audio('crash' + this.game.rnd.integerInRange(1, 2));
		this.falling_sound = this.game.add.audio('falling');
		this.sprite = this.game.add.sprite(this.game.rnd.integerInRange(80, 800), 50, 'target' + this.type);
		this.sprite.animations.add('alive', [0], 1, false);
		this.sprite.animations.add('falling', [1,2,3,4], 3, false);
		this.sprite.animations.add('crashing', [5, 6], 6, false);

		this.game.physics.arcade.enable(this.sprite);
		this.sprite.body.collideWorldBounds = true;
		this.sprite.anchor.setTo(0.5, 0.5);
		this.sprite.scale.x = 0.5;
		this.sprite.scale.y = 0.5;

		this.sprite.body.setSize(100, 82, 125, 97);

		this.alive = true;
		this.sprite.angle = 0;
		this.angleR = 0;

		this.sprite.animations.play('alive');

		this.falling_sound.play();
	},

	kill: function(){
		if (this.alive)
		{
			this.alive = false;

            this.sprite.animations.play('falling');

			// Aca hay que hacerlo cagar fuego; borrarlo, o algo asi!
			//this.sprite.alpha = 0;
				
			/*r = this.game.rnd.integerInRange(-100, 100);
			this.sprite.angle = 0;
			if (r <= 0)
			{
				this.left = true;
			}
			else
			{
				this.left = false;
			}
			*/
/*
			this.sprite.body.velocity.setTo(
					r,
					this.game.rnd.integerInRange(-200, -100)
					);
*/
            		//this.sprite.animations.play('falling');
		}
	},

	revive: function(){
		this.sprite.animations.play('alive');
		this.crashed = false;
			if (!this.alive)
			{
				this.alive = true;
				this.sprite.x = this.game.rnd.integerInRange(10, 500);
				this.sprite.y = 50;
				this.sprite.angle = 0;
				this.angleR = 0;
			}
	},

	update: function(){
        var self = this;

        if (this.sprite.y >= 500 && this.sprite.body.moves) {
            this.sprite.body.moves = false;
            this.falling_sound.stop();
            this.crash_sound.volume = 2;
            this.crash_sound.play();

            if(!this.alive){
                this.sprite.animations.play('crashing');
                setTimeout( function(){
                    self.game.add.tween(self.sprite).to({alpha: 0}, 1000, Phaser.Easing.Linear.None, true, 1000).onComplete.add(function(){
                        //self.sprite.destroy();
                    })
                } , 3000 )
            }
            else {
                self.sprite.alpha = 0;
            }

        }


		if (this.alive)
		{
			// Muerto por una onda
			var a = HErz == 10 && this.type == 1;
			var b = HErz == 40 && this.type == 2;
			var c = HErz == 80 && this.type == 3;
			if (a || b || c)
			{
				this.kill();
				return TARGET_KILL;
			}

			// Crash contra el suelo
			else if (this.sprite.y >= 500)
			{
                this.alive = false;
//				this.sprite.body.moves = false;
				//this.game.physics.arcade.disable(this.sprite);
				//this.sprite.animations.play('crashing');

//				this.falling_sound.stop();


				return TARGET_CRASH;

				//this.sprite.animations.currentAnim.onComplete.add(function () { this.sprite.destroy();	}, this);
			}
		}

	},

	stfu: function()
	{
		this.crash_sound.volume = 0;
		this.falling_sound.volume = 0;
	}

}
