let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let csd = GetCurrentScriptDirectory();
let img=csd~"img/effbreak.png";
let i_time = 0;
let DELETE = 1;
let SCORE = 2;
@Initialize
{
SetDefaultBonusItemEnable(false);
LoadTexture(csd~"img/effbreak.png");
LoadTexture(img);
LoadItemData(csd~"Item_Data.txt");
}

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

@Event
{
	alternative(GetEventType())
	
	case(EV_GET_ITEM){
		if(GetEventArgument(0)==DELETE){
		AddScore(75);
		}
	}
	case(EV_DELETE_SHOT_TO_ITEM){
	let obj = GetEventArgument(0);
	let point = GetEventArgument(1);
	BulletBreak([ObjMove_GetX(GetEventArgument(0)), ObjMove_GetY(GetEventArgument(0))],rand(0,360),ObjShot_GetImageID(GetEventArgument(0)),BLEND_ALPHA);
	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]);
			}
			
			
		}
	}
}

task BulletBreak(pos,angle,ImgID,Blend){

	let ID		= ObjPrim_Create(OBJ_SPRITE_2D);
	let animC	= 0;
	let anim1	= 0;
	let anim2	= 0;
	let rectX	= 128;
	let rectY	= 128;
	
	ObjRender_SetPosition(ID, pos[0], pos[1], 0 );
	ObjPrim_SetTexture(ID, GetCurrentScriptDirectory~"/img/bulletbreak00.png");
	Obj_SetRenderPriorityI(ID, 50);
	ObjRender_SetBlendType(ID, Blend );
	ObjRender_SetAngleXYZ(ID, 0, 0, rand(0,360) );
	ObjRender_SetAlpha(ID, 192 );
	ObjSprite2D_SetSourceRect(ID, 0 + rectX*anim1, 0 + rectY*anim2, rectX + rectX*anim1, rectY + rectY*anim2 );
	ObjSprite2D_SetDestCenter(ID);
	
	ShotColor(ID, ImgID, Blend );
	
	let sca	= 0.7;
	let pn	= 0;
	if( Blend == BLEND_ADD_ARGB ){
		sca += 0.1;
		ObjRender_SetScaleXYZ(ID, sca, sca, 0 );
	}
	else{
		ObjRender_SetScaleXYZ(ID, sca/1.2, sca/1.2, 0 );
	}
	
	while( anim2 < 2 ){
		ObjSprite2D_SetSourceRect(ID, 0 + rectX*anim1, 0 + rectY*anim2, rectX + rectX*anim1, rectY + rectY*anim2 );
		
		
		animC++;
		if( animC > 0 && animC%3 == 0 ){
			if( anim1 >= 3 ){
				anim1 = 0;
				anim2++;
			}
			anim1++;
		}
		
		yield;
	}
	Obj_Delete(ID);
}



task ShotColor(ID,ImgID,Blend){

let color = GetShotDataInfoA1(ImgID,TARGET_ENEMY,INFO_DELAY_COLOR);

	
	ObjRender_SetColor(ID, color[0], color[1], color[2] );
}

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);
	Obj_SetRenderPriorityI(obj, 42);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	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 PlaySound(str,v,w){
	let s = ObjSound_Create;
	if(str == "item"){ObjSound_Load(s,csd~"sound/item.wav");}
	if(str == "extend"){ObjSound_Load(s,csd~"sound/extend.wav");}
	if(str == "bonus"){ObjSound_Load(s,csd~"sound/bonus.wav");}
	if(str == "bonus2"){ObjSound_Load(s,csd~"sound/bonus2.wav");}
	loop(w){yield;}
	ObjSound_SetSoundDivision(s,SOUND_SE);
	ObjSound_SetVolumeRate(s,v);
	ObjSound_Play(s);
}
		

		
function GetDistance(x1,y1,x2,y2){
return((|((x2-x1)^2+(y2-y1)^2)^(1/2)|));
}