#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 7 (Danua)"]
#Text["7th SpellCard"]
#include"./Index.txt"

let bossObj;
let hansel;
let gretel;
let bossX = 0;
let bossY = 0;
let AnimFrame = 0;
let AnimFrame2 = 0;

let imgBoss = CSD ~ "/Texture/Danua.png";
let AttackSFX = CSD ~ "/SFX/TWINKLE3.wav";
let Attack2SFX = CSD ~ "/SFX/ATTACK5.wav";

@Initialize {
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);

	ObjMove_SetDestAtWeight(bossObj,190,120,8,5);

	Cutin(bossObj,"Grand Guignol",7);
	LoadSound(AttackSFX);
	LoadSound(Attack2SFX);
	mainTask;
 }

@Event {
	
	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(8500);
	}		
	case(EV_REQUEST_TIMER) {
		SetScriptResult(120);
	}
	case(EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(700000);
	}
 }

@MainLoop {
	
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);
	
	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,15);
		
	yield;
 }

@Finalize {

 }

task mainTask{
	renderBoss;
	renderBG("DANUA");
	callbackboss;
	Fire;
	movement;
	TEnd;
	wait(60);
	Hansel;
	Gretel;
	wait(60);
	Fire2;
 }
 
task movement{
	wait(70);
	loop{
		ObjMove_SetDestAtFrame(bossObj,rand(270,111),rand(90,131),60);
		wait(180);
	}
}
 
task renderBoss{
	let Z=0;
	let X=0;
	let dir;
	
	ObjPrim_SetTexture(bossObj, imgBoss);
	ObjSprite2D_SetSourceRect(bossObj,0,0,250,250);
	ObjSprite2D_SetDestCenter(bossObj);
	ObjRender_SetScaleXYZ(bossObj,0.5,0.5,0);
	
	while(!Obj_IsDeleted(bossObj)) {
		dir = ObjMove_GetAngle(bossObj);
		
		if(cos(dir) < 0) {
			loop(60){
				ObjRender_SetScaleXYZ(bossObj,0.4+0.02*sin(Z),0.4-0.02*sin(Z),0);
				Z+=3;
				ObjRender_SetAngleXYZ(bossObj,0,X,0);
				if(X>0){X-=3;}
				wait(1);
			}
		}
		else if(cos(dir) > 0) {
			loop(60){
				ObjRender_SetScaleXYZ(bossObj,0.4+0.02*sin(Z),0.4-0.02*sin(Z),0);
				Z+=3;
				ObjRender_SetAngleXYZ(bossObj,0,X,0);
				if(X<180){X+=3;}
			wait(1);
			}
		}
		yield;
	}
 }

task Fire{
	wait(120);
	let B = rand(-180,181);
	let angle= 0;
	loop{
		PlaySound(AttackSFX);
		while (angle<90){
			let obj = CreateShotA2(bossX+30*sin(angle+B),bossY-30*cos(angle+B),0,angle+B-45,0.01,3,302,12);
			ObjMove_AddPatternA2(obj,0,0.1,NO_CHANGE,0.1,0.5,2);
			ObjMove_AddPatternA2(obj,90,NO_CHANGE,NO_CHANGE,-0.1,0,2);
			
			let obj2 = CreateShotA2(bossX+30*sin(angle-B),bossY-30*cos(angle-B),0,angle-B+45,0.01,3,302,12);
			ObjMove_AddPatternA2(obj2,0,0.1,NO_CHANGE,0.1,-0.5,2);
			ObjMove_AddPatternA2(obj2,90,NO_CHANGE,NO_CHANGE,-0.1,0,2);
			angle+=90/3;
		}
		angle=0;
		B+=11;
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		wait(6);
		yield;
	}	
}

task Hansel{
	let addtoT=0;
	let signal=0;

	hansel = DrawHG(bossX,bossY,"HANSEL",false,false);
	TPos(hansel,0);
	wait(20);
	TChargeEffect(bossX,bossY);
	bullet;

	task TPos(obj,a){
		wait(10);
		while(!Obj_IsDeleted(obj)&&ObjEnemy_GetInfo(bossObj,INFO_LIFE)>0){
			if(signal==1&&addtoT<=1){
				addtoT+=0.005;
			}
			PlaySound(CSD ~ "/SFX/ahaha.mp3");
			ObjMove_SetDestAtFrame(hansel,0,0,300);
			wait(300);
			PlaySound(CSD ~ "/SFX/ahaha.mp3");
			ObjMove_SetDestAtFrame(hansel,380,450,300);
			wait(300);
			PlaySound(CSD ~ "/SFX/ahaha.mp3");
			ObjMove_SetDestAtFrame(hansel,0,450,300);
			wait(300);
			PlaySound(CSD ~ "/SFX/ahaha.mp3");
			ObjMove_SetDestAtFrame(hansel,bossX,bossY,300);
			wait(300);
			yield;
		}
	}
	task bullet{
		while(!Obj_IsDeleted(hansel)&&ObjEnemy_GetInfo(bossObj,INFO_LIFE)>0){
		ObjEnemy_SetIntersectionCircleToShot(bossObj,ObjMove_GetX(hansel),ObjMove_GetY(hansel),25);
		ObjEnemy_SetIntersectionCircleToPlayer(bossObj,ObjMove_GetX(hansel),ObjMove_GetY(hansel),5);
		yield;
		}
	}
}

