//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Last spell (retrac)"]
#System["./../../system/NSystem.dnh"]
#Player["./../../player/Reimu by plasmaflame22/Reimu_A.txt", "./../../player/Reimu by plasmaflame22/Reimu_C.txt"]
#include "./../../ZUNShot/ZUNShot_Const.txt"
#include "./../sharedFuncSaul.dnh"


let objBoss;
let width = 444;
let height = 448;
let life = 7800;
let time= 180;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";
let twinkle = LoadEffect(path~"TWINKLE.WAV", 80);
let shot4 = LoadEffect(path~"ATTACK4.WAV", 80);
let shot5 = LoadEffect(path~"ATTACK5.WAV", 70);

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


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
	SetCommonData("non1", "no");
	SetCommonData("spell", "Retraction");
	spell;
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, true);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, height/3, 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 spell{
    let num = GetPlayerSpell;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    while(GetPlayerSpell >= num){yield;}
		ObjEnemy_SetDamageRate(objBoss, 0,0);
		wait(400);
		num = GetPlayerSpell;
		ObjEnemy_SetDamageRate(objBoss, 60, 0);
	}
}
		


task Main{
    SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	let shot3= LoadEffect(path~"ATTACK3.WAV", 100);
	Spell("Mind twist: Deadly retract", objBoss, 1);
    wait(240);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
    warpCircles(22, 0.5, 10, 1, 219, 60);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 60,0);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>3*life/4 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 3*time/4){yield;}
	
	ObjSound_Play(shot3);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	loop(5){DeleteShotAll(TYPE_SHOT, TYPE_FADE); yield;}
	warpCircles(20, 0.8, 10, -1, 224, 90); 
	wait(55);
	ObjEnemy_SetDamageRate(objBoss, 60, 0);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/2 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> time/2){yield;}
	
	ObjSound_Play(shot3);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	loop(5){DeleteShotAll(TYPE_SHOT, TYPE_FADE); yield;}
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 60, 0);
	wait(30);
	let ang = findAngRad(0, width, 0, height)[0];
	shotSound(shot4, -1, 50, 5, objBoss, false, life/4, scene, time/4);
	helix(width, height, 9, ang, 20, 105, -1, 5, 188, -1, objBoss, 0);
	helix(0, 0, 9, ang+180, 20, 105, -1, 5, -1, 188, objBoss, 0);
	ang = findAngRad(width, 0, 0, height)[0];
	helix(0, height, 9, ang, 20, 105, -1, 5, 188, -1, objBoss, 0);
	helix(width, 0, 9, ang+180, 20, 105, -1, 5, -1, 188, objBoss, 0);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> time/3){yield;}
	
	ObjSound_Play(shot3);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	loop(5){DeleteShotAll(TYPE_SHOT, TYPE_FADE); yield;}
	shotSound(shot4, -1, 55, 5, objBoss, false, 0, scene, 0);
	ang = rand(0,360); let ang2= ang;
	wait(55);
	ObjEnemy_SetDamageRate(objBoss, 60, 0);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ObjSound_Play(shot5);
	    ascent(i in 0..3){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 1, ang+120 *i +rand(-5,5), 55,0);
		}
		ascent(i in 0..3){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 1, ang2+120 *i +rand(-5,5), 55,0);
		}
		let r = rand(10,15);
		ang+=r; ang2-=r; wait(12);
	}  

}


task del(obj){
    wait(120);
	ObjShot_SetAutoDelete(obj, true);
}

	
task warpCircles(numBul, speed, maxdiffAng, dir, graphic, pause){
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ObjSound_Play(twinkle);
	    let r = rand(0,maxdiffAng)*dir;
	    ascent(i in 0..numBul){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    let obj = CreateShotA1(width/2 + 330*cos(360/numBul *i +r), height/2 + 330*sin(360/numBul *i +r), 1.2, 360/numBul *i +180, graphic,0);
			ObjShot_SetAutoDelete(obj, false); del(obj);
		}
		wait(pause);
	}
}
	
	
	
	
	
	
	