#include "./System_Effect.dnh"

function wait(w){ loop(w){yield;} }

task ConcA1(enemyObj,type,colour,radius,length){
	let dir = 0;
	let dir2 = 0;
	
	let duration = 1;
	let sound;
	
	alternative(length)
	case("Short"){
		duration = 3;
		sound = "se_ch02";
	}
	case("Long"){
		duration = 9;
		sound = "se_ch00";
	}
	
	let number = 7;
	let scale = radius/480;
	let intensity = 1.75;
	let speed = 3;
	let increase = 0.2;
	
	if(length == "Short" || length == "Long"){
		NotifyEventAll(PLAY_SFX,[sound,IntToString(75)~""]);
	}
	
	loop(duration){
		ConcCircle(enemyObj,ObjMove_GetX(enemyObj),ObjMove_GetY(enemyObj),7,radius,colour);
		loop(number){
			if(!Obj_IsDeleted(enemyObj)){
				let obj1 = Star1(enemyObj,dir,type,number,radius,scale,intensity,speed,increase,colour);
				dir+=360/number;
			}
		}
		wait(5);
		loop(number){
			if(!Obj_IsDeleted(enemyObj)){
				let obj2 = Star2(enemyObj,dir2,type,number,radius,scale,intensity,speed,increase,colour);
				dir2-=360/number;
			}
		}
		wait(5);
	}
	function Star1(enemyObj,ang,type,number,radius,scale,intensity,speed,increase,colour){
		let spin = 0;
		let a = speed;
  
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let path = GetCurrentScriptDirectory ~ "./img/" ~ type ~ ".png";
		ObjPrim_SetTexture(obj,path);
		Obj_SetRenderPriorityI(obj,41);
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		ObjSprite2D_SetSourceRect(obj,0,0,GetTextureWidth(path),GetTextureHeight(path));
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetAlpha(obj,0);
		spinstar;
		scalestar;
		alphastar;
		colorstar;
		posistar;
		
		task posistar{
			let s = 0;
			let e = 0;
			while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
				ObjRender_SetPosition(obj,ObjMove_GetX(enemyObj)+(radius-e*(radius/240))*cos(dir+s),ObjMove_GetY(enemyObj)+(radius-e*(radius/240))*sin(dir+s),0);
				dir+=360/number;
				a+=increase;
				s+=intensity;
				e+=a;
				yield;
			}
			Obj_Delete(obj);
		}
		task spinstar{
			while(!Obj_IsDeleted(obj)){
				ObjRender_SetAngleZ(obj,ang+spin);
				spin-=4;
				yield;
			}
		}
		task alphastar{
			let alp = 0;
			while(alp<255){
				ObjRender_SetAlpha(obj,alp);
				alp+=a*2;
				yield;
			}
			while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0 && alp>0){
				ObjRender_SetAlpha(obj,alp);
				alp-=a;
				yield;
			}
			Obj_Delete(obj);
		}
		task scalestar{
			let s = scale;
			while(s>0){
				ObjRender_SetScaleXYZ(obj,s,s,s);
				s-=scale/(255/a);
				yield;
			}
		}
		task colorstar{
			let color = 255;
			let color2 = 255;
			alternative(colour)
			case("RED"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,255,color,color);
					color-=a;
					yield;
				}
			}
			case("ORANGE"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,255,color2,color);
					color-=a;
					color2-=a/2;
					yield;
				}
			}
			case("YELLOW"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,255,255,color);
					color-=a;
					yield;
				}
			}
			case("LGREEN"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,color2,255,color);
					color-=a;
					color2-=a/2;
					yield;
				}
			}
			case("GREEN"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,color,255,color);
					color-=a;
					yield;
				}
			}
			case("AQUA"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,color,color2,255);
					color-=a;
					color2-=a/2;
					yield;
				}
			}
			case("BLUE"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,color,color,255);
					color-=a;
					yield;
				}
			}
			case("PURPLE"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,255,color,color2);
					color-=a;
					color2-=a/2;
					yield;
				}
			}
			case("PINK"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,255,color,255);
					color-=a;
					yield;
				}
			}
		}	
		return obj;
		Obj_Delete(obj);
	}
	function Star2(enemyObj,ang,type,number,radius,scale,intensity,speed,increase,colour){
		let spin = 0;
		let a = speed;
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let path = GetCurrentScriptDirectory ~ "./img/" ~ type ~ ".png";
		ObjPrim_SetTexture(obj,path);
		Obj_SetRenderPriorityI(obj,41);
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		ObjSprite2D_SetSourceRect(obj,0,0,GetTextureWidth(path),GetTextureHeight(path));
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetAlpha(obj,0);
		spinstar;
		scalestar;
		alphastar;
		colorstar;
		posistar;
		
		task posistar{
			let s = 0;
			let e = 0;
			while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
				ObjRender_SetPosition(obj,ObjMove_GetX(enemyObj)+(radius-e*(radius/240))*cos(dir+s),ObjMove_GetY(enemyObj)+(radius-e*(radius/240))*sin(dir+s),0);
				dir+=360/number;
				a+=increase;
				s-=intensity;
				e+=a;
				yield;
			}
			Obj_Delete(obj);
		}
		task spinstar{
			while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
				ObjRender_SetAngleZ(obj,ang+spin);
				spin-=4;
				yield;
			}
		}
		task alphastar{
			let alp = 0;
			while(alp<255){
				ObjRender_SetAlpha(obj,alp);
				alp+=a*2;
				yield;
			}
			while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0 && alp>0){
				ObjRender_SetAlpha(obj,alp);
				alp-=a;
				yield;
			}
			Obj_Delete(obj);
		}
		task scalestar{
			let s = scale;
			while(s>0){
				ObjRender_SetScaleXYZ(obj,s,s,s);
				s-=scale/(255/a);
				yield;
			}
		}
	task colorstar{
			let color = 0;
			let color2 = 128;
			alternative(colour)
			case("RED"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,255,color,color);
					color+=a;
					yield;
				}
			}
			case("ORANGE"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,255,color2,color);
					color+=a;
					color2+=a/2;
					yield;
				}
			}
			case("YELLOW"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,255,255,color);
					color+=a;
					yield;
				}
			}
			case("LGREEN"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,color2,255,color);
					color+=a;
					color2+=a/2;
					yield;
				}
			}
			case("GREEN"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,color,255,color);
					color+=a;
					yield;
				}
			}
			case("AQUA"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,color,color2,255);
					color+=a;
					color2+=a/2;
					yield;
				}
			}
			case("BLUE"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,color,color,255);
					color+=a;
					yield;
				}
			}
			case("PURPLE"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,255,color,color2);
					color+=a;
					color2+=a/2;
					yield;
				}
			}
			case("PINK"){
				while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
					ObjRender_SetColor(obj,255,color,255);
					color+=a;
					yield;
				}
			}
		}	
		return obj;
		Obj_Delete(obj);
	}
}

