#include "./Index.txt"
task OrinSpread(angle,spread,radius,number,delay,graphic,direction,deletioncheck,loopnumber,addition, acceleration, maxspeed){
	let playX = GetPlayerX;
	let playY = GetPlayerY;
	let mod = 0;
	let mod2 = 1.2;
	let mod3 = 0;
	let del = delay;
	loop(loopnumber){
    loop(number){
        let obj = CreateShotA1(ObjMove_GetX(objBoss)+mod*cos(angle)+24, ObjMove_GetY(objBoss)+mod*sin(angle)-35, 0, angle+direction, graphic, 5);
		SE_Play(GetCurrentScriptDirectory ~ "./sfx/smbullet.wav", 50);
			if(del<1){Go(obj,0,acceleration,maxspeed);}
			else{Go(obj,del,acceleration,maxspeed);}
		
			if(deletioncheck==true){
			GetShotIdInCircleA2(playX,playY,50,TARGET_ALL);
			let arr = GetShotIdInCircleA2(playX,playY,50,TARGET_ALL);
				ascent(i in 0..length(arr)) {
				if(ObjShot_GetImageID(arr[i])==graphic){Obj_Delete(arr[i])};
				}
			}
		angle += 360/number;
		}
	del -= 5;
	angle+=spread+mod3;
	mod3+=0.05*addition;
	mod2 = 2;
	mod += radius*mod2;
	wait(1);
	}
	task Go(obj,delay,acceleration,maxspeed){
	wait(delay);
	ObjMove_AddPatternA4(obj, 0, 0, NO_CHANGE, acceleration, 0, maxspeed, 0, NO_CHANGE);
	SE_Play(GetCurrentScriptDirectory ~ "./sfx/spirit.wav", 75);
	}
}


//********************************************************************

let soulSprite = GetCurrentScriptDirectory ~ "./img/ghost.png";
let breakout = false;

function SummonGhost(x,y,health, life, hitbox, hitboxplayer){
	let rect = 32;
	let frame = 0;
	let scale = 1;
	let lifespan = 0;
	
	let soul = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(soul);
	ObjPrim_SetTexture(soul,soulSprite);
	ObjRender_SetBlendType(soul, BLEND_ALPHA);
	Obj_SetRenderPriorityI(soul, 25);
	ObjEnemy_SetLife(soul,health);
	ObjSprite2D_SetSourceRect(soul,0,rect*2,rect,rect*3);
	ObjSprite2D_SetDestCenter(soul);
	ObjMove_SetPosition(soul,x,y);
	ObjRender_SetAlpha(soul,255);
	ObjRender_SetAngleXYZ(soul,0,0,0);
	ObjRender_SetScaleXYZ(soul,1,1,0);
	Rendering(soul, rect, frame, scale, life, lifespan, hitbox, hitboxplayer);
	Flamecontrol(soul,breakout);
	return soul;
	}
	task Rendering(soul, rect, frame, scale, life, lifespan, hitbox, hitboxplayer){
		while(ObjEnemy_GetInfo(soul,INFO_LIFE) > 0){	
		if(hitbox==true){
		ObjEnemy_SetIntersectionCircleToShot(soul,ObjMove_GetX(soul),ObjMove_GetY(soul),12);
		}
		if(hitboxplayer==true){
		ObjEnemy_SetIntersectionCircleToPlayer(soul,ObjMove_GetX(soul),ObjMove_GetY(soul),8);
		}
		if(frame<8){ObjSprite2D_SetSourceRect(soul,0,rect*2,rect,rect*3);}
		if(frame>=8 && frame<16){ObjSprite2D_SetSourceRect(soul,rect*1,rect*2,rect*2,rect*3);}
		if(frame>=16 && frame<24){ObjSprite2D_SetSourceRect(soul,rect*2,rect*2,rect*3,rect*3);}
		if(frame>=24 && frame<32){ObjSprite2D_SetSourceRect(soul,rect*3,rect*2,rect*4,rect*3);}
		if(frame>=32 && frame<40){ObjSprite2D_SetSourceRect(soul,rect*4,rect*2,rect*5,rect*3);}
		if(frame>=40 && frame<48){ObjSprite2D_SetSourceRect(soul,rect*5,rect*2,rect*6,rect*3);}
		if(frame>=48 && frame<56){ObjSprite2D_SetSourceRect(soul,rect*6,rect*2,rect*7,rect*3);}
		if(frame>=56 && frame<64){ObjSprite2D_SetSourceRect(soul,rect*7,rect*2,rect*8,rect*3);}
		
		frame++;
		lifespan++;
		if(lifespan==life){break;}
		if(frame==(64+8)){frame=0;}
		yield;
	}
if(hitbox==true){
SE_Play(GetCurrentScriptDirectory ~ "./sfx/die.wav", 50);
shockwave(soul);
}
	yield;
	ObjEnemy_SetLife(soul, 1);
	yield; //To avoid 0,0 spawning
	Obj_Delete(soul);
}

