let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let GCSD = GetCurrentScriptDirectory();
let img=GCSD~"img/effbreak.png";
let i_time = 0;
let DELETE = 1;
let SCORE = 2;
let LIFE = 3;
let BOMB = 4;
SetCommonData("ItemStart",1);
#include"./Functions/EventList.Txt"
#include"./Functions/SoundFunctions.Txt"
@Initialize
{
SetDefaultBonusItemEnable(false);
LoadItemData(GCSD~"Item_Data.txt");
}

@MainLoop
{
	CollectItemsInCircle(ObjMove_GetX(GetPlayerObjectID),ObjMove_GetY(GetPlayerObjectID),70);
	if(ObjMove_GetY(GetPlayerObjectID)<133){CollectAllItems;}
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_END_BOSS_STEP){
		SetAutoDeleteObject(true);
		CheckItem;
		DeleteWave(GetCommonData("ItemEnd",5));
	}
	case(EV_FAM_DIE){
		let enemy = GetAllEnemyID;
			ascent(i in 0..length(enemy)){
					if(enemy[i]!=GetEventArgument(0)){
					//PlaySound("Enep2",100,0);
					FamExplode(ObjMove_GetX(enemy[i]),ObjMove_GetY(enemy[i]),0.6,Obj_GetValueD(enemy[i],"FamColor","WHITE"));
					}
			}
		}
	case(EV_GET_ITEM){
	PlaySound("item",85,0);
		if(GetEventArgument(0)==ITEM_SPELL){
		PlaySound("item",90,0);
		SetPlayerSpell(GetPlayerSpell+1);
		}
		if(GetEventArgument(0)==ITEM_1UP){
		PlaySound("extend",90,0);
		SetPlayerLife(GetPlayerLife+1);
		}
		if(GetEventArgument(0)==DELETE){
		AddScore(75);
		}
	}
	case(EV_DELETE_SHOT_TO_ITEM){
	let obj = GetEventArgument(0);
	let point = GetEventArgument(1);
	let graphic = ObjShot_GetImageID(obj);
	let px = GetCommonData("BossX",0);
	let py = GetCommonData("BossY",0);
	if(Obj_GetValueD(obj,"ShotEffectOrNot",1)==1){
			if(IsPlayerSpellActive==false){
			CreateDeleteItem(point[0],point[1]);
			}
			fragment(point[0],point[1],0,ObjShot_GetImageID(obj));
			SetCommonData("Effect",2);
			if(GetCommonData("Effect",0)==2){
			spongy(obj,0.1,10);
			}
		}
	}
}

task CreateDeleteItem(x,y) {
	let obj = CreateItemU1(DELETE,x,y,0);
	let time = 0;
	ObjItem_SetRenderScoreEnable(obj,false);
	ObjMove_SetAngle(obj, 270+rand(-5,5));
	let speed = rand(1,1.25);
	ObjRender_SetScaleXYZ(obj,0.9,0.9,0.9);
	time = 60-GetCommonData("ItemEnd",0)*15-i_time;
		descent(i in 0..time){
		ObjMove_SetSpeed(obj,speed*i/time);
		yield;
		}
	ObjItem_SetDefinedMovePatternA1(obj,ITEM_MOVE_TOPLAYER);
}

task DeleteWave(type){ // type 0 for nonspell, type 1 for spell card
		let wave = 0;
		loop(60-type*58){
		DeleteShotInCircle(TYPE_ALL,TYPE_ITEM,GetCommonData("BossX",0),GetCommonData("BossY",0),wave);
		if(type==0){wave += 40;}
		if(type==1){wave+=1000;}
		i_time++;
		yield;
		}
		i_time = 0; // i_time is global variable
}
	
