#include "../../functions/functions_enemy.txt"

function CreateEnemyA1(x,dir,spread)
	{
	let obj = CreateBasicEnemyA1(x,-32,0,0,30,30,type_fairy,fairy_yellow,0);
	TAction;
	return obj;

	task TAction
		{			
		if(spread == -1)
			{
			spread = 2+Dif*2;
			}
		let angleA = 5;
		let count = 45;
		let num = floor(rand(0,count));
		let counter = -1;

		ObjMove_SetAngle(obj,90+rand(-1,1));
		ObjMove_SetSpeed(obj,4);
		ObjMove_SetAcceleration(obj,-4/75);
		ObjMove_SetMaxSpeed(obj,0);
		while(!Obj_IsDeleted(obj))
			{
			counter+=1;
			if(counter == 75)
				{
				ObjMove_SetAngle(obj,90+dir*60+rand(-1,1));
				ObjMove_SetSpeed(obj,rand(2,2.5));
				ObjMove_SetAcceleration(obj,0);
				}
			if(counter >= 30 && counter < 180 &&  (counter+num)%count == 0)
				{
				let angle = rand(-1,1)*angleA*0.5;
				ascent(i in 0..spread)
					{
					i = i-spread/2+0.5;
					CreateBulletA1(ObjMove_GetX(obj),ObjMove_GetY(obj),3,GetAngleToPlayer(obj)+i*angleA+angle,SMALL_WHITE,8,0.67);
					}
				}
			if(counter >= 75 && IsBeyondClip(obj,32))
				{
				Obj_Delete(obj);
				}
			yield;
			}
		}
	}

function CreateEnemyB1(dir,t)
	{
	let obj = CreateBasicEnemyA1(GetStgFrameWidth*(0.5+dir*0.5)+dir*32,GetStgFrameHeight*0.5-90,0,0,45,30,type_orb,orb_green,36);
	TAction;
	return obj;

	task TAction
		{		
		let spread = 12;
		let angleA = rand(-1,1)*2.5;
		let count = floor(rand(45,60));
		let num = floor(rand(0,count));
		let counter = -1;

		ObjMove_SetAngle(obj,90+dir*90);
		ObjMove_SetSpeed(obj,4);
		while(!Obj_IsDeleted(obj))
			{
			counter+=1;
			if(counter == 45)
				{	
				ObjMove_SetAngularVelocity(obj,-dir*(360+angleA)/90);
				}
			if(counter == 45+90)
				{	
				ObjMove_SetAngularVelocity(obj,0);
				}
			if(counter >= 30 && (counter+num)%count == 0)
				{
				let speed = 0.75+t*1;
				t = 1-t;
				let angle = rand(-1,1)*angleA*0.5;
				PlaySound("se_Shot1",100);
				ascent(i in 0..spread)
					{
					let angle = i*360/spread+90;
					CreateBulletA2(ObjMove_GetX(obj)+24*cos(angle),ObjMove_GetY(obj)+24*sin(angle),4,angle,(speed-4)/20,0,speed,BL_SMALL_GREEN,8,1);
					}
				}
			if(counter >= 75 && IsBeyondClip(obj,32))
				{
				Obj_Delete(obj);
				}
			yield;
			}
		}
	}

function CreateEnemyC1
	{
	let obj = CreateBasicEnemyA1(GetStgFrameWidth*0.5,-64,0,0,360,15,type_fairy,fairy_red,6);
	TAction;
	return obj;
	task TSound
		{
		PlaySound("se_Shot4",100);
		loop(90){yield;}
		PlaySound("se_Shot8",80);
		}
	task TAction
		{		
		let spreadM = [[12,5+Dif*2],[4,2,8]];
		let counter = -1;
		let type = 0.25;

		ObjMove_SetDestAtWeight(obj,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-60,30,5);
		loop(120){yield;}
		while(!Obj_IsDeleted(obj))
			{
			counter+=1;
			if(counter%180 == 0)
				{		
				let spread = spreadM[0];
				let angleA = rand(0,360);
				ascent(i0 in 0..spread[0])
					{	
					TSound;
					ascent(i1 in 0..spread[1])
						{
						let dist = (spread[1]*0.25+i1*0.75)/spread[1];
						let speed = dist*2.5+0.5;
						ascent(dir in 0..2)
							{
							dir = dir*2-1;
							let angle = (i0+0.5+dir*i1*0.75/spread[1])*360/spread[0]+90+dir*angleA;
							let shot = CreateBulletA2(ObjMove_GetX(obj)+dist*8*cos(angle),ObjMove_GetY(obj)-2+dist*8*sin(angle),dist*4,angle,-dist*4/75,dir*((spread[1]*0.33+i1*0.67)/spread[1])*180/75,0,ARROWHEAD_BLUE-1,8,1.125);
							ObjMove_AddPatternA2(shot,75,NO_CHANGE,NO_CHANGE,0,0,0);
							ObjMove_AddPatternA2(shot,90,4,NO_CHANGE,(speed-4)/30,0,speed);
							}
						}
					}
				}
			if(counter%45 == 0)
				{
				let spread = spreadM[1];
				PlaySound("se_Shot1",80);
				ascent(i0 in 0..spread[0])
					{
					ascent(i1 in 0..spread[1])
						{	
						i1 = i1-spread[1]/2+0.5;
						ascent(i2 in 0..spread[2])
							{
							let dist = (spread[2]*0.33+i2*0.67)/spread[2];
							let angle = (i0+type)*360/spread[0]+90+i1*(12.5+(spread[2]-i2)*47.5/spread[2])/spread[1];
							let shot = CreateBulletA1(ObjMove_GetX(obj)+32*cos(angle),ObjMove_GetY(obj)+32*sin(angle),dist*4.25,angle,SMALL_BLUE,8,1.125);
							Obj_SetRenderPriority(shot,0.49);
							}
						}
					}
				type *= -1;
				}
			if(counter >= 75 && IsBeyondClip(obj,64))
				{
				Obj_Delete(obj);
				}
			yield;
			}
		}
	}