task Flamecontrol(soul, breakout){
	while(!Obj_IsDeleted(soul)){
	if(breakout==true){break;}
	Flamey(soul);
	wait(5);
	}
}
	
task Flamey(soul){
	let scale = 1;
	let rect = 32;
	let random = rand(-0.3,0.3);
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,soulSprite);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj, 24);
	ObjSprite2D_SetSourceRect(obj,0,rect*2,rect,rect*3);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,ObjMove_GetX(soul),ObjMove_GetY(soul),0);
	ObjRender_SetAlpha(obj,255);
	ObjRender_SetAngleXYZ(obj,0,0,270+rand(-10,10));
	let X = ObjRender_GetX(obj);
	let Y = ObjRender_GetY(obj);
	
	while(!Obj_IsDeleted(obj)){
	if(ObjRender_GetX(obj)<50 && ObjRender_GetY(obj)<10){Obj_Delete(obj);}
	ObjRender_SetScaleXYZ(obj,scale,scale,0);
	ObjRender_SetPosition(obj,X,Y,0);
	scale-=0.02;
	Y -= 1;
	X += random;
	if(scale<0){break;}
	yield;
	}
	Obj_Delete(obj);
	}
	
//********************************************************************

let FairySprite = GetCurrentScriptDirectory ~ "./img/enemy.png";
function ZombieFairy(x,y,health,respawn, explode){
	let IsDead = false;
	let fairy = ObjEnemy_Create(OBJ_ENEMY);
	let frame = 0;
	ObjEnemy_Regist(fairy);
	ObjPrim_SetTexture(fairy,FairySprite);
	ObjRender_SetBlendType(fairy, BLEND_ALPHA);
	Obj_SetRenderPriorityI(fairy, 25);
	ObjEnemy_SetLife(fairy,health);
	ObjSprite2D_SetSourceRect(fairy,0,32,32,32*2);
	ObjSprite2D_SetDestCenter(fairy);
	ObjMove_SetPosition(fairy,x,y);
	ObjRender_SetAlpha(fairy,255);
	ObjRender_SetAngleXYZ(fairy,0,0,0);
	ObjRender_SetScaleXYZ(fairy,1,1,0);
	Hitbox(fairy);
	Rendering(fairy,frame);
	DeathLoop(fairy,health,respawn, explode);
	BossShotSound(fairy, 100, 0);
	return fairy;
	task Hitbox(fairy){
		while(!Obj_IsDeleted(fairy)){
		if(IsDead==false){
		ObjEnemy_SetIntersectionCircleToShot(fairy,ObjMove_GetX(fairy),ObjMove_GetY(fairy),6);
		ObjEnemy_SetIntersectionCircleToPlayer(fairy,ObjMove_GetX(fairy),ObjMove_GetY(fairy),8);
		}
		yield;
		}
	}
	task Rendering(fairy,frame){
	while(!Obj_IsDeleted(fairy)){
		
		if(IsDead==true){
		frame=0;
		ObjSprite2D_SetSourceRect(fairy,32*4,0,32*5,32);
		ObjRender_SetAlpha(fairy, 122);
		}
		else{
		if(frame<8){ObjSprite2D_SetSourceRect(fairy,0,32,32,32*2);}
		if(frame>=8 && frame<16){ObjSprite2D_SetSourceRect(fairy,32,32,32*2,32*2);}
		if(frame>=16 && frame<24){ObjSprite2D_SetSourceRect(fairy,32*2,32,32*3,32*2);}
		if(frame>=24 && frame<32){ObjSprite2D_SetSourceRect(fairy,32*3,32,32*4,32*2);}
		if(frame>=32 && frame<40){ObjSprite2D_SetSourceRect(fairy,32*4,32,32*5,32*2);}
		ObjRender_SetAlpha(fairy, 255);
		}

		frame++;
		if(frame==(32)){frame=0;}
		wait(2);
		}
	}
	task DeathLoop(fairy,health,respawn, explode){
	while(!Obj_IsDeleted(fairy)){
	while(ObjEnemy_GetInfo(fairy, INFO_LIFE) > 2){yield;}
	if(explode==true){
	shockwave(fairy);
	}
	IsDead = true;
	yield;
	ObjEnemy_SetLife(fairy,1);
	wait(respawn/2);
	DeadFairyFlame(fairy, breakout);
	wait(respawn/2);
	IsDead = false;
	ObjEnemy_SetLife(fairy,health);
	}
	}
	task DeadFairyFlame(obj, breakout){
	while(!Obj_IsDeleted(obj)){
	if(IsDead!=true){break;}
	Flamey(obj);
	wait(7);
	}
	}
	}