task fragment(let px,let py,size,let pic){ // Bullet clear effect from UFO,TD
			if(pic<1000){
			let color = GetShotDataInfoA1(pic,TARGET_ENEMY,INFO_DELAY_COLOR);
			let obj=ObjPrim_Create(OBJ_SPRITE_2D);
			let s = rand(0.8,1.2)+size;
			let rect = [0,0,32,32];
			ObjPrim_SetTexture(obj,img);
			ObjSprite2D_SetSourceRect(obj,rect[0],rect[1],rect[2],rect[3]);
			ObjSprite2D_SetDestCenter(obj);
			ObjRender_SetScaleXYZ(obj,s,s,0);
			ObjRender_SetAngleXYZ(obj,0,0,rand(0,359));
			ObjRender_SetBlendType(obj,BLEND_ALPHA);
			ObjRender_SetColor(obj,color[0],color[1],color[2]);
			Obj_SetRenderPriority(obj,0.29);
			if(px!=0 && py!=0){
			ascent(i in 0..7){
			ObjSprite2D_SetDestCenter(obj);
			ObjRender_SetPosition(obj,px,py,0);
			ObjSprite2D_SetSourceRect(obj,rect[0],rect[1],rect[2],rect[3]);
			rect[0] = rect[0]+32;
			rect[2] = rect[2]+32;
			wait(3);
			}
			}
			Obj_Delete(obj);
			}
		}
		
task spongy(shot,v,t){ // make bullet slightly expand and fade it's alpha/color away
		let cloud = ObjShot_GetImageID(shot);
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		let img=GCSD~"Shot/JINXShot.png";
		let s = 1;
		let adc = false;
		let rect = GetShotDataInfoA1(cloud,TARGET_ENEMY,INFO_RECT);
		let dir = ObjMove_GetAngle(shot)+90;
		ObjPrim_SetTexture(obj,img);
		ObjSprite2D_SetSourceRect(obj,rect[0],rect[1],rect[2],rect[3]);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetPosition(obj,ObjMove_GetX(shot),ObjMove_GetY(shot),0);
		ObjRender_SetAngleXYZ(obj,0,0,dir);
		ObjRender_SetBlendType(obj,BLEND_ALPHA);
	 if(GetShotDataInfoA1(cloud,TARGET_ENEMY,INFO_BLEND)==BLEND_ADD_RGB || GetShotDataInfoA1(cloud,TARGET_ENEMY,INFO_BLEND)==BLEND_ADD_ARGB){
	 adc = true;
	 ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	 }
		if(cloud<1000){
		ascent(i in 0..t){
		ObjRender_SetScaleXYZ(obj,s,s,0);
		if(adc==false){ObjRender_SetAlpha(obj,255-i*255/t);}
		if(adc==true){ObjRender_SetColor(obj,255-i*255/t,255-i*255/t,255-i*255/t);}
		s += v;
		yield;
		}
		}
		Obj_Delete(obj);
		}

		task CheckItem{
		loop(GetCommonData("Score",0)){
		let obj = CreateItemU1(SCORE,GetStgFrameWidth/2+rand(-100,100),GetStgFrameHeight/2+rand(-100,50),1000);
		ObjItem_SetRenderScoreEnable(obj,false);
		LenenItem( obj, 102, 102, 255, 0.85 );
		//ObjMove_AddPatternA2( obj, 0, -3.5, 90, 0.05, 0, 3 );
		}
		loop(GetCommonData("Bomb",0)){
		let x = GetStgFrameWidth/2+rand( -50, 50 );
		let y = GetStgFrameHeight/2 + rand( -50, 50 );
		let obj = CreateItemA1(ITEM_SPELL ,x,y,5000);
		let obj1 = CreateItemU1(BOMB,x,y,5000);
		LenenItem( obj, 102, 255, 102, 1.25 );
		//ObjMove_AddPatternA2( obj, 0, -3.5, 90, 0.05, 0, 3 );
		//ObjMove_AddPatternA2( obj1, 0, -3.5, 90, 0.05, 0, 3 );
			}

		loop(GetCommonData("Life",0)){
		let x = GetStgFrameWidth/2+rand( -50, 50 );
		let y = GetStgFrameHeight/2 + rand( -50, 50 );
		let obj = CreateItemA1(ITEM_1UP ,x,y,5000);
		let obj1 = CreateItemU1(LIFE,x,y,5000);	
		LenenItem( obj, 255, 102, 255, 1.25 );
		//ObjMove_AddPatternA2( obj, 0, -3.5, 90, 0.05, 0, 3 );
		//ObjMove_AddPatternA2( obj1, 0, -3.5, 90, 0.05, 0, 3 );

			}
		}

