// Enemy Effects

task EExplosion(x, y, amount){
	let angle = rand(0,360);
	ERing(x, y, 0.09, 0.09, 0.06, 1, BLEND_ADD_ARGB, rand(50,255), rand(50,255), rand(50,255));
	loop(amount){
		let angvel = [-0.2,0.2][rand_int(0,1)];
		EParticle(x, y, rand(2,7), -0.07, angle, 0.09, 0.09, 0.002, 3, angvel, BLEND_ADD_ARGB, 0, 0, 0);
		EParticle(x, y, rand(2,7), -0.07, angle, 0.09, 0.09, 0.002, 3, angvel, BLEND_ADD_ARGB, rand(50,255), rand(50,255), rand(50,255));
		angle+=360/amount;
	}
}

task EShockwave(x, y, radius){
	ERing(x, y, 0.09, 0.09, radius, 1, BLEND_ADD_ARGB, 255, 255, 255);
}

task ECharge(x, y, amount){
	let angle = rand(0,360);
	let rad = rand(90, 360);
	let size = 0.18+rand(-0.1,0.1);

	loop(96){
		rad = rand(90, 360);
		size = 0.18+rand(-0.1,0.1);
		let angvel = [-0.1,0.1][rand_int(0,1)];
		let p1 = EParticle(x+rad*cos(angle), y+rad*sin(angle), 0, 0, 0, size, size, 0, 0, angvel, BLEND_ADD_ARGB, 200+rand(-60,60), 0+rand(-60,60), 255+rand(-60,60));
		let p2 = EParticle(x+rad*cos(angle), y+rad*sin(angle), 0, 0, 0, size, size, 0, 0, angvel, BLEND_SUBTRACT, 200+rand(-60,60), 0+rand(-60,60), 255+rand(-60,60));
		EAlpha(p1); EAlpha(p2); 
		ERadius(p1, 1, rad, x, y, angle);
		ERadius(p2, 2, rad, x, y, angle);
		angle += 360/96;
	}
}
task EAlpha(p){
	let al = 0;
	ObjRender_SetAlpha(p, al);
	loop(64){
		al += 255/64;
		ObjRender_SetAlpha(p, al);
		yield;
	}
}
task ERadius(p, type, rad, x, y, angle){
	let inc = 0;

	while(rad > 0){
		inc++;
		if(type == 1){ angle += 1; }
		else{ angle -= 1; }
		rad -= inc*rand(0.05, 0.1);
		ObjRender_SetPosition(p, x+rad*cos(angle), y+rad*sin(angle), 0);
		yield;
	}
	Obj_Delete(p);
}

function EParticle(x, y, speed, acceleration, angle, scaleX, scaleY, scaleinc, radius, angvelocity, blend, colorR, colorG, colorB){
	let alInc = 255;
	let scl = 0;

	let img = GetCurrentScriptDirectory ~ "../img/EParticle.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img);
	ObjSprite2D_SetSourceRect(obj, 0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 30);
	ObjRender_SetScaleXYZ(obj, scaleX+scl, scaleY+scl, 0);
	ObjRender_SetAlpha(obj, alInc);
	ObjRender_SetBlendType(obj, blend);
	ObjRender_SetPosition(obj, x, y, 0);
	ObjRender_SetColor(obj, colorR, colorG, colorB);

	ParticleTasks(obj, x, y, speed, acceleration, angle, scaleX, scaleY, scaleinc, radius, angvelocity, alInc, scl);
	return obj;
}
task ParticleTasks(obj, x, y, speed, acceleration, angle, scaleX, scaleY, scaleinc, radius, angvelocity, alInc, scl){
	while(alInc > 0){
		alInc -= radius;
		scl += scaleinc;

		x+=cos(angle)*speed;
		y+=sin(angle)*speed;

		angle+=angvelocity;

		ObjRender_SetPosition(obj, x, y, 0);
		ObjRender_SetAlpha(obj, alInc);
		ObjRender_SetScaleXYZ(obj, scaleX+scl, scaleY+scl, 0);

		if(speed > 1){
			speed+=acceleration;
		}
		yield;
	}
	Obj_Delete(obj);
}

task ERing(x, y, scaleX, scaleY, scaleinc, radius, blend, colorR, colorG, colorB){
	let alInc = 100;
	let scl = 0;

	let img = GetCurrentScriptDirectory ~ "../img/ERing2.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img);
	ObjSprite2D_SetSourceRect(obj, 0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 34);
	ObjRender_SetScaleXYZ(obj, scaleX+scl, scaleY+scl, 0);
	ObjRender_SetAlpha(obj, alInc);
	ObjRender_SetBlendType(obj, blend);
	ObjRender_SetPosition(obj, x, y, 0);
	ObjRender_SetColor(obj, colorR, colorG, colorB);

	while(alInc > 0){
		alInc-=radius;
		scl+= scaleinc;

		ObjRender_SetPosition(obj, x, y, 0);
		ObjRender_SetAlpha(obj, alInc);
		ObjRender_SetScaleXYZ(obj, scaleX+scl, scaleY+scl, 0);

		yield;
	}
	Obj_Delete(obj);
}

// Shot Effects

task EffectTrail1(shot, graphic, frame, blend, alpha, scale){
	while(!Obj_IsDeleted(shot)){
		CreateEffTrail1(shot, graphic, blend, alpha, scale);
		wait(frame);
	}
}

task CreateEffTrail1(shot, graphic, blend, alpha, scale){
	let rect = GetShotDataInfoA1(graphic, TARGET_ENEMY, INFO_RECT);
	let eff = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(eff, "../shot/CrispBright4/Shot.png");
	ObjSprite2D_SetSourceRect(eff, rect[0], rect[1], rect[2], rect[3]);
	ObjSprite2D_SetDestCenter(eff);
	ObjRender_SetPosition(eff, ObjMove_GetX(shot), ObjMove_GetY(shot), 0);
	ObjRender_SetAngleZ(eff, ObjMove_GetAngle(shot)+90);
	ObjRender_SetBlendType(eff, blend);
	ObjRender_SetScaleXYZ(eff, scale, scale, 1);

	//let alpha = 100;
	while(!Obj_IsDeleted(shot)){
		ObjRender_SetAlpha(eff, alpha);
		ObjRender_SetAngleZ(eff, ObjMove_GetAngle(shot)+90);
		alpha-=5;
		if(alpha <= 0){
			Obj_Delete(eff);
		}
		yield;
	}
	Obj_Delete(eff);
}

task SlaveAura{
	while(!Obj_IsDeleted(objBoss)){
		if(Obj_GetValue(objBoss, "isSlave") == 1){ ERing(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0.1, 0.1, 0.03, 2, BLEND_ADD_ARGB, 205, 0, 255); }
		wait(15);
	}
}