task ConcA1_Rainbow(enemyObj,type,radius){
	let duration = 1;
	let number = 7;
	let scale = radius/480;
	let intensity = 1.75;
	let speed = 3;
	let increase = 0.2;
	let radiusincrease = 5;
	
	let t = 0;
	loop(duration){
		if(!Obj_IsDeleted(enemyObj)){
		
			NotifyEventAll(PLAY_SFX,["se_ch00",IntToString(75)~""]);
			ConcA1(enemyObj,type,"RED",radius+t,"");
			ConcCircle(enemyObj,ObjMove_GetX(enemyObj),ObjMove_GetY(enemyObj),7,radius+t,"RED");
			wait(10);
			t+=radiusincrease;
		}
		if(!Obj_IsDeleted(enemyObj)){
			ConcA1(enemyObj,type,"ORANGE",radius+t,"");
			ConcCircle(enemyObj,ObjMove_GetX(enemyObj),ObjMove_GetY(enemyObj),7,radius+t,"ORANGE");
			wait(10);
			t+=radiusincrease;
		}
		if(!Obj_IsDeleted(enemyObj)){
			ConcA1(enemyObj,type,"YELLOW",radius+t,"");
			ConcCircle(enemyObj,ObjMove_GetX(enemyObj),ObjMove_GetY(enemyObj),7,radius+t,"YELLOW");
			wait(10);
			t+=radiusincrease;
		}
		if(!Obj_IsDeleted(enemyObj)){
			ConcA1(enemyObj,type,"LGREEN",radius+t,"");
			ConcCircle(enemyObj,ObjMove_GetX(enemyObj),ObjMove_GetY(enemyObj),7,radius+t,"LGREEN");
			wait(10);
			t+=radiusincrease;
		}
		if(!Obj_IsDeleted(enemyObj)){
			ConcA1(enemyObj,type,"GREEN",radius+t,"");
			ConcCircle(enemyObj,ObjMove_GetX(enemyObj),ObjMove_GetY(enemyObj),7,radius+t,"GREEN");
			wait(10);
			t+=radiusincrease;
		}
		if(!Obj_IsDeleted(enemyObj)){
			ConcA1(enemyObj,type,"AQUA",radius+t,"");
			ConcCircle(enemyObj,ObjMove_GetX(enemyObj),ObjMove_GetY(enemyObj),7,radius+t,"AQUA");
			wait(10);
			t+=radiusincrease;
		}
		if(!Obj_IsDeleted(enemyObj)){
			ConcA1(enemyObj,type,"BLUE",radius+t,"");
			ConcCircle(enemyObj,ObjMove_GetX(enemyObj),ObjMove_GetY(enemyObj),7,radius+t,"BLUE");
			wait(10);
			t+=radiusincrease;
		}
		if(!Obj_IsDeleted(enemyObj)){
			ConcA1(enemyObj,type,"PURPLE",radius+t,"");
			ConcCircle(enemyObj,ObjMove_GetX(enemyObj),ObjMove_GetY(enemyObj),7,radius+t,"PURPLE");
			wait(10);
			t+=radiusincrease;
		}
		if(!Obj_IsDeleted(enemyObj)){
			ConcA1(enemyObj,type,"PINK",radius+t,"");
			ConcCircle(enemyObj,ObjMove_GetX(enemyObj),ObjMove_GetY(enemyObj),7,radius+t,"PINK");
			wait(10);
			t+=radiusincrease;
		}
	}
}

