#TouhouDanmakufu
#Title["Poison Sign 'Poison Bullets'"]
#Text[Medicine fires some bullets which turn into poison when they hit you! Their speed is very high , so it's too hard that you can dodge all of them. Also be aware those following pellets !]
#Image[.\img\spellcard0103.jpg]
#PlayLevel[Normal]
#ScriptVersion[2]

script_enemy_main
{
#include_function ".\system\Effects.txt";
#include_function ".\cutin.txt";
#include_function ".\MediEffect.txt";
#include_function ".\draweverything.txt";
let CSD = GetCurrentScriptDirectory;
let poison = CSD~"img\shot_poison.png";
let frame = 0;
let angle = 0;
let angle2 = 0;
let r = 0;
let arr = [37,39,41,43];
let switch = 1;
let ran = 0;
let cx = GetCenterX();
SetCommonData("back",1);
@Initialize
{
SetLife(750);
SetDamageRate(20,10);
SetTimer(60);
mainTask;
MediStart;
SetMovePosition03(GetCenterX, 120, 10, 4);
MagicCircle(false);
LoadGraphic(poison);
Aura;
}

//---------------------------------------------------------------

@MainLoop 
{

SetCollisionA(GetX, GetY, 32);
SetCollisionB(GetX, GetY, 16);
SetGraphicScale(1.2,1.2);

yield;

}

//---------------------------------------------------------------

@DrawLoop
{
	drawall;
}
//---------------------------------------------------------------

@BackGround
{
	draw2;
}	

//---------------------------------------------------------------

@Finalize
{
DeleteGraphic(ImgBoss);
}

// main task
task mainTask{
	checkmove;
	wait(60);
	Border();
	drawcc(GetX,GetY+120,0,0);
	PlaySE(GetCurrentScriptDirectory~"cardcall.wav");
	SetScore(500000);
	cutin("KANAKO", "Poison Sign 'Poison Bullets'", Cut, 0, 0, 205, 410);
	spellcardattack;
	wait(90);
	Concentration01(60);
	PlaySE(CSD~"sound\Charge2.wav");
	wait(60);
	fire;
}

task fire{
	shoot;
	ascent(n in 0..15){
	PlaySE(CSD~"sound\shot1.wav");
	ascent(i in 0..24){
	Bullet(GetX,GetY,5-n/15,i*15+25*sin(n*37)+n*10,205,10,0);
	}
	yield;
	wait(4);
	}
	stopshoot;
	wait(30-frame);
	fire2;
	}

task fire2{
	ran =rand_int(0,3);
	ascent(n in 0..35){
	PlaySE(CSD~"sound\shot1.wav");
	ascent(a in 0..3){
	ascent(i in 0..3){
	CreateShotA(0,GetX+30*cos(i*90+angle),GetY+30*sin(i*90+angle),10);
	SetShotDataA(0,0,2.5-a/2,i*90+angle,0,0,0,63+i);
	FireShot(0);
	}
	CreateShotA(0,GetX+30*cos(270+angle),GetY+30*sin(270+angle),10);
	SetShotDataA(0,0,2.5-a/2,270+angle,0,0,0,58);
	FireShot(0);
	}
	angle += arr[ran]*switch;
	wait(2);
	}
	switch = -switch;
	SetMovePositionRandom01(rand(20,50),0,2,GetClipMinX+100,rand(150,160),GetClipMaxX-100,rand(100,150));
	wait(30-frame);
	frame++;
	fire;
	}

task Bullet(x, y, v, dir, graphic, delay,check) 
	{
    	 let obj = Obj_Create(OBJ_SHOT);
	 let c = 0;
    	 Obj_SetPosition   (obj, x, y);
    	 Obj_SetSpeed      (obj, v);
    	 Obj_SetAngle      (obj, dir);
    	 ObjShot_SetGraphic(obj, graphic);
    	 ObjShot_SetDelay  (obj, delay);
	 Obj_SetCollisionToPlayer(obj,false);
	 Obj_SetAlpha(obj,100);
		 while(! Obj_BeDeleted(obj)) 
		{
		if(Collision_Circle_Circle(Obj_GetX(obj), Obj_GetY(obj), 5, GetPlayerX, GetPlayerY, 10)){
		PlaySE(CSD~"sound\shot5.wav");
		poi(Obj_GetX(obj),Obj_GetY(obj));
		Obj_Delete(obj);
		}
		 yield;
		}
	}

task poi(x,y)
	{
    	 let e = Obj_Create(OBJ_EFFECT);
	 let c = 0;
    	 Obj_SetPosition   (e, x, y);
	 ObjEffect_SetScale(e,1,1);
	 ObjEffect_SetTexture(e,poison);
	 ObjEffect_SetRenderState(e,ADD);
	ObjEffect_SetLayer(e,3);
	 ObjEffect_SetPrimitiveType(e,PRIMITIVE_TRIANGLESTRIP);
	 ObjEffect_CreateVertex(e, 4);
	Obj_SetAlpha(e,255);
	ObjEffect_SetVertexXY(e,0,-128,-128);
	ObjEffect_SetVertexXY(e,1,128,-128);
	ObjEffect_SetVertexXY(e,2,-128,128);
	ObjEffect_SetVertexXY(e,3,128,128);
	ObjEffect_SetVertexUV(e,0,0,0);
	ObjEffect_SetVertexUV(e,1,256,0);
	ObjEffect_SetVertexUV(e,2,0,256);
	ObjEffect_SetVertexUV(e,3,256,256);
	size;
	task size{
	loop(150){c+=0.005; yield;}
	loop(60){c-=0.0125; yield;}
	Obj_Delete(e);
	}
		 while(! Obj_BeDeleted(e)) 
		{
		ObjEffect_SetScale(e,c,c);
		yield;
		}
	}

function Collision_Circle_Circle(x1, y1, r1, x2, y2, r2){
    let distance = ((x1-x2)^2+(y1-y2)^2)^0.5;
    return distance < r1+r2;
}

	function Obj_GetAngleToPlayer(obj) {
	return atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj));
	}

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