//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Homotopy of a wave"]
#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 "./../sharedFuncSamir.dnh"


let objBoss;
let width = 420;
let height = 440;
let life = 3300;
let limit = life*0.4;
let time= 60;
let scene = GetEnemyBossSceneObjectID();

let path = GetCurrentScriptDirectory~"./../../SFX/";
let charge = LoadEffect(path~ "Charge.WAV", 80);
let shot4 = LoadEffect(path~"ATTACK4.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);
	SetPlayerClip(5,5, 415, 435);
    ObjEnemyBossScene_StartSpell(scene);
	
	SetCommonData("non1", "no");
	SetCommonData("spell", "saul");
	
	animSpell(objBoss, 2);
	TFinalizeSpell(scene, objBoss, false, false);
	
	Name(2, objBoss);
    Indicator(objBoss, 2);
    takeDamage(objBoss, scene, life);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, height/4, 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{
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	Spell("Mind twist:[r]Homotopy of a wave", objBoss, 2);
    wait(200); 
	ObjSound_Play(charge);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	move(objBoss, width/2-30, width/2+30, height*0.28 ,height/3, 0, 180, 100, objBoss, 0, scene, 0);
	

	BoWarp(8, 2.1, 9, 2, limit);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>25){yield;}
	ObjSound_Play(LoadEffect(path~ "ATTACK3.WAV", 90));
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	ObjSound_Play(LoadEffect(path~"CAUTIONSHORT.WAV", 100));
		
	wait(120);
	ObjSound_Play(LoadEffect(path~ "ATTACK3.WAV", 90));
	ObjSound_Play(LoadEffect(path~"LenenBomb.WAV", 100));
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	
	BoWarp(5, 5.6, 14, 2, 0);
}
	
	
	
	
	

function f1(x){return sin(x) +3*sin(2*x) - 2*sin(3.2*x)+2*sin(1.8*x);}	


task BoWarp(numBranch, speed, gap, delay, lim){	
	let col = [187,188,190]; let timeLim = 25; if(lim==0){timeLim=0;}
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>lim && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>timeLim){
		let ang = rand(90,100);
		let inc = 0;
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=lim || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=timeLim){return;}
		shotSound(shot4, 1000, 40, 2, objBoss, false, lim, scene, timeLim);
		while(absolute(inc)<500){	
			ascent(i in 0..numBranch){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=lim || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=timeLim){return;}
				CreateShotA1(X(objBoss), Y(objBoss), speed, ang+360/numBranch *i, col[i%3], 5);
			}
			ang += f1(inc) * gap;
			inc+=rand(0.5,1.5); wait(delay);
		}
		wait(60);
	}
}