script_enemy_main {

	#include_function ".\..\ShotSheet\shot_replace.dnh"
	#include_function ".\..\functions.txt"
	#include_function ".\..\other\enemy_functions_fairy.txt"

	let GCSD=GetCurrentScriptDirectory;
	let imgEnemy=GPSD~"img\minion.png";

	let type=GetCommonData("E");
	let arg1=GetArgument;
	let arg2=GetCommonData("A2");
	let arg3=GetCommonData("A3");
	let arg4=GetCommonData("A4");
	let arg5=GetCommonData("A5");
	let diff=GetCommonDataDefault("Diff",1);

	let Life=[120,3000];
	let expsize=[2,2];
	let expexpand=[0.2,0.4];
	let bomb=[100,70];

	let size=          [0,0];
	let ScoreItem=     [1,4];
	let MultiplierItem=[0,4];

	let ScoreKill=[3,3,3,5]; //divided by 100
	let ScoreDamage=[1.5,1.5,1.5,3]; //divided by 100 (Score per kill, but calculated per hit)

	let BeDeleted=1;
	let MaxGraze=0;
	let prevlife=0;
	let totallife=0;
	let ii=0;

	let xs=0;
	let ys=0;

    @Initialize {
	Randomize;
	shotinit;
        SetLife(Life[type]+10000);
	prevlife=Life[type];
	totallife=Life[type];

	FairyInit;


	if(type==0){
		DamageRate;
		Move0;
	}else{
		DeathFairyInit(3,1,1);
		Move1;
	}
    }

    @MainLoop {
	FairyDrawLogic;
	if(type==1){
		DeathFairyLogic(170+rand(-20,20),rand(0,0.8),200+rand(-20,20),rand(0,0.8));
	}
	AddScore(((prevlife - (upper(GetLife-10000,0)) )/totallife)*(ScoreDamage[type]*100));
	prevlife=upper(GetLife-10000,0);

	SetCollisionA(GetX,GetY, [20,40][type]);
	SetCollisionB(GetX,GetY, 12);

	if((GetX<GetClipMinX-64||GetX>GetClipMaxX+64||GetY<GetClipMinY-64||GetY>GetClipMaxY+64)&&BeDeleted==1){
		VanishEnemy;
	}

	if(GetLife<10000){VanishEnemy;}	

	yield;
    }

task DamageRate{
	let time=40;
	SetDamageRate(0,0);
	Wait(time);
	SetDamageRate(215,bomb[type]*2);
}
// Type, X, Y, V, Angle, a1, a2, a3, a4

function Wait(frames){loop(frames){yield;}}


//0=flamethrower | angvel, graphic, angleadd, frequency //-------------------------------------------------------------------------------------------------

task Move0{
	Shoot0;
	let angle=GetAngle;
	loop{
		SetAngle(angle);
		angle+=arg1;
		yield;
	}
}
task Shoot0{
	let angle=0;
	if(diff==1){
		arg3*=1.5;
		arg4*=1.5;
	}
	if(diff==0){
		arg3*=2;
		arg4*=2;
	}
	loop{
		SE("shot1");
		FlameTrail(angle);
		angle+=arg3;
		Wait(arg4);
	}
}
task FlameTrail(angle){
	let angleenemy=GetAngle;
	let x=GetX;
	let y=GetY;
	let delay=15;
	delay=EnemyShotDelay(x,y,angle+angleenemy,70,30,SP01,delay,40);
	CreateShotA(0,x,y,0);
	SetShotDataA(0,0,0,angle,0,0,0,arg2);
	SetShotDataA(0,80+delay,0,angle+angleenemy,0,0.1,2,arg2);

	if((angle+angleenemy+3600)%360!=(-angle+angleenemy+3600)%360){
		CreateShotA(1,0,0,0);
		SetShotDataA(1,0,0,-angle+angleenemy,0,0.1,2,arg2);
		AddShot(80+delay,0,1,0);
	}

	FireShot(0);
}

//-------------------------------------------------------------------------------------------------  //1=deathfairy	| no params

task Move1{
	SetInvincibility(60);
	SetMovePositionNew(GetCenterX-100,GetCenterY-100,60);
	Wait(60);
	let dir=1;
	let angle=0;
	DamageRateScale(240,70,500,100, 20*12);
	let time=[50,30,30][diff];
	let shotdelay=[5,3,3][diff];
	let angleadd=[3,3.5,4.2][diff];
	loop([12,12,9][diff]){
		SetMovePosition02(GetCenterX+100*dir,GetCenterY-100+rand(-50,50),time);
		angle+=45+rand(-15,15);
		SE("laser0");
		loop(10){
			SE("laser1");
			loop(12){
				CreateLaser01(GetX,GetY,2,angle+90,90,10,GREEN01,0);
				if(diff==2){
					CreateShot01(GetX,GetY,rand(2,3),rand(0,360),AQUA11,0);
				}else if(diff==1&&rand_int(0,1)==0){
					CreateShot01(GetX,GetY,rand(1.5,2.5),rand(0,360),AQUA11,0);
				}
				angle+=360/12;
			}
			angle+=angleadd*dir;
			Wait(shotdelay);
		}
		dir=-dir;
		Wait(20);
	}
	Wait(60);
	let speed=0;
	loop{
		speed+=0.1;
		SetY(GetY-speed);
		yield;
	}
}



//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------


@DrawLoop{
	DeathFairyEffect(4,0);
	FairyDrawLoop;
}

    @Finalize {
	deleteobjects=true;
	yield;
	deleteobjects=false;
	if(GetLife<10000){
		AddScore(ScoreKill[type]*100);

		CreateItems(GetX,GetY,size[type],ScoreItem[type],MultiplierItem[type],arg5[2],arg5[3]);

		SE("enemyexplode");

		CreateExplosion(0,GetX,GetY,expsize[type],[color[0]-prand(0,100),color[1]-prand(0,100),color[2]-prand(0,100)],expexpand[type]);
		CreateExplosion(0,GetX,GetY,expsize[type],[color[0]-prand(0,100),color[1]-prand(0,100),color[2]-prand(0,100)],expexpand[type]/2);

	}
    }


}
