//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Butterfly spiral"]
#include "./../../ZUNShot/ZUNShot_Const.txt"
#include "./../sharedFuncDave.dnh"



let objBoss;
let width = 384;
let height = 448;
let life = 4800;
let scene = GetEnemyBossSceneObjectID();

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(life);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(45);
    }
}


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	SetCommonData("non1", "yes");
	
	animNon(objBoss);
	TFinalizeNon(scene, objBoss);
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, 2*height/5, 60);
	Main;
}


@MainLoop{
    ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); 
    ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 28);
    yield;
}



task Main{
    Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 20, 0);
	wait(120);
	let shot2 = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK4.WAV", 50);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/Charge.WAV", 80));
    wait(60); 
	ObjEnemy_SetDamageRate(objBoss, 100, 50);
    let ang; let dir=1; if(rand(0,1)>=1/2){dir=-1;}
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    moveButterfly(dir);
	    shotSound(shot2, 480, 50, 15, objBoss, false, 0);
	    ang= rand(0,360);
	    simpleSpir(ang, 300, dir);
		wait(180);
		simpleSpir(ang, 300, -dir);
		wait(450);
	}
}


task moveButterfly(dir){
    ObjMove_SetDestAtFrame(objBoss, width/2 - dir*rand(0.1*width, width/4), rand(height/5, 2*height/5), 120);
	loop(240){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;} else{yield;}}
	ObjMove_SetDestAtFrame(objBoss, width/2 + dir* rand(width*0.1, width/4), rand(height/5, 2*height/5), 180);
	loop(300){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;} else{yield;}}
	ObjMove_SetDestAtFrame(objBoss, width/2, 2*height/5, 10);
	loop(60){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;} else{yield;}}
}
    


task simpleSpir(ang, time, dir){
    let t=0;
    while(t<time){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ascent(i in 0..3){let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3.5, ang +120*i, 319, 0); transform(obj);}
		ang+= dir*rand(8, 12);
		wait(15);
		t+=15;
	}
}


task transform(obj){
    let shot1 = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/TWINKLE.WAV", 70);
    let shot2 = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK5.WAV", 60);
    while(ObjMove_GetY(obj)>0 && ObjMove_GetY(obj) < height){yield;}
	let x = ObjMove_GetX(obj);
	let y = ObjMove_GetY(obj);
	if(y>=height){
	    ObjSound_Play(shot1);let ang = rand(260, 280); let col = rand(218,232); 
		ascent(i in 0..3){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		CreateShotA2(x,y, 0, rand(260, 270), rand(0.005, 0.02*i), 4+i/2, col, 20); Obj_Delete(obj);}
	}
	else{if(x>=10 && x<=width-10){ObjSound_Play(shot2);launch(obj,3, 90, 10, 0, 0.005, 0.01, 4, rand(138, 154), false, 0,0, 0,0,0, false, objBoss);}}
}
	
	
	



