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

EntityBosstankIPad = 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: 110, y:80},
	offset:{x:4,y:8},
	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_tank-iPad.png', 122, 92 ),	
	// These are our own properties. They are not defined in the base
	// ig.Entity class. We just use them internally for the Player
	health: 3200,
	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('idle',0.1,[0]);
		this.addAnim( 'shooting', 0.1, [1] );
		this.addAnim( 'die', 0.1, [2] );
		this.addAnim( 'none', 0.1, [3] );
		
	},
	draw: function() {
		this.parent();
		if(this.health==1000)
		{
			ig.game.spawnEntity( EntityBurstIPad, this.last.x+20, this.last.y, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x, this.last.y+20, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x-40, this.last.y, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x, this.last.y-40, {flip:this.flip} );
		}
		if(this.health<=0) {
			var font=new ig.Font('media/font-iPad.png');
			font.draw('LEVEL 2 COMPLETED', 350,210,ig.Font.ALIGN.CENTER);
		}
	},
	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<0) {
			this.pos.x=0;
			this.directionIsLeft=false;
		}else if(this.pos.x>700-122) {
			this.pos.x=700-122;
			this.directionIsLeft=true;
		}
		
		if(this.directionIsLeft)
			this.pos.x-=4;
		else
			this.pos.x+=4;
		
		if(Math.abs(this.dy)<881 &&this.health>1000) {	
			this.count--;
			if( this.count > 0 && this.count < 51 && this.count % 5 == 0 &&Math.abs(this.dy)<920) {
				this.currentAnim = this.anims.shooting;		
				this.currentAnim.flip.x = this.flip;
				ig.game.spawnEntity( EntityHitIPad, this.pos.x+54, this.pos.y+88, {flip:this.flip} );
				ig.game.spawnEntity( EntityEnemyBullet2IPad, this.pos.x+54, this.pos.y+88, {flip:this.flip} );
			}else if(this.count==0) {
				this.count=65;
				this.currentAnim = this.anims.idle;		
				this.currentAnim.flip.x = this.flip;
			}else {
				this.currentAnim = this.anims.idle;		
				this.currentAnim.flip.x = this.flip;
			}
		}
//		this.currentAnim = this.anims.idle;		
	//	this.currentAnim.flip.x = this.flip;
		if(this.health<1000&&this.health>0) {
			this.currentAnim = this.anims.die;		
			this.currentAnim.flip.x = this.flip;
			this.vel.x=0;
			this.pos.x=this.last.x;
		}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( EntityBurstIPad, this.last.x+20, this.last.y, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x, this.last.y+20, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x-20, this.last.y, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x, this.last.y-20, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x-20, this.last.y, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x-20, this.last.y-20, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x+20, this.last.y+20, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x+40, this.last.y, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x, this.last.y+40, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x+40, this.last.y+40, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x-40, this.last.y, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x-40, this.last.y-40, {flip:this.flip} );
			ig.game.spawnEntity( EntityBurstIPad, this.last.x, this.last.y-40, {flip:this.flip} );
			this.isKill = true;
			ig.game.score+=1000;
			this.timer.set(2);
		} else {
			
			if(this.timer.delta() > 0) 
			{	ig.game.winLevel2=true;
				this.parent();				
			}
			
		}
	}
});
});