//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Transmathematic: All of the above"]
#include "./../ZUNShot/ZUNShot_Const.txt"
#include "./sharedfuncMario.dnh"


let objBoss;
let width = 384;
let height = 448;
let life = 4000;
let arrow = false;
let time= 80;
let scene= GetEnemyBossSceneObjectID();


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


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	takeDamage(objBoss, scene, life);
	Indicator(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
	SetForbidPlayerShot(false);

	animSpell(objBoss);
	
	TFinalizeSpellA;
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, height/2, 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 TFinalizeSpellA{
    SetForbidPlayerShot(false);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	StartSlow(TARGET_ALL, 30);
	let a = LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK3.WAV", 70);
	loop(4){ObjSound_Play(a); wait(15);}
	StopSlow(TARGET_ALL);
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
	if(ObjEnemyBossScene_GetInfo(scene, INFO_PLAYER_SHOOTDOWN_COUNT)      
      +ObjEnemyBossScene_GetInfo(scene, INFO_PLAYER_SPELL_COUNT) == 0){
	    ObjSound_Play(LoadEffect("script/Test/SFX/BONUS3.WAV", 100));
        AddScore(ObjEnemyBossScene_GetInfo(scene, INFO_SPELL_SCORE));
		SetPlayerLife(GetPlayerLife()+1);
    }   
	else if(ObjEnemyBossScene_GetInfo(scene, INFO_PLAYER_SHOOTDOWN_COUNT)                 
      +ObjEnemyBossScene_GetInfo(scene, INFO_PLAYER_SPELL_COUNT) > 0){
	      ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK3.WAV", 80));
	      failBonus;
    }
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "../SFX/DEFEATED.WAV", 100));
	wait(60);
	NotifyEventAll(EV_USER_STAGE+5, 0);
	wait(240);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}

task Main{
    ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "../SFX/SPELLCARD.WAV", 100));
	Spell("Transmathmatic: [r] All of the above", scene, objBoss);
	wait(240);
	let obja = ObjSound_Create;
	ObjSound_Load(obja, GetCurrentScriptDirectory~ "../SFX/ATTACK2.WAV");
	let horn = LoadEffect(GetCurrentScriptDirectory~ "../SFX/Horn.WAV", 100);
	ObjSound_SetRestartEnable(horn, true);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	DelayLaser(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 45, 300, 100, 13, 60);
    DelayLaser(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 135, 300, 100, 13, 60);
	ascent(i in 0..3){DelayLaser(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 225+i*45, 300, 100, 13, 60);}
	wait(60);
	shotSound(obja, -1, 60, 10, objBoss);
	let c=1;
	if(rand(-1, 1)<0){c=-1;}
	let k=1;
	if(rand(-1, 1)<0){k=-1;}
	let del2 = rand(400, 600);
    tentacles(45, 180, del2, 100, 20, c, k);
	tentacles(135, 180, del2, 100, 20, c, k);
	ascent(i in 0..3){tentacles(225+i*45, 180, del2, 100, 10, c, k);}
	wait(180);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	wait(60);
	balls;
	laser;
	
	
}

task balls{
    let shot = LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK3.WAV", 50);
	ObjSound_SetRestartEnable(shot, true);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ObjSound_Play(shot);
	    loop(4){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    let obj=CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 8, rand(GetAngleToPlayer(objBoss)+90, GetAngleToPlayer(objBoss)+260), rand(-0.3, -0.15), 0, 405,0);
            ObjRender_SetAlpha(obj, 240);
			Commanda(obj);
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>= 2*life/3){wait(200);}
		else if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>= life/3){wait(120);}
		else{wait(80);}
	}
}

task Commanda(obj){
    let shot = LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK3.WAV", 50);
	ObjSound_SetRestartEnable(shot, true);
    while(ObjMove_GetSpeed(obj)>0){yield;}
	ObjSound_Play(shot);
	let ang= GetAngleToPlayer(obj);
	ascent(i in 0..8){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;} let obja = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 4, ang +25+45*i, 374, 0); ObjRender_SetAlpha(obja, 250);}
	Obj_Delete(obj);
}

task laser{
    let las = LoadEffect( GetCurrentScriptDirectory~ "../SFX/LASER2.WAV", 90);
	ObjSound_SetRestartEnable(las, true);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)> 3*life/4){yield;}
	let wvel=1;
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ascent(i in 0..4){
		    if(wvel == 1){DelayLaser(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 30+90*i, 300, 50, 13, 60);}
			else{DelayLaser(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), -30+90*i, 300, 50, 13, 60);}
		}
		wait(60);
	    let col= truncate(rand(26, 34));
		ObjSound_Play(las);
	    ascent(i in 0..4){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
	        let obj=CreateCurveLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 45, 90*i, 40, 20, col, 0);
			ObjLaser_SetInvalidLength(obj, 15, 15);
			ObjCrLaser_SetTipDecrement(obj, 0.25);
			ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
			ObjRender_SetAlpha(obj, 255);
			control(obj, wvel, 60, 300);
		}
		wvel = -wvel;
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>= life/2){wait(120);}
		else{wait(80);}
	}
}


task tentacles(ang, delay1, delay2, del, maxdiff, c, k){
    let horn = LoadEffect(GetCurrentScriptDirectory~ "../SFX/Horn.WAV", 100);
	ObjSound_SetRestartEnable(horn, true);
    let angle=ang;
	let newAng = angle;
	armMove(angle, newAng, delay1, 30, false, c, k);
	wait(delay1 - 70);
	ObjSound_Play(horn);
	wait(80);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    armMove(angle, newAng, delay2, 30, true, c, k);
		wait(delay2-60);
		ObjSound_Play(horn);
		wait(80);
		angle+=delay2 * c/4;
		c=-c;
		delay2+=del;
		if(delay2>700){delay2= 500;}
    }
}


task armMove(ang, newAng, delay, maxdiff, move, c, k){
    let time=0;
    while(time<delay){
		while(max(newAng-ang, ang-newAng)<maxdiff){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj1= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(3,4), rand(newAng-2, newAng+2), 51, 0);
			ObjRender_SetAlpha(obj1, 170);
			ObjShot_SetSpellResist(obj1, true);
			let obj2=CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(5,7), rand(newAng-1, newAng+1), 410, 0);
			ObjRender_SetAlpha(obj2, 170);
			ObjShot_SetSpellResist(obj2, true);
			newAng+=1*k;
			if(move){ang+=c/4;}
			time++;
			yield;
		}
		k=-k;
		if(newAng>ang){newAng= ang+maxdiff-1;}
		else{newAng= ang-maxdiff+1;}
		time++;
		yield;
	}
}	