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



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

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


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
	SetCommonData("non1", "yes");
	SetCommonData("spell", "Chaos rising");
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, false, false);
	
	
    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, 0, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/SPELLCARD.WAV", 100));
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/CAUTIONSHORT.WAV", 100));
	Spell("Chaos sign:[r]Chaos rising", objBoss, 1);
    wait(120);
	ObjEnemy_SetDamageRate(objBoss, 20, 10);
	wait(120);
	damage;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		butterfly(width/2, height/2, 0.001, 0.02, 3, -90, 600, 3, 60, 7, 93, 200);
		wait(240);
		loop(3){
			ObjMove_SetDestAtFrame(objBoss, rand(width/5, 4*width/5), rand(height/5, 3*height/10), 60);
			wait(60);
			butterfly(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0.001,0.01, 2.5, GetAngleToPlayer(objBoss), 450, 2, 30, 20, 95, 60);
			wait(80);
		}
		wait(50);
		ascent(i in 1..4){
			ObjMove_SetDestAtFrame(objBoss, i * 3*width/10 +rand(-20, 20), rand(60, height/5), 60);
			wait(60);
			butterfly(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0.006,0.02, 3, 45*i, 260, 1, 20, 20, 99, 30);
			wait(40);
		}
		wait(60);
	}	
}

task damage{wait(120); ObjEnemy_SetDamageRate(objBoss, 60, 20);}

task play(delay){
    loop(delay){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;} yield;}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Wave.WAV", 90));
}


task butterfly(xcentre, ycentre, accelmin, accelmax, maxspeed, ang, numBul, numCycles, radius, bulPerFrame, graphic, delay){
    play(delay);
	let shot = LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK4.WAV", 70);
    let theta=0; let r; let x; let y; let i=0;
	shotSound(shot, numBul/bulPerFrame, 60, 1, objBoss, false, 0);
	loop(numBul/bulPerFrame){
	    loop(bulPerFrame){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			r = radius*(power(2.7, sin(theta)) - 2*cos(4*theta) + (sin((2*theta -180)/24))^2);
			x = cos(ang-90) * r*cos(theta) -sin(ang-90)* r*sin(theta);
			y = sin(ang-90) * r*cos(theta) +cos(ang-90)* r*sin(theta);
			let obj = CreateShotA1(xcentre + x, ycentre + y, 0, ang+theta+90, graphic, 0);
			ObjShot_SetSpellResist(obj, true);
			ObjMove_AddPatternA2(obj, delay-i, NO_CHANGE, NO_CHANGE, rand(accelmin, accelmax), 0, maxspeed);
			theta+= (360*numCycles)/numBul;
		}
		i++;
		yield;
	}
}