task ConcA2(enemyObj,length){
	let duration;
	let waitduration;
	let sound;
	alternative(length)
	case("Short"){
		duration = 6;
		waitduration = 3;
		sound = "se_ch02";
	}
	case("Long"){
		duration = 10;
		waitduration = 3;
		sound = "se_ch00";
	}
	
	let scale = 2;
	let decrease = 0.7;

	if(length == "Short" || length == "Long"){
		NotifyEventAll(PLAY_SFX,[sound,IntToString(75)~""]);
	}
		ascent(i in 0..duration){
			if(ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
				Circle(enemyObj,scale,decrease,sound,waitduration*i);
			}
		}
	task Circle(enemyObj,scale,decrease,sound,delay){
		let spin = 0;
		
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let path = GetCurrentScriptDirectory ~ "./img/Inverse.png";
		ObjPrim_SetTexture(obj,path);
		Obj_SetRenderPriorityI(obj,42);
		ObjRender_SetBlendType(obj,BLEND_INV_DESTRGB);
		ObjSprite2D_SetSourceRect(obj,0,0,GetTextureWidth(path),GetTextureHeight(path));
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetPosition(obj,ObjMove_GetX(enemyObj),ObjMove_GetY(enemyObj),0);
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		
		scalestar;
		
		task scalestar{
			let sca = scale;
			let s = 0;
			wait(delay);
			while(sca>0 && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
				ObjRender_SetScaleXYZ(obj,sca,sca,sca);
				s+=decrease;
				sca-=sca/(255/s);
				yield;
			}
			Obj_Delete(obj);
		}
		
		
	}
}

task ConcObject(type,x,y,speed,dir,rotation,radius,opacity,color){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let path = GetCurrentScriptDirectory ~ "./img/" ~ type ~ ".png";
		ObjPrim_SetTexture(obj,path);
		Obj_SetRenderPriorityI(obj,41);
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		ObjSprite2D_SetSourceRect(obj,0,0,GetTextureWidth(path),GetTextureHeight(path));
		ObjSprite2D_SetDestCenter(obj);
	
	if(radius>200){ scale = 1; }
	let Red = color[0];
	let Green = color[1];
	let Blue = color[2];
	let scale = 0.75*(radius/200);
	let spin = rand(0,180);
	let alpha = 0;
	
	while(!Obj_IsDeleted(obj)){
		ObjRender_SetPosition(obj,x+radius*cos(dir),y+radius*sin(dir),0);
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		ObjRender_SetAngleZ(obj,spin);
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetColor(obj,Red*(alpha/255),Green*(alpha/255),Blue*(alpha/255));
	
		if(alpha<opacity && radius>=10){ alpha+=opacity/90*speed; }
		if(radius<200){ scale-=0.75/200*speed; }
		if(radius<10){ alpha-=opacity/10*speed; }
		if(alpha<=0){ Obj_Delete(obj); }
		spin+=rand(3,4)*rotation;
		radius-=speed;
			
		yield;
	}

}

task ConcCircle(enemyObj,x,y,speed,radius,colour){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let path = GetCurrentScriptDirectory ~ "./img/ConcentrationCircle.png";
		ObjPrim_SetTexture(obj,path);
		Obj_SetRenderPriorityI(obj,41);
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		ObjSprite2D_SetSourceRect(obj,0,0,GetTextureWidth(path),GetTextureHeight(path));
		ObjSprite2D_SetDestCenter(obj);
	
	let alpha = 0;
	let increase = 0;
	let scale = 2.5*(radius/200);
	colourcircle;
	
	task colourcircle{
		let color = 0;
		let color2 = 0;
	
		alternative(colour)
		case("RED"){
				ObjRender_SetColor(obj,255,color,color);
		}
		case("ORANGE"){
				ObjRender_SetColor(obj,255,color2,color);
		}
		case("YELLOW"){
				ObjRender_SetColor(obj,255,255,color);
		}
		case("LGREEN"){
				ObjRender_SetColor(obj,color2,255,color);
		}
		case("GREEN"){
				ObjRender_SetColor(obj,color,255,color);
		}
		case("AQUA"){
				ObjRender_SetColor(obj,color,color2,255);
		}
		case("BLUE"){
				ObjRender_SetColor(obj,color,color,255);
		}
		case("PURPLE"){
				ObjRender_SetColor(obj,255,color,color2);
		}
		case("PINK"){
				ObjRender_SetColor(obj,255,color,255);
		}	
	}
		
	while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(enemyObj,INFO_LIFE)>0){
		ObjRender_SetPosition(obj,x,y,0);
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		if(alpha<255){ alpha+=96/(radius-90)*speed+increase; }
		increase+=0.0001;
		scale-=speed/100+increase;
		if(scale<=0){ Obj_Delete(obj); }
		
		yield;
	}
	Obj_Delete(obj);
}