#TouhouDanmakufu
#Title[FutoLast_E]
#Text[FutoLast_E]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main {
let imglifebar = GetCurrentScriptDirectory~"lifebar_thWW.png";
	let nums = 34;
	let timer = GetTimer;
let time1 = GetCurrentScriptDirectory~"sfx\time1.wav";
let time2 = GetCurrentScriptDirectory~"sfx\time2.wav";

	let shot = GetCurrentScriptDirectory~"sfx\shot.wav";
	let laser = GetCurrentScriptDirectory~"sfx\laser.wav";
	let frame3 = -60;
	let num = 30;
	let futo = GetCurrentScriptDirectory~"sprites\futo.png";
	let thunder = GetCurrentScriptDirectory~"sfx\thunder.wav";
	let frame = -60;
	let shooting = false;
	let frame2 = -60;
	let shot2 = GetCurrentScriptDirectory~"sfx\shot2.wav";
	let charge = GetCurrentScriptDirectory~"sfx\Charge2.wav";
	let b = 200;
	let a = 0;
let transition = false;
let acc1 = 0;
let acc2 = 0;
let acc3 = 0;
let acc4 = 0;
let acc5 = 0;
let acc6 = 0;
let acc7 = 0;
let anim1 = 0;
let anim2 = 0;
let anim3 = 0;
let waittime = 60;
let SP = GetCurrentScriptDirectory~"sprites\futo.png";
let MC = GetCurrentScriptDirectory~"sprites\MC.png";
let CBG1 = GetCurrentScriptDirectory~"sprites\CBG1.png";
let CBG2 = GetCurrentScriptDirectory~"sprites\CBG2.png";
let Cut = GetCurrentScriptDirectory~"sprites\futonormal.png";
	#include_function ".\cutin.dnh";
	#include_function ".\blossom.txt";

	@Initialize{
Movement;
attack1;
LastSpell;
	SetEffectForZeroLife(60, 100, 2);
	LoadGraphic(imglifebar); 
LoadSE(time1);
LoadSE(time2);
LoadSE(charge);
		PlaySE(GetCurrentScriptDirectory~"cardcall2.wav");
	LoadUserShotData(GetCurrentScriptDirectory~"shot.txt");
	SetLife(1000);
	LoadSE(shot);
	SetScore(50000*2);
	LoadGraphic(futo);
	LoadSE(shot2);
	LoadSE(laser);
	SetTimer(53);
	SetEnemyMarker(true);
	SetInvincibility(120);
	SetDamageRate(4, 1);
	SetMovePosition02(GetCenterX, GetClipMinY + 120, 1);
	SetShotAutoDeleteClip(10, 10, 10, 10);

LoadGraphic(SP);
LoadGraphic(MC);
LoadGraphic(CBG1);
LoadGraphic(CBG2);
LoadGraphic(Cut);
	cutin(" "\""Legend of the Great Gods"\", Cut, 0, 0, 430, 448);
	
	}
let f = true;
task Movement {
let angle = 0;
wait(240);
loop{
ExMove;
wait(110);

wait(80);
}
}

task ChangeThePhase {
PlaySE(charge);
wait(30);
PlaySE(laser);
CreateLaserA(1, GetClipMinX, GetClipMaxY, 1000, 10, 10, 120);
SetLaserDataA(1, 0, atan2(GetClipMaxY - GetClipMinY, GetClipMinX - GetClipMaxX)-180, 0, 0, 0, 0);
FireShot(1);

CreateLaserA(1, GetClipMaxX, GetClipMaxY, 1000, 10, 10, 120);
SetLaserDataA(1, 0, atan2(GetClipMaxY - GetClipMinY, GetClipMaxX - GetClipMinX)-180, 0, 0, 0, 0);
FireShot(1);

CreateLaserA(1, GetCenterX, GetClipMinY, 1000, 10, 10, 120);
SetLaserDataA(1, 0, 90, 0, 0, 0, 0);
FireShot(1);
transition = true;
wait(30);


}
	task attack1 {
wait(120);
let col = 217;
 loop {  
loop(6){
	let a = rand(-5, 5);
	col++;
	if(col == 222 || col == 224){
	col++;
	}
	if(col>224){col = 217; }
 ascent(i in 0..20) {
	PlaySE(shot);
 CreateShotA(0, GetClipMinX+a+i*30, GetClipMinY+b, 20);
 SetShotDataA(0, 0, 0, -90+i*17*sin(b)+i*83.5854854, 0, 0, 0, col);
 SetShotDataA(0, 90, 0, NULL, 0, 0.02, 0.9345+(i/20), NULL);
 FireShot(0);
	
 } a-=17+pi*19*sin(b); a%=250; b-=110/pi; b%=360;
 wait(10); 
  }
if(GetEnemyLife<500 && transition == false){
ChangeThePhase;
}
wait(120);
 }
}

	@MainLoop{
if(GetCommonDataDefault("lifebar", 2)==1){
 if(f){
		SetX(GetX); SetY(GetY); CreateLifebar(1);
	f=false;} }
if(GetTimer!=timer && (GetTimer < 10 && GetTimer>3)){
	PlaySE(time1);
	}

	if(GetTimer!=timer && GetTimer < 4){
	PlaySE(time2);
	

	
}
timer = GetTimer;
frame2++;
	
if(frame3==240){
SetDamageRate(20, 20);
}
frame3++;



	if(b<GetClipMinY+10){b=300;}

	SetCollisionA(GetX, GetY, 32);
	SetCollisionB(GetX, GetY, 32);
	frame++;
	yield;
	
	}

	let ZUN_f = 0;
let ZUN_f2 = 0;
let ZUN_f3=0;
task ZUNimate(sprite){
	SetTexture(sprite);
	SetRenderState(ALPHA);
	SetAlpha(255);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(0.9,0.9);
	DrawGraphic(GetX,GetY);
}


task ZUNimateMain{
	if(shooting==false){
	if(GetSpeedX>0){
		if(ZUN_f2<10){SetGraphicRect(0,90,64,180);}
		if(ZUN_f2>=10 && ZUN_f2<20){SetGraphicRect(64,90,128,180);}
		if(ZUN_f2>=20 && ZUN_f2<30){SetGraphicRect(128,90,192,180);}
		if(ZUN_f2>=30){SetGraphicRect(192,90,256,180);}
		ZUN_f2++;
	ZUN_f3=0;
	}

	if(GetSpeedX<0){
		if(ZUN_f2<10){SetGraphicRect(0,90,64,180);}
		if(ZUN_f2>=10 && ZUN_f2<20){SetGraphicRect(64,90,128,180);}
		if(ZUN_f2>=20 && ZUN_f2<30){SetGraphicRect(128,90,192,180);}
		if(ZUN_f2>=30){SetGraphicRect(192,90,256,180);}
		ZUN_f2++;
	ZUN_f3=0;
	}
	
	if(GetSpeedX==0){
		if(ZUN_f<10){SetGraphicRect(0,1,64,82);}
		if(ZUN_f>=10 && ZUN_f<20){SetGraphicRect(64,1,128,82);}
		if(ZUN_f>=20 && ZUN_f<30){SetGraphicRect(128,1,192,82);}
		if(ZUN_f>=30 && ZUN_f<40){SetGraphicRect(192,1,256,82);}
		ZUN_f2 = 0;
	ZUN_f3=0;
	}
	ZUN_f++;
	}

	if(shooting==true){
		if(ZUN_f3<10){SetGraphicRect(0,190,75,275);}
		if(ZUN_f3>=10 && ZUN_f3<20){SetGraphicRect(70,190,160,275);}
		if(ZUN_f3>=20){SetGraphicRect(155,190,255,275);}

		ZUN_f3+=2;
		
	}

	if(ZUN_f==40){ZUN_f=0;}


}

sub ExMove { ExMove_(GetCenterY-75, 30, rand(15, 30)); }
function ExMove_(height, ud, length) {
	
	if(GetPlayerX < GetX) {
		if(GetX - length <= GetClipMinX) {
			SetMovePosition03(GetX + length+20, rand(height+4 - ud, height + ud), 20, 2);
		} else {
			SetMovePosition03(GetX - length-20, rand(height - ud, height + ud), 20, 2);
		}
	} else {
		if(GetX + length >= GetClipMaxX) {
			SetMovePosition03(GetX - length-20, rand(height - ud, height + ud), 20, 2);
		} else {
			SetMovePosition03(GetX + length+20, rand(height - ud, height + ud), 20, 2);
		}
	}
}
	// Movement functions for the boss to move towards the player.

task drown{
	let w =  rand(-50, 50);
	loop(4){
	b+=7;
	wait(2);
	PlaySE(shot2);
	loop(num){
	CreateShotA(1, GetClipMinX+a,  GetClipMinY+20+b, 20);
	SetShotDataA(1, 0, 0, rand(0, 360), 0, 0, 0,rand_int(217, 223));
	SetShotDataA(1, 90, 0, rand(0, 361), 0, 0.02, 2, NULL);
	FireShot(1);
	a+=25;
	
	}
	}
	}


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

    @DrawLoop {


SetTexture(CBG1);
SetRenderState(ALPHA);
SetGraphicAngle(0,0,0);
SetGraphicScale(1,1);
SetGraphicRect(0, anim2, 416, 1024+anim2);
SetAlpha(255);
DrawGraphic(GetCenterX, GetCenterY);

SetTexture(CBG2);
SetRenderState(SUBTRACT);
SetGraphicAngle(0,0,0);
SetGraphicScale(1,1);
SetGraphicRect(0-anim2, 0-anim2, 512-anim2, 512-anim2);
SetAlpha(255);
SetColor(150+acc7,230,120+acc4);
DrawGraphic(GetCenterX, GetCenterY);

SetTexture(MC);
SetRenderState(ADD);
SetGraphicAngle(0,0,-acc1);
SetGraphicScale(2+acc3,2+acc3);
SetGraphicRect(0, 0, 128, 128);
SetAlpha(255);
SetColor(255,255,255);
DrawGraphic(GetX, GetY);

if (GetSpeedX == 0){
SetTexture(SP);
SetRenderState(ALPHA);
SetGraphicAngle(0,0,0);
SetGraphicScale(1,1);
SetGraphicRect(0, 0, 67, 95);
SetAlpha(255);
DrawGraphic(GetX+5, GetY+3*sin(anim1));
}
if (GetSpeedX > 0){
SetTexture(SP);
SetRenderState(ALPHA);
SetGraphicAngle(0,0,0);
SetGraphicScale(1,1);
SetGraphicRect(196, 99, 246, 184);
SetAlpha(255);
DrawGraphic(GetX, GetY+3*sin(anim1));
}
if (GetSpeedX < 0){
SetTexture(SP);
SetRenderState(ALPHA);
SetGraphicAngle(0,0,0);
SetGraphicScale(-1,1);
SetGraphicRect(196, 99, 246, 184);
SetAlpha(255);
DrawGraphic(GetX - 10, GetY+3*sin(anim1));
}
acc1 += 5;
acc2 += 2;
anim1 += 7;
anim2 += 0.6;
anim3++;
acc5  += 2;
acc6 += 0.5;
acc3 = 0.2*cos(acc2);
acc4 = 55*cos(acc6);
acc7 = 100*cos(acc2);
}

task CreateLifebar(type) {
	let initlife = GetLife; yield;
	let sides=80;
	let totalradius=64;
	let repeat=2;
	let Z=-90;
	let IN=0;
	let OUT=8;
	task lifeseparation {
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, imglifebar);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexUV(obj, 0, 16, 0);
		ObjEffect_SetVertexUV(obj, 1, 24, 0);
		ObjEffect_SetVertexUV(obj, 2, 24, 11);
		ObjEffect_SetVertexUV(obj, 3, 16, 11);
		ObjEffect_SetVertexXY(obj, 0, -4, -11/2);
		ObjEffect_SetVertexXY(obj, 1, 4, -11/2);
		ObjEffect_SetVertexXY(obj, 2, 4, 11/2);
		ObjEffect_SetVertexXY(obj, 3, -4, 11/2);
		let totalradius_ = totalradius+OUT*3/4;
		ObjEffect_SetScale(obj, 1, 5/4);
		loop { ObjEffect_SetAngle(obj, 0, 0, 90+360*0.15); Obj_SetPosition(obj, GetX+totalradius_*cos(-90-360*0.15), GetY+totalradius_*sin(-90-360*0.15)); yield; }
	}
	let objL = Obj_Create(OBJ_EFFECT);
	let objB = Obj_Create(OBJ_EFFECT);
	if(type == 0 || type == 1) { lifeseparation; }
	ascent(n in 0..2) {
		let d=0;
		let objid = [objB, objL][n];
		Obj_SetX(objid,GetX);
		Obj_SetY(objid,GetY);
		ObjEffect_SetScale(objid,1,1);
		ObjEffect_SetPrimitiveType(objid,PRIMITIVE_TRIANGLESTRIP);
		ObjEffect_SetRenderState(objid,ALPHA);
		ObjEffect_SetTexture(objid,imglifebar);
		ObjEffect_CreateVertex(objid,2+sides*2);
		ascent(i in 0..sides+1){
			ObjEffect_SetVertexUV(objid,i*2,8+8*n,d*repeat/sides);
			ObjEffect_SetVertexUV(objid,i*2+1,8*n,d*repeat/sides);
			d+=-256;
		}
	}
	if(type!=0 && type!=1 && type!=2){type=2}
	let expand = [0, 1, 0, 0][type];
	let this = [0.85, 0.15, 1][type];
	let left = [0.15, 0, 0][type];
	loop {
		Obj_SetPosition(objL, GetX, GetY);
		Obj_SetPosition(objB, GetX, GetY);
		let ratio = GetLife/initlife*this+left; if(expand<1){expand+=1/15;}else if(expand>1){expand=1}
		ascent(i in 0..sides+1){
			let radiusINX=(totalradius+IN)*cos(i*360*ratio*expand/sides+Z);
			let radiusINY=(totalradius+IN)*sin(i*360*ratio*expand/sides+Z);
			let radiusOUTX=(totalradius+OUT)*cos(i*360*ratio*expand/sides+Z);
			let radiusOUTY=(totalradius+OUT)*sin(i*360*ratio*expand/sides+Z);
			ObjEffect_SetVertexXY(objL,i*2,-radiusINX,radiusINY);
			ObjEffect_SetVertexXY(objL,i*2+1,-radiusOUTX,radiusOUTY);
			let radiusINX=(totalradius+IN)*cos(i*360*expand/sides+Z);
			let radiusINY=(totalradius+IN)*sin(i*360*expand/sides+Z);
			let radiusOUTX=(totalradius+OUT)*cos(i*360*expand/sides+Z);
			let radiusOUTY=(totalradius+OUT)*sin(i*360*expand/sides+Z);
			ObjEffect_SetVertexXY(objB,i*2,-radiusINX,radiusINY);
			ObjEffect_SetVertexXY(objB,i*2+1,-radiusOUTX,radiusOUTY);
		}
		yield;
	}
}

	@Finalize {
	PlaySE(GetCurrentScriptDirectory~"sfx\se_enep01.wav");
	DeleteGraphic(imglifebar);
	if(GotSpellCardBonus==true){ PlaySE(GetCurrentScriptDirectory~".\sfx\capture.wav"); }
	if(GotSpellCardBonus==false){ PlaySE(GetCurrentScriptDirectory~".\sfx\fail.wav"); }
	DeleteGraphic(futo);
	}
}