#include"./../System/LIB/Event_Const.dnh"


task TimeStopB1(obj, length){
	SetCommonData("TimeStop",true);
	yield;
	
	let renderTexture = GetReservedRenderTargetName(0);
	let objCurentFrame = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetPosition(objCurentFrame, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	let frameskip = 0;
	let PlayerCoord = [GetPlayerX, GetPlayerY];
	Obj_SetRenderPriorityI(objCurentFrame, 35);
	SetInvalidRenderPriorityA1(20, 34);
	
	let currentFrameTexture = renderTexture;
	RenderToTextureA1(currentFrameTexture, 20, 34, true);
	ObjPrim_SetTexture(objCurentFrame, currentFrameTexture);
	ObjSprite2D_SetSourceRect(objCurentFrame, 40, 16, 424, 464);
	ObjSprite2D_SetDestCenter(objCurentFrame);
	
	let objScene = GetEnemyBossSceneObjectID;
	let rateOrg1 = ObjEnemy_GetInfo(obj,INFO_DAMAGE_RATE_SHOT);
	let rateOrg2 = ObjEnemy_GetInfo(obj,INFO_DAMAGE_RATE_SPELL);
	let timerOrg = ObjEnemyBossScene_GetInfo(objScene,INFO_TIMERF);
	
	while(length>0&&!Obj_IsDeleted(obj)){
		SetForbidPlayerSpell(true);
		ObjMove_SetPosition(GetPlayerObjectID, PlayerCoord[0], PlayerCoord[1]);
		ObjEnemyBossScene_SetSpellTimer(objScene,timerOrg);
		ObjEnemy_SetDamageRate(obj,0,0);
		// SetPlayerSpeed(0,0);
		length--;
		yield;
	}
	SetForbidPlayerSpell(false);
	ObjEnemy_SetDamageRate(obj,rateOrg1,rateOrg2);
	
	Obj_Delete(objCurentFrame);
	SetCommonData("TimeStop",false);
	ClearInvalidRenderPriority;
}


function SetMovePositionRandom(obj,xdist,ydist,weight,maxspeed,left,right,top,bottom) {
	let destangle = rand(0,360);
	let destx = ObjMove_GetX(obj)+xdist*RandNumber;
	let desty = ObjMove_GetY(obj)+ydist*RandNumber;
	
	loop(200) {
		if(destx<left || destx>right || desty<top || desty>bottom) {
			destx = ObjMove_GetX(obj)+xdist*RandNumber;
			desty = ObjMove_GetY(obj)+ydist*RandNumber;
		}
		else {
			break;
		}
	}
	ObjMove_SetDestAtWeight(obj, destx, desty, weight, maxspeed);
	function RandNumber {
		let num = 0;
		while(num==0) {
			num = round(rand(-1,1));
		}
		return num;
	}
}
task SetInvincibilityDamageRate(obj,frames,ShotRate,BombRate) {
	loop(frames) {
		ObjEnemy_SetDamageRate(obj,0,0);
		yield;
	}
	ObjEnemy_SetDamageRate(obj,ShotRate,BombRate);
}
task CreateItems01(num,type,x,y,disperse) {
	let objEnemy = ID_INVALID;
	loop(num) {
		NotifyEventAll(EV_CREATE_ITEM,[objEnemy,type,x,y,disperse]);
	}
}
task CreateItems02(objEnemy,num,type,x,y,disperse) {
	loop(num) {
		NotifyEventAll(EV_CREATE_ITEM,[objEnemy,type,x,y,disperse]);
	}
}
function SpellPieces01(objScene,objEnemy,x,y) {
	let pieces = 2;
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_LAST_SPELL)) { pieces=4; }
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_PLAYER_SPELL_COUNT)>0) { pieces-=1; }
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_PLAYER_SHOOTDOWN_COUNT)>0) { pieces-=2; }
	if(pieces==1) {
		PlaySoundEffect(bonus,100);
		CreateItems01(1,8,x,y,0);
	}
	if(pieces==2) {
		PlaySoundEffect(bonus,100);
		CreateItems01(1,8,x+20,y,0);
		CreateItems01(1,8,x-20,y,0);
	}
	if(pieces==3) {
		PlaySoundEffect(bonus,100);
		CreateItems01(1,8,x+20,y+10,0);
		CreateItems01(1,8,x-20,y+10,0);
		CreateItems01(1,8,x,y-10,0);
	}
	if(pieces==4) {
		PlaySoundEffect(bonus,100);
		CreateItems01(1,8,x+20,y+10,0);
		CreateItems01(1,8,x+20,y+10,0);
		CreateItems01(1,8,x+20,y-10,0);
		CreateItems01(1,8,x+20,y-10,0);
	}
}
function SpellPieces02(objScene,objEnemy) {
	let pieces = 2;
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_LAST_SPELL)) { pieces=4; }
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_PLAYER_SPELL_COUNT)>0) { pieces-=1; }
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_PLAYER_SHOOTDOWN_COUNT)>0) { pieces-=2; }
	if(pieces==1) {
		CreateItems02(objEnemy,1,8,0,0,0);
	}
	if(pieces==2) {
		CreateItems02(objEnemy,1,8,20,0,0);
		CreateItems02(objEnemy,1,8,-20,0,0);
	}
	if(pieces==3) {
		CreateItems02(objEnemy,1,8,20,10,0);
		CreateItems02(objEnemy,1,8,-20,10,0);
		CreateItems02(objEnemy,1,8,0,-10,0);
	}
	if(pieces==4) {
		CreateItems02(objEnemy,1,8,20,10,0);
		CreateItems02(objEnemy,1,8,-20,10,0);
		CreateItems02(objEnemy,1,8,20,-10,0);
		CreateItems02(objEnemy,1,8,-20,-10,0);
	}
}
function LifePieces01(objScene,objEnemy,x,y) {
	let pieces = 2;
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_LAST_SPELL)) { pieces=4; }
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_PLAYER_SPELL_COUNT)>0) { pieces-=1; }
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_PLAYER_SHOOTDOWN_COUNT)>0) { pieces-=2; }
	if(pieces==1) {
		PlaySoundEffect(bonus,100);
		CreateItems01(1,6,x,y,0);
	}
	if(pieces==2) {
		PlaySoundEffect(bonus,100);
		CreateItems01(1,6,x+20,y,0);
		CreateItems01(1,6,x-20,y,0);
	}
	if(pieces==3) {
		PlaySoundEffect(bonus,100);
		CreateItems01(1,6,x+20,y+10,0);
		CreateItems01(1,6,x-20,y+10,0);
		CreateItems01(1,6,x,y-10,0);
	}
	if(pieces==4) {
		PlaySoundEffect(bonus,100);
		CreateItems01(1,6,x+20,y+10,0);
		CreateItems01(1,6,x+20,y+10,0);
		CreateItems01(1,6,x+20,y-10,0);
		CreateItems01(1,6,x+20,y-10,0);
	}
}
function LifePieces02(objScene,objEnemy) {
	let pieces = 2;
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_LAST_SPELL)) { pieces=4; }
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_PLAYER_SPELL_COUNT)>0) { pieces-=1; }
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_PLAYER_SHOOTDOWN_COUNT)>0) { pieces-=2; }
	if(pieces==1) {
		CreateItems02(objEnemy,1,6,0,0,0);
	}
	if(pieces==2) {
		CreateItems02(objEnemy,1,6,20,0,0);
		CreateItems02(objEnemy,1,6,-20,0,0);
	}
	if(pieces==3) {
		CreateItems02(objEnemy,1,6,20,10,0);
		CreateItems02(objEnemy,1,6,-20,10,0);
		CreateItems02(objEnemy,1,6,0,-10,0);
	}
	if(pieces==4) {
		CreateItems02(objEnemy,1,6,20,10,0);
		CreateItems02(objEnemy,1,6,-20,10,0);
		CreateItems02(objEnemy,1,6,20,-10,0);
		CreateItems02(objEnemy,1,6,-20,-10,0);
	}
}
task BossEffectsEx(ID) {
	NotifyEventAll(EV_BOSS_EFFECT,ID);
}
function BulletDeleteEffect(objShot) {
	NotifyEventAll(EV_DELETE_SHOT,[ObjMove_GetX(objShot),ObjMove_GetY(objShot),ObjShot_GetImageID(objShot)]);
}
task DeleteShotWave(type,x,y,speed) {
	NotifyEventAll(EV_DELETE_WAVE,[type,x,y,speed]);
}
function waitEx(obj,w) {
	loop(w) {
		yield;
		if(Obj_IsDeleted(obj)) { break; }
	}
	if(Obj_IsDeleted(obj)) { break; }
}