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

EntityEnemy2 = 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: 14, y:16},
	offset: {x:2,y:2},
	maxVel: {x: 32, y: 32},
	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.png', 18, 20 ),		
	// 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;
		
		// shoot
		
		
		if(Math.abs(this.dy)<350) {
			if(this.count<40)
				this.vel.y+=5;
			else 
				this.vel.y-=5;
			this.count--;
			if( this.count==25 ) {
				this.currentAnim = this.anims.shooting;		
				this.currentAnim.flip.x = this.flip;
				ig.game.spawnEntity( EntityHit, this.pos.x-1, this.pos.y+11, {flip:this.flip} );
				ig.game.spawnEntity( EntityEnemyBullet2, this.pos.x, this.pos.y+11, {flip:this.flip} );
			}else if( this.count==20 ) {
				this.currentAnim = this.anims.shooting;		
				this.currentAnim.flip.x = this.flip;
				ig.game.spawnEntity( EntityHit, this.pos.x-1, this.pos.y+11, {flip:this.flip} );
				ig.game.spawnEntity( EntityEnemyBullet2, this.pos.x, this.pos.y+11, {flip:this.flip} );
			}else if( this.count==15 ) {
				this.currentAnim = this.anims.shooting;		
				this.currentAnim.flip.x = this.flip;
				ig.game.spawnEntity( EntityHit, this.pos.x-1, this.pos.y+11, {flip:this.flip} );
				ig.game.spawnEntity( EntityEnemyBullet2, this.pos.x, this.pos.y+11, {flip:this.flip} );
			}else if( this.count==10 ) {
				this.currentAnim = this.anims.shooting;		
				this.currentAnim.flip.x = this.flip;
				ig.game.spawnEntity( EntityHit, this.pos.x-1, this.pos.y+11, {flip:this.flip} );
				ig.game.spawnEntity( EntityEnemyBullet2, this.pos.x, this.pos.y+11, {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.parent();
	},
	check: function( other ) {
		if(other.type==ig.Entity.TYPE.BOTH){
			return;
		}	
		other.receiveDamage( 100, this );
		this.kill();
	},
	kill: function() {
		ig.game.spawnEntity( EntityKillenemy, this.last.x, this.last.y, {flip:this.flip} );
		ig.game.score+=300;
		this.parent();
	}
});

EntityEnemyBullet2 = ig.Entity.extend({
	size: {x: 5, y: 8},
//	offset: {x: 2, y: 2},
	maxVel: {x: 0, y: 100},	
	// The fraction of force with which this entity bounces back in collisions
//	bounciness: 0.6, 	
	type: ig.Entity.TYPE.NONE,
	checkAgainst: ig.Entity.TYPE.A, // Check Against B - our evil enemy group
	collides: ig.Entity.COLLIDES.NEVER,		
	animSheet: new ig.AnimationSheet( 'media/gfx/img12a0.png', 10, 13 ),	
	dx:0,
	dy:0,
	
	init: function( x, y, settings ) {
		this.parent( x, y, settings );
		
		this.vel.x -= 10;
		this.vel.y += 300;
		this.addAnim( 'shot', 0.1, [0] );
		this.dx=ig.game.player_position.x-this.pos.x;
		this.dy=ig.game.player_position.y-this.pos.y;
	},
		
	handleMovementTrace: function( res ) {		
		if( res.collision.x || res.collision.y ) {
		ig.game.spawnEntity( EntityHit, this.last.x, this.last.y, {flip:this.flip} );
				this.kill();
		}
		this.parent( res );
	},

	
	// This function is called when this entity overlaps anonther entity of the
	// checkAgainst group. I.e. for this entity, all entities in the B group.
	check: function( other ) {
		if(other.type==ig.Entity.TYPE.BOTH){
			ig.game.spawnEntity( EntityHit, this.last.x, this.last.y, {flip:this.flip} );
			this.kill();
			return;
		}		
		ig.game.spawnEntity( EntityHit, this.last.x, this.last.y, {flip:this.flip} );
		other.receiveDamage( 100, this );
		this.kill();
	}	,
	update: function() {
		this.pos.y+=2;
		// if(this.dy>0&&Math.abs(this.dy)<350) {
			// if(this.dx<0) {
				// this.pos.y++;
				// this.pos.x--;
			// }else {
				// this.pos.y++;
				// this.pos.x++;
			// }
		// }
		this.parent();
	}
});

});