let spinning = GCSD~"item_10.png";

	task LenenItem( objt, R, G, B, size ){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,31);
	ObjPrim_SetTexture(obj,spinning);
	ObjSprite2D_SetSourceRect(obj,0,0,64,64);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetColor(obj,R,G,B);
	let i = rand( 0, 360 );
	ObjRender_SetScaleXYZ( obj, size, size, 0 );
	ObjRender_SetAlpha( obj, 155 );
	while( !Obj_IsDeleted(objt)){
	ObjRender_SetPosition(obj,ObjMove_GetX( objt ), ObjMove_GetY( objt ),0);
	ObjRender_SetAngleXYZ(obj,0,0,i);
	i += 1;
	yield;
	}
	Obj_Delete(obj);
}
		
	function wait(t){
	loop(t){yield;}
	}
		
function GetDistance(x1,y1,x2,y2){
return((|((x2-x1)^2+(y2-y1)^2)^(1/2)|));
}

task FamExplode(px,py,speed,c){
	let color = GetColor(c);
	let st = rand(0,30);
	TExpan(px,py,6,30,color[0],color[1],color[2]);
	/*ascent(i in 0..12){
		Maple(px,py,speed+speed/(5/3)*sin(i*177),i*30+st,color[0],color[1],color[2]);
	}*/
}

task Maple(px,py,sizespeed,dir,red,green,blue){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GCSD~"img/effect1.png";
	let spd = sizespeed*7;
	let acc = spd/60;
	let acc2 = acc/100;
	let r = 0;
	let a = [0,0,0];
	let angle_s = [0,0,0];
	let alpha = 255;
	let result = 0;
	let color_ratio = [red/90,green/90,blue/90];
	ascent(i in 0..3){
	result = rand(-1,1);
	if(result>0){result = 1;}
	else{result = -1;}
	angle_s[i] = rand(2,4)*result;
	}
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	ObjRender_SetScaleXYZ(obj,sizespeed,sizespeed,sizespeed);
		loop(90)
		{
		ObjRender_SetAngleXYZ(obj,a[0],a[1],a[2]);
		ObjRender_SetPosition(obj,px+r*cos(dir),py+r*sin(dir),0);
		ObjRender_SetColor(obj,red,green,blue);
		r+=spd;
		if(spd>1.5){
		red -= color_ratio[0];
		green -= color_ratio[1];
		blue -= color_ratio[2];
		spd -=acc;
		acc += acc2;
		}
		else{
		red -= color_ratio[0]*4;
		green -= color_ratio[1]*4;
		blue -= color_ratio[2]*4;
		}
		ascent(i in 0..3){
		a[i] = a[i]+angle_s[i];
		}
		yield;
		}
		Obj_Delete(obj);
}

function TExpan(px,py,maxv,t,r,g,b){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GCSD~"img/charge.png";
	let scale = 0;
	let v = 0;
	let malpha = 255/t;
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,px,py,0);
	ObjRender_SetColor(obj,r,g,b);
	Obj_SetRenderPriorityI(obj,25);
	Expan;
		task Expan{
			descent(i in 0..t)
			{
			
			ObjRender_SetAlpha(obj,malpha*i);
			v = (i*maxv/2)/(t*t);
			scale += v;
			ObjRender_SetScaleXYZ(obj,scale,scale,0);
			yield;
			}
			Obj_Delete(obj);
		}
	}

function GetColor(type){
	if(type=="RED"){return [255,32,32];}
	if(type=="ORANGE"){return [255,128,32];}
	if(type=="YELLOW"){return [255,255,32];}
	if(type=="GREEN"){return [32,255,32];}
	if(type=="BLUE"){return [32,32,255];}
	if(type=="PURPLE"){return [255,32,255];}
	if(type=="PINK"){return [255,32,128];}
	if(type=="WHITE"){return [255,255,255];}
	if(type=="AQUA"){return [0,255,255];}
	if(type=="WHITE"){return [255,255,255];}
	}