#TouhouDanmakufu
#Title["Poison Sign 'Poison Fountain'"[Lunatic]]
#Text[Medicine fires....arrrr.... the flashy poison. They are very harmful you know.... wait..... they also turn into laser! Beautiful indeed , isn't it? ]
#Image[.\img\spellcard0103.jpg]
#PlayLevel[Lunatic]
#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(950);
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);
frame++;

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 Fountain'", Cut, 0, 0, 205, 410);
	spellcardattack;
	wait(90);
	fire;
}

task fire{
	loop{
	attack;
	Concentration01(60);
	wait(60);
	stopattack;
	PlaySE(CSD~"sound\bulletwave.wav");
	ran = rand_int(0,29);
	ascent(i in 0..20){
	Bullet(GetX,GetY,3.5,i*18+GetAngleToPlayer,224,0,0);
	}
	ascent(i in 0..18){
	ascent(a in 0..3){
	CreateShotA(0,GetX+50*cos(i*20),GetY+50*sin(i*20),15);
	SetShotDataA(0,0,1+a/3,i*20,-1*switch,0,0,28);
	SetShotDataA(0,60,1+a/3,NULL,0,0,0,28);
	FireShot(0);
	}
	}
	wait(180);
	SetMovePositionRandom01(rand(20,50),0,2,GetClipMinX+100,rand(150,160),GetClipMaxX-100,rand(100,150));
	checkmove;
	switch = -switch;
	wait(210);
	}
	}


task Bullet(x, y, v, dir, graphic, delay,check) 
	{
    	 let obj = Obj_Create(OBJ_SHOT);
	 let c = 0;
	let count = 0;
    	 Obj_SetPosition   (obj, x, y);
    	 Obj_SetSpeed      (obj, v);
    	 Obj_SetAngle      (obj, dir);
    	 ObjShot_SetGraphic(obj, graphic);
    	 ObjShot_SetDelay  (obj, delay);
	Obj_SetAlpha(obj,0);
	ObjShot_SetBombResist(obj, true);
	effect;
	 task effect{
	 loop(55){
		CreateShotA(0,Obj_GetX(obj),Obj_GetY(obj),30);
		SetShotDataA(0,0,0,0,0,0,0,58);
		SetShotKillTime(0,0);
		FireShot(0);
		yield;
		wait(2);
		}
		if(dir>0 && dir<120){
		Concentration01(60);
		}
		PlaySE(CSD~"sound\Charge2.wav");
		wait(30);
		//lightlaser(Obj_GetX(obj), Obj_GetY(obj)-30, 0, 270,500,20, 47, 40,0);
		wait(30);
		PlaySE(CSD~"sound\stars.wav");
		if(! Obj_BeDeleted(obj)){
		ascent(i in 0..12){
		ascent(a in 0..1){
		CreateShot01(Obj_GetX(obj),Obj_GetY(obj),1,195+i*30,58,10);
		}
		}
		Explosion01(Obj_GetX(obj),Obj_GetY(obj),3,0.05,120);
		}
		flash(Obj_GetX(obj),Obj_GetY(obj),15,270,229,0,0);
		Obj_Delete(obj);
	                }
		 while(! Obj_BeDeleted(obj)) 
		{
		Obj_SetAngle(obj,Obj_GetAngle(obj)+0.5*switch);
		 if(Obj_GetY(obj)>=GetClipMaxY && count == 0){
		Explosion01(Obj_GetX(obj),Obj_GetY(obj),3,0.03,120);
		count = 1;
		Obj_SetSpeed(obj,0);
		}
		 yield;
		}
	}

task flash(x, y, v, dir, graphic, delay,check) 
	{
    	 let obj = Obj_Create(OBJ_SHOT);
	 let c = 0;
	 let count = 0;
    	 Obj_SetPosition   (obj, x, y);
    	 Obj_SetSpeed      (obj, v);
    	 Obj_SetAngle      (obj, dir);
    	 ObjShot_SetGraphic(obj, graphic);
    	 ObjShot_SetDelay  (obj, delay);
	ObjShot_SetBombResist(obj, true);
	Obj_SetAlpha(obj,0);
	task shot{
	 let x = Obj_GetX(obj);
	 let y = Obj_GetY(obj);
	CreateShotA(0,x,y,25);
	SetShotDataA(0,0,0,0,0,0,0,3);
	SetShotKillTime(0,90);
	FireShot(0);
	}
	task effect{
	 let x = Obj_GetX(obj);
	  let y = Obj_GetY(obj);
	 loop(30){
		CreateShotA(0,x,y,25);
		SetShotDataA(0,0,0,0,0,0,0,58);
		SetShotKillTime(0,0);
		FireShot(0);
		yield;
		wait(3);
		}
		if(c%4==0){
		PlayDelaySE(CSD~"sound\shot5.wav",30);
		poi(x,y);
		}
	}
		 while(! Obj_BeDeleted(obj)) 
		{
		if(c%2==0){effect;}
		if(c%1==0){shot;}
		if(Obj_GetY(obj)<GetClipMinY && count == 0 && Obj_GetX(obj)<GetClipMaxX+10 && Obj_GetX(obj)>GetClipMinY-10){
		Explosion01(Obj_GetX(obj),Obj_GetY(obj),3,0.12,120);
		loop(20){
		CreateShotA(0,Obj_GetX(obj),Obj_GetY(obj),10);
		SetShotDataA(0,0,rand(0.5,3),rand_int(-30,210),0,0,0,3);
		FireShot(0);
		loop(3){
		CreateShotA(0,Obj_GetX(obj),Obj_GetY(obj),10);
		SetShotDataA(0,0,rand(0.5,2.5),rand_int(-30,210),0,0,0,248);
		FireShot(0);
		}
		loop(2){
		CreateShotA(0,Obj_GetX(obj),Obj_GetY(obj),10);
		SetShotDataA(0,0,rand(0.5,2.75),rand_int(-30,210),0,0,0,224);
		FireShot(0);
		}
		}
		count++;
		}
		c++;
		 yield;
		}
	}

	   task lightlaser(x, y, v, dir,l,w, graphic, delay,check) {
	   let obj = Obj_Create(OBJ_LASER);
 	   let c = 0;
 	   Obj_SetPosition   (obj, x, y);
  	  Obj_SetSpeed      (obj, v);
 	   ObjShot_SetGraphic(obj, graphic);
  	  ObjShot_SetDelay  (obj, delay);
 	   Obj_SetAngle      (obj, dir);
 	   ObjLaser_SetLength(obj,l);
    	ObjLaser_SetWidth (obj,w);
    	ObjLaser_SetSource(obj,false);
  	  while(! Obj_BeDeleted(obj)) {
		c++;
		if(c>30){ObjShot_FadeDelete(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(60){c+=0.01; yield;}
	loop(30){c-=0.02; 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;}
			}
}