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;
SetCommonData("ItemStart",1);
@Initialize
{
SetDefaultBonusItemEnable(false);
LoadTexture(GCSD~"img/effbreak.png");
LoadTexture(img);
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("End",5));
	}
	case(EV_GET_ITEM){
	PlaySound("item",80,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));
			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.5,0.5,0.5);
	time = 75-GetCommonData("End",0)*30-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 += 20;}
		if(type==1){wave+=500;}
		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<500){
			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/AllStarShot.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>=41){
		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,stgx+rand(-100,100),stgy+rand(-100,50),1000);
		ObjItem_SetRenderScoreEnable(obj,false);
		}
			loop(GetCommonData("Bomb",0)){
			CreateItemA1(ITEM_SPELL,stgx+rand(-50,50),stgy+rand(-50,50),1);
			}
			loop(GetCommonData("Life",0)){
			CreateItemA1(ITEM_1UP,stgx+rand(-50,50),stgy+rand(-50,50),1);
			}
		}
		
task PlaySound(str,v,w){
	let s = ObjSound_Create;
	if(str == "item"){ObjSound_Load(s,GCSD~"sound/item.wav");}
	if(str == "extend"){ObjSound_Load(s,GCSD~"sound/extend.wav");}
	if(str == "bonus"){ObjSound_Load(s,GCSD~"sound/bonus.wav");}
	if(str == "bonus2"){ObjSound_Load(s,GCSD~"sound/bonus2.wav");}
	loop(w){yield;}
	ObjSound_SetSoundDivision(s,SOUND_SE);
	ObjSound_SetVolumeRate(s,v);
	ObjSound_Play(s);
}
		
	function wait(t){
	loop(t){yield;}
	}
		
function GetDistance(x1,y1,x2,y2){
return((|((x2-x1)^2+(y2-y1)^2)^(1/2)|));
}