ig.module(
	'game.entities.boss'
)
.requires(
	'impact.entity',
	'impact.timer'
)
.defines(function(){

EntityBoss = 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: 25, y:23},
	offset: {x:2, y:2},
	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/boss.png', 29, 28 ),	
	// These are our own properties. They are not defined in the base
	// ig.Entity class. We just use them internally for the Player
	health: 3000,
	flip: false,
	count: 50,
	dx:0,
	dy:0,
	isKill:false,
	
	timer: new ig.Timer(),
	
	//i add-khi
	directionIsLeft: false,	//if true,move right to left,else left to right
	
	init: function( x, y, settings ) {
		this.parent( x, y, settings );		
		// Add the animations
		this.addAnim('leftshot',0.1,[0]);
		this.addAnim( 'rightshot', 0.1, [1] );
		this.addAnim( 'none', 0.1, [2] );
		this.addAnim( 'die', 0.1, [3] );
//		this.addAnim( 'none', 0.1, [3] );
		
	},
	draw: function() {
		this.parent();
		
		if(this.health<=0) {
			var font=new ig.Font('media/font.png');
			font.draw('WELL DONE!', 100,100);
			font.draw('BUGGY MASTER.', 100,140);
			
		}
	},
	update: function() {	
		// move left or right
		// shoot
		
		this.dx=ig.game.player_position.x-this.pos.x;
		this.dy=ig.game.player_position.y-this.pos.y;
		// move!
		
		if(this.pos.x<30) {
			this.pos.x=30;
			this.directionIsLeft=false;
		}else if(this.pos.x>280) {
			this.pos.x=280;
			this.directionIsLeft=true;
		}
		
		if(this.directionIsLeft)
			this.pos.x-=3;
		else
			this.pos.x+=3;
		
		if(Math.abs(this.dy)<480&&this.health>0) {	
			this.count--;
		if(this.count==0){
			this.count=10;
			this.currentAnim = this.anims.rightshot;		
			this.currentAnim.flip.x = this.flip;
			ig.game.spawnEntity( EntityHit, this.pos.x, this.pos.y+26, {flip:this.flip} );
			ig.game.spawnEntity( EntityEnemyBullet2, this.pos.x+3, this.pos.y+29, {flip:this.flip} );
	//		ig.game.spawnEntity( EntityBullet, this.pos.x+24, this.pos.y, {flip:this.flip} );
		//	ig.game.spawnEntity( EntityBullet, this.pos.x+24, this.pos.y-14, {flip:this.flip} );		
		}else if(this.count==2){
			this.currentAnim = this.anims.leftshot;		
			this.currentAnim.flip.x = this.flip;
			ig.game.spawnEntity( EntityHit, this.pos.x+20, this.pos.y+26, {flip:this.flip} );
			ig.game.spawnEntity( EntityEnemyBullet2, this.pos.x+26, this.pos.y+29, {flip:this.flip} );		
		}
		}
//		this.currentAnim = this.anims.idle;		
	//	this.currentAnim.flip.x = this.flip;
		if(this.health<=0) {
			this.currentAnim = this.anims.none;		
			this.currentAnim.flip.x = this.flip;
		}
		this.parent();

	},
	check: function( other ) {
		if(other.type==ig.Entity.TYPE.BOTH){
			return;
		}	
		other.receiveDamage( 100, this );
	//	this.kill();
	},
	
	kill: function() {
	//	alert("Target zone, Level completed.");
		
			
		if(this.isKill == false) {			
			ig.game.spawnEntity( EntityBurst, this.last.x+10, this.last.y, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurst, this.last.x, this.last.y+10, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurst, this.last.x-10, this.last.y, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurst, this.last.x, this.last.y-10, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurst, this.last.x-10, this.last.y, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurst, this.last.x-10, this.last.y-10, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurst, this.last.x+10, this.last.y+10, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurst, this.last.x+20, this.last.y, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurst, this.last.x, this.last.y+20, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurst, this.last.x+20, this.last.y+20, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurst, this.last.x-20, this.last.y, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurst, this.last.x-20, this.last.y-20, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurst, this.last.x, this.last.y-20, {flip:this.flip} );
			this.isKill = true;
			ig.game.score+=10000;
			
			this.timer.set(1);
		} else {
			if(this.timer.delta() > 0) 	
			{	this.parent();
				ig.game.winLevel7=true;					
			}
			
		}
	}
});
});