function CreateEnemyD1(x,type)
	{
	let obj = CreateBasicEnemyA1(x,-64,0,0,20+type*90,30,type_orb,orb_green-type*3,38);
	TAction;
	return obj;

	task TAction
		{		
		let counter = -1;
		let count = 15;
		let num = floor(rand(0,count));

		ObjMove_SetAngle(obj,90-((x-GetStgFrameWidth*0.5)/GetStgFrameWidth)*7.5);
		ObjMove_SetSpeed(obj,3);
		
		while(!Obj_IsDeleted(obj))
			{
			counter+=1;
			if(type == 1 && ObjMove_GetY(obj) < GetStgFrameHeight-16&& ObjMove_GetY(obj) > 8 && (counter+num)%count == 0)
				{
				PlaySound("se_Shot1",80);
				TShot;
				}
			if(counter >= 120 && IsBeyondClip(obj,64))
				{
				Obj_Delete(obj);
				}
			yield;
			}
		}
	task TShot
		{
		let shot = CreateBulletA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2.25,GetAngleToPlayer(obj),SMALL_RED,0,1);
		ObjShot_SetIntersection(shot,false);
		descent(t in 0..20)
			{
			ObjShot_SetScale(shot,1.125+t*1/20);
			ObjRender_SetAlpha(shot,(20-t)*255/20);
			yield;
			}
		ObjShot_SetIntersection(shot,true);	
		}
	}
function CreateEnemyD2(dir)
	{
	let obj = CreateBasicEnemyA1(GetStgFrameWidth*(0.5+dir*0.525),GetStgFrameHeight*0.5-120,0,0,90,30,type_fairy,fairy_green,2);
	TAction;
	return obj;

	task TAction
		{		
		let spread = [3,8+Dif*2];
		let counter = -1;
		let angleM;

		ObjMove_SetAngle(obj,90+dir*90);
		ObjMove_SetSpeed(obj,4);
		ObjMove_SetAcceleration(obj,-4/60);
		ObjMove_SetMaxSpeed(obj,0);
		let rate = 0.5+Dif*0.17;
		
		while(!Obj_IsDeleted(obj))
			{
			counter+=1;
			if(counter == 90)
				{
				angleM = GetAngleToPlayer(obj)+dir*90;
				}
			if(counter >= 90 && counter < 120 && counter%ceil((120-90)/6) == 0)
				{
				PlaySound("se_Shot8",100);
				descent(i2 in 0..2)
					{
					ascent(i0 in 0..spread[0])
						{	
						i0 = i0-spread[0]*0.5+0.5;
						let angle = angleM+i0*(20-i2*7.5)/spread[0];
						ascent(i1 in 0..spread[1])
							{
							let dist = (spread[1]*(1-rate)+i1*rate)/spread[1];
							let shot = CreateBulletA1(ObjMove_GetX(obj)+(16+dist*16)*cos(angle),ObjMove_GetY(obj)+(16+dist*16)*sin(angle),dist*(3-i2*0.25),angle,BL_SMALL_GREEN-i2*3,8,1);
							}
						}
					}
				angleM-=dir*270/6;
				}
			if(counter == 150)
				{
				ObjMove_SetAngle(obj,90);
				ObjMove_SetAcceleration(obj,1/45);
				ObjMove_SetMaxSpeed(obj,2.25);
				}
			if(counter >= 180 && IsBeyondClip(obj,128))
				{
				Obj_Delete(obj);
				}
			yield;
			}
		}
	}