task Gretel{
	let addtoT=0;
	let signal=0;

	gretel = DrawHG(bossX,bossY,"GRETEL",false,false);
	TPos(gretel,0);
	wait(20);
	TChargeEffect(bossX,bossY);
	bullet;

	task TPos(obj,a){
		wait(10);
		while(!Obj_IsDeleted(obj)&&ObjEnemy_GetInfo(bossObj,INFO_LIFE)>0){
			if(signal==1&&addtoT<=1){
				addtoT+=0.005;
			}
			PlaySound(CSD ~ "/SFX/ahaha.mp3");
			ObjMove_SetDestAtFrame(gretel,380,0,300);
			wait(300);
			PlaySound(CSD ~ "/SFX/ahaha.mp3");
			ObjMove_SetDestAtFrame(gretel,0,450,300);
			wait(300);
			PlaySound(CSD ~ "/SFX/ahaha.mp3");
			ObjMove_SetDestAtFrame(gretel,380,450,300);
			wait(300);
			PlaySound(CSD ~ "/SFX/ahaha.mp3");
			ObjMove_SetDestAtFrame(gretel,bossX,bossY,300);
			wait(300);
			yield;
		}
	}
	task bullet{
		while(!Obj_IsDeleted(gretel)&&ObjEnemy_GetInfo(bossObj,INFO_LIFE)>0){
		ObjEnemy_SetIntersectionCircleToShot(bossObj,ObjMove_GetX(gretel),ObjMove_GetY(gretel),25);
		ObjEnemy_SetIntersectionCircleToPlayer(bossObj,ObjMove_GetX(gretel),ObjMove_GetY(gretel),5);
		yield;
		}
	}
}

task Fire2{
	wait(60);
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE)>0){
		let angle=0;
		while(angle<360){
			CreateShotA2(ObjMove_GetX(hansel)+20*sin(angle+ObjMove_GetAngle(hansel)),	 ObjMove_GetY(hansel)-20*cos(angle+ObjMove_GetAngle(hansel)),	 0,angle-90+ObjMove_GetAngle(hansel),0.01,2,46,30);
			CreateShotA2(ObjMove_GetX(hansel)+20*sin(angle+180+ObjMove_GetAngle(hansel)),ObjMove_GetY(hansel)-20*cos(angle+180+ObjMove_GetAngle(hansel)),0,angle+90+ObjMove_GetAngle(hansel),0.01,2,37,30);
			CreateShotA2(ObjMove_GetX(gretel)+20*sin(angle+ObjMove_GetAngle(gretel)),	 ObjMove_GetY(gretel)-20*cos(angle+ObjMove_GetAngle(gretel)),	 0,angle-90+ObjMove_GetAngle(gretel),0.01,2,46,30);
			CreateShotA2(ObjMove_GetX(gretel)+20*sin(angle+180+ObjMove_GetAngle(gretel)),ObjMove_GetY(gretel)-20*cos(angle+180+ObjMove_GetAngle(gretel)),0,angle+90+ObjMove_GetAngle(gretel),0.01,2,37,30);
			angle+=360/3;
		}
		PlaySound(AttackSFX);
		wait(40);
	}
}

 task TEnd {
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 0) {
		yield;
	}
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	loop(1){
		TExplosionB(ObjMove_GetX(bossObj),ObjMove_GetY(bossObj),5,0.3,255,255,10);
		TExplosionB(ObjMove_GetX(bossObj),ObjMove_GetY(bossObj),5,0.2,255,10,255);
		TExplosionB(ObjMove_GetX(bossObj),ObjMove_GetY(bossObj),5,0.1,255,100,0);
		wait(1);
	}
	Obj_Delete(bossObj);
	ResetShaderI(0, 100);
	wait(60);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	CloseScript(GetOwnScriptID);
}