// JavaScript Document
ig.module(
	'game.entities.enemy4-iPad'
)
.requires(
	'impact.entity'
)
.defines(function(){

EntityEnemy4IPad = ig.Entity.extend({
	
	// The players (collision) size is a bit smaller than the animation
	// frames, so we have to move the collision box a bit (offset)
	size: {x: 28, y:32},
	offset: {x:4,y:4},
	maxVel: {x: 64, y: 64},
	type: ig.Entity.TYPE.B, // Player friendly group
	checkAgainst: ig.Entity.TYPE.A,
	collides: ig.Entity.COLLIDES.ACTIVE,	
	animSheet: new ig.AnimationSheet( 'media/gfx/enemy_2-iPad.png', 36, 40 ),		
	// These are our own properties. They are not defined in the base
	// ig.Entity class. We just use them internally for the Player
	health: 300,
	flip: false,
	count: 45,
	dx:0,
	dy:0,
	
	init: function( x, y, settings ) {
		this.parent( x, y, settings );		
		// Add the animations
		this.addAnim( 'shooting', 0.1, [1,1,1] );
		this.addAnim( 'walking', 0.1, [0,1] );
	},
	
	handleMovementTrace: function( res ) {		
		if( res.collision.x || res.collision.y ) {
			this.dx=ig.game.player_position.x-this.pos.x;
			this.dy=ig.game.player_position.y-this.pos.y;
		}
		
		this.parent( res );
	},	
	
	update: function() {
		this.dx=ig.game.player_position.x-this.pos.x;
		this.dy=ig.game.player_position.y-this.pos.y;
		if(Math.abs(this.dy)<920) {
		this.count--;
			if( this.count==25 ) {
				this.currentAnim = this.anims.shooting;		
				this.currentAnim.flip.x = this.flip;
				ig.game.spawnEntity( EntityHitIPad, this.pos.x-2, this.pos.y+22, {flip:this.flip} );
				ig.game.spawnEntity( EntityEnemyBullet2IPad, this.pos.x, this.pos.y+22, {flip:this.flip} );
			}
			else if( this.count==20 ) {
				this.currentAnim = this.anims.shooting;		
				this.currentAnim.flip.x = this.flip;
				ig.game.spawnEntity( EntityHitIPad, this.pos.x-2, this.pos.y+22, {flip:this.flip} );
				ig.game.spawnEntity( EntityEnemyBullet2IPad, this.pos.x, this.pos.y+22, {flip:this.flip} );
			}else if( this.count==15 ) {
				this.currentAnim = this.anims.shooting;		
				this.currentAnim.flip.x = this.flip;
				ig.game.spawnEntity( EntityHitIPad, this.pos.x-2, this.pos.y+22, {flip:this.flip} );
				ig.game.spawnEntity( EntityEnemyBullet2IPad, this.pos.x, this.pos.y+22, {flip:this.flip} );
			}else if( this.count==10 ) {
				this.currentAnim = this.anims.shooting;		
				this.currentAnim.flip.x = this.flip;
				ig.game.spawnEntity( EntityHitIPad, this.pos.x-2, this.pos.y+22, {flip:this.flip} );
				ig.game.spawnEntity( EntityEnemyBullet2IPad, this.pos.x, this.pos.y+22, {flip:this.flip} );
			}else if(this.count==0) {
				this.count=70
			}else {
				this.currentAnim = this.anims.walking;		
				this.currentAnim.flip.x = this.flip;
			}
		}	
		this.currentAnim = this.anims.walking;		
		this.currentAnim.flip.x = this.flip;
		// move!
		
		
		this.shot=!this.shot;
		
		if(this.dy>0&&Math.abs(this.dy)<350&&this.shot) {
			if(this.dx<0) {
				this.vel.y+=2;
				this.vel.x-=2;
			}else {
				this.vel.y+=2;
				this.vel.x+=2;
			}
		}
		this.parent();
	},
	check: function( other ) {
		if(other.type==ig.Entity.TYPE.BOTH){
			return;
		}	
		other.receiveDamage( 100, this );
		this.kill();
	},
	kill: function() {
		ig.game.spawnEntity( EntityKillenemyIPad, this.last.x, this.last.y, {flip:this.flip} );
		ig.game.score+=300;
		this.parent();
	}
});
});