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

@Initialize{TMain;}
@MainLoop{yield;}

task TMain
	{	
	loop(60){yield;}
	CreateEnemyA1;
	loop(390){yield;}
	local
		{
		let spread = [32,4];
		ascent(i0 in 0..spread[0])
			{
			ascent(i1 in 0..spread[1])
				{
				let y = GetStgFrameHeight*0.5-105+i1*50/spread[1];
				let shot = CreateBulletA1(0,y,0,0,SMALL,0,1,SHOT_GRAY);
				ObjMove_SetDestAtWeight(shot,GetStgFrameWidth*(1-(0.15+i0*0.65/(spread[0]-1))),y,15,5);
				ObjMove_AddPatternA2(shot,210-i0,0,90,1/40,0,10);
				}
			loop(1){yield;}
			}
		}
	loop(360){yield;}
	loop(11)
		{
		ascent(dir in 0..2)
			{	
			dir = dir*2-1;
			CreateEnemyA2(GetStgFrameWidth*(0.5+dir*0.35));
			}
		loop(30){yield;}
		}
	loop(GetStgFrameHeight/2-30){yield;}
	ascent(t in 0..16)
		{
		ascent(dir in 0..2)
			{
			dir = dir*2-1;
			CreateEnemyA3(GetStgFrameWidth*(0.5+dir*(-0.5+t/16)));
			}
		loop(5){yield;}
		}
	loop(10^10){yield;}
	}

task CreateEnemyA1
	{
	let scale = 144;
	let x = GetPlayerX;
	let y = GetStgFrameHeight+scale*0.5;
	let a = 270;
	let s = 7;
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "./img/fist.png");
	Obj_SetRenderPriority(obj,0.55);
	ObjSpriteList2D_SetSourceRect(obj,0,0,256,256);
	ObjRender_SetScaleXYZ(obj,scale*(0.5/0.375)/256,scale*(0.5/0.375)/256,0);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAngleZ(obj,-90);
	let t = -1;
	let c = 39;
	while(!Obj_IsDeleted(obj) && y > scale*0.5)
		{
		x+=s*cos(a);
		y+=s*sin(a);	
		ObjSpriteList2D_ClearVertexCount(obj);	
		if(c%40 != 39)
			{
			c+=1;
			}
		if(c%40 == 39 && GetDistance(GetPlayerX,GetPlayerY,x,y) < scale*0.5*0.8)
			{
			c+=1;
			let spam = CreateBulletA1(GetPlayerX,GetPlayerY,0,0,BUBBLE,0,1,SHOT_RED);
			Obj_SetVisible(spam,false);
			Obj_DelayDelete(spam,2);
			}
		ascent(i in 0..2)
			{
			ObjRender_SetColorHSV(obj,330,i*255,255);
			ObjRender_SetPosition(obj,x,y,0);
			ObjSpriteList2D_SetDestCenter(obj);	
			ObjSpriteList2D_AddVertex(obj);
			}
		t+=1;
		if(t%2 == 0 && y < GetStgFrameHeight*0.8)
			{
			loop(5)
				{
				let shot = CreateBulletA1(x+rand(-1,1)*scale*0.35,y+scale*0.4,rand(1,2.125),a+180+rand(-1,1)*90,SMALL,8,1.125,SHOT_RED);
				ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
				}
			}
		yield;
		}
	ascent(i in 0..2)
		{
		TLine(i*2-1,i);
		}
	descent(t in 0..45)
		{
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(i in 0..2)
			{
			ObjRender_SetColorHSV(obj,330,i*255,255);
			ObjRender_SetPosition(obj,x,y,0);
			ObjSpriteList2D_SetDestCenter(obj);					
			ObjRender_SetAlpha(obj,t*255/45);	
			ObjSpriteList2D_AddVertex(obj);
			}
		yield;
		}
	Obj_Delete(obj);
	task TLine(dir,i)
		{
		let a = 90;
		let y = 0;
		let d = max(x,GetStgFrameWidth-x)+20;
		let t = -1;	
		let s = 2;
		while(t*s < d)
			{
			t+=1;
			if(t%4 == 0)
				{
				if(t >= i)
					{
					ascent(n in 0..2)
						{
						TShot(x+t*s*cos(a+90+i*180));
						}
					}
				}
			}
		}
	task TShot(x)
		{
		let shot = CreateBulletA2(x,7,0,90+rand(-1,1)*3.5,1/30,0,rand(1.125,3),SMALL,5,1.125,SHOT_PURPLE);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
		while(!Obj_IsDeleted(shot) && ObjMove_GetY(shot) < GetStgFrameHeight)
			{
			yield;
			}
		ObjMove_SetAngle(shot,-ObjMove_GetAngle(shot));
		ObjMove_SetY(shot,GetStgFrameHeight);
		ObjMove_SetMaxSpeed(shot,ObjMove_GetSpeed(shot)+1);
		ObjMove_SetSpeed(shot,ObjMove_GetSpeed(shot)+1);
		}
	}
task CreateEnemyA2(x)
	{	
	let obj = CreateBasicEnemyA1(x,-24,2,90,100,50,type_fairy,fairy_red,1);
	while(!Obj_IsDeleted(obj) && ObjMove_GetY(obj) < 5){yield;}
	if(Obj_IsDeleted(obj)){return;}
	let t = -1;
	while(!IsBeyondClip(obj,-4) && !Obj_IsDeleted(obj))
		{
		t+=1;	
		if(t%20 == 0)
			{
			CreateBulletA1(ObjMove_GetX(obj),ObjMove_GetY(obj),4,GetAngleToPlayer(obj),KUNAI,8,1.125,SHOT_RED);
			}
		yield;
		}
	Obj_Delete(obj);
	}

task CreateEnemyA3(x)
	{
	let shot = CreateBulletA2(x,GetStgFrameHeight,0,0,2/20,0,3,BUBBLE,0,1,SHOT_RED);
	ObjShot_SetIntersection(shot,false);
	ObjMove_SetAngle(shot,GetAngleToPlayer(shot));
	ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
	ascent(t in 0..20)
		{
		ObjRender_SetAlpha(shot,t*255/20);
		ObjShot_SetScale(shot,(1-t/20)+1);
		yield;
		}
	ObjRender_SetAlpha(shot,255);
	ObjShot_SetScale(shot,1);
	ObjShot_SetIntersection(shot,true);
	}