//~~~~~~~~~~~~~~~~~~~~~~~~~~

task GlowExplosion(obj, looptimes, colorscale, scale, hitbox, hitboxradius, hitboxdelay, deletebullet, color, deletedelay){
let Glow = CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),0,GetAngleToPlayer(obj),color,0);
ObjShot_SetSpellResist(Glow,1);
ObjShot_SetIntersectionEnable(Glow, 0);
ObjRender_SetBlendType(Glow, BLEND_ADD_ARGB);

	let XRender = ObjRender_GetScaleX(Glow);
	let YRender = ObjRender_GetScaleY(Glow);
	if(deletebullet==true){Obj_Delete(obj);}
	if(hitboxradius == -1){hitboxradius = (ObjRender_GetScaleX(Glow)+ObjRender_GetScaleY(Glow)*10);}
	
	ascent(i in 0..looptimes){
		ObjRender_SetColor(Glow, i*colorscale,i*colorscale,i*colorscale);
		ObjRender_SetScaleXYZ(Glow,XRender+i/scale,YRender+i/scale,0);
		if(i>hitboxdelay && hitbox==true){ObjShot_SetIntersectionCircleA1(Glow, hitboxradius);}
		yield;
	}
	wait(deletedelay);
descent(i in 0..25){
	ObjRender_SetColor(Glow, i*25,i*25,i*25);
	yield;
}
Obj_Delete(Glow);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

task CatSpread(dens, color, mult){
	let angle = rand(0,360);
	let dist = 20;
	let x = ObjMove_GetX(objBoss);
	let y = ObjMove_GetY(objBoss);
		loop(3){
			loop(dens){
			let obj = CreateShotA1(x+dist*cos(angle), y+dist*sin(angle), 0, angle, color, 20);
			delay(obj);
			angle += 360/dens;
			}
		angle+=360/(dens/mult);
		dist+=15;
		wait(10);
		}
	task delay(obj){
	wait(120);
	SE_Play(GetCurrentScriptDirectory ~ "sfx/spirit.wav", 50);
	ObjMove_AddPatternA4(obj, 0, 0, NO_CHANGE, 0.01, 0, 10, 0, NO_CHANGE);
	}
}