//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Fury of Archimedes"]
#include "./../ZUNShot/ZUNShot_Const.txt"
#include "./sharedfuncMario.dnh"


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


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


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	Indicator(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
    
	animSpell(objBoss);
	
	
	TFinalizeSpell(scene, objBoss);	
    ObjMove_SetDestAtFrame(objBoss, width/2, 90, 60);
	mainT;
	
}


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


task mainT{
    takeDamage(objBoss, scene, life);
    SetForbidPlayerSpell(true);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "../SFX/SPELLCARD.WAV", 100));
	Spell("Fury of Archimedes", scene, objBoss);
	wait(240);
	let s = LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK5.WAV", 80);
	ObjSound_Play(s);
	ascent(i in 1..5){
	    let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
		corner(obj, i);
	}
	wait(60);
	loop(3){
	    ascent(i in 0..300){
		    ObjSound_Play(s);
		    let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 4.5, 360*i/300, 90, 0);
			ObjRender_SetAlpha(obj, 140);
			ObjShot_SetIntersectionEnable(obj, false);
		}
		wait(20);
	}
	wait(60);
	
	SetForbidPlayerSpell(false);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	other;
	warn;
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	archim1;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > life/2){yield;}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK3.WAV", 80));
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	archim2;
}
    

task corner(obj, i){
    alternative(i)
	case(1){ObjMove_SetDestAtFrame(obj, 5,5, 60);}
	case(2){ObjMove_SetDestAtFrame(obj, width-5,5, 60);}
	case(3){ObjMove_SetDestAtFrame(obj, 5,height-5, 60);}
	case(4){ObjMove_SetDestAtFrame(obj, width-5, height-5, 60);}
}


task other{
    let obja = LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK5.WAV", 80);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > life/2){
	    ObjSound_Play(obja);
	    ascent(i in 0..4){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0) {return;} CreateShotA1(cos(90*i/3), sin(90*i/3), 4, 90*i/3, 409, 0);}
		ascent(i in 0..4){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0) {return;} CreateShotA1(width + cos(90 + 90*i/3), sin(90 + 90*i/3), 4, 90 + 90*i/3, 409, 0);}
		ascent(i in 0..4){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0) {return;} CreateShotA1(width + cos(180 +90*i/3), height+ sin(180 + 90*i/3), 3, 180 + 90*i/3, 409, 0);}
		ascent(i in 0..4){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0) {return;} CreateShotA1(cos(260 + 90*i/3), height + sin(260 + 90*i/3), 3, 260 + 90*i/3, 409, 0);}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 3*life/4){wait(60);}
		else{wait(30);}
	}
}
	    
task otherb{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > life/2){
	    ascent(i in 0..3){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0) {return;}
	        CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2, GetAngleToPlayer(objBoss), 336, 3*i);
		}
		wait(240);
	}
}

task archim1{
	let obja = LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK2.WAV", 80);
    let j=1;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	    shotSound(obja, 65, 60, 10, objBoss);
		if(j%2==1){
			spiral(1,130, 274, 2, life/2, true);
			spiral(2,80, 84, 2, life/2, true);
		}
		else if(j%2 ==0){
			spiral(-1,130, 274, 2, life/2,true);
			spiral(-2,80, 84, 2, life/2, true);
		}
		wait(90);
		j++;
	}
}


task archim2{
    let obja = LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK2.WAV", 80);
	let j=1;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	    shotSound(obja, 40, 60, 10, objBoss);
		let col1= truncate(rand(43, 57));
		let col2= truncate(rand(218, 231));
		if(j%2==1){
			spiral(1,140, col1, 3, 0, false);
			spiral(2,40, col2, 3, 0, false);
		}
		else if(j%2 ==0){
			spiral(-1,140, col1, 3, 0, false);
			spiral(-2,40, col2, 3, 0, false);
		}
		wait(70);
		j++;
	}
}

task warn{
    let i=1;
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)> life/2){
	    let theta=15;
	    let r =0;
		let ang;
		loop(140){
		    if(i%2==1){
				let obj1 = CreateShotA1(width/2 + r*cos(theta), height/2 + r*sin(theta), 0, theta, 90, 0);
				let obj2 =CreateShotA1(width/2 - r*cos(theta), height/2 - r*sin(theta), 0, theta+180,90, 0);
				del(obj1,i);
				del(obj2, i);
			}
			else{
			    let obj1 = CreateShotA1(width/2 - r*cos(theta), height/2 + r*sin(theta), 0, 180-theta,90, 0);
				let obj2 =CreateShotA1(width/2 + r*cos(theta), height/2 - r*sin(theta), 0, -theta, 90, 0);
				del(obj1,i);
				del(obj2, i);
			}
			theta+= (360-15)/140;
		    r= theta* width/(2*360);
		}
		if(i==1){wait(160);}
		else{wait(90);}
		i++;
	}
}
	
		
task del(obj, i){
    ObjRender_SetAlpha(obj, 100);
	ObjShot_SetIntersectionEnable(obj, false);
    if(i==1){wait(120);}
	else{wait(50);}
	Obj_Delete(obj);
}
		
		
task spiral(n,num, graphic, bulperframe, limit, phase1){
	let theta=15;
	let r =0;
	let col= truncate(rand(75, 88));
	let ang;
    alternative(n)
	case(1){
	    loop(num){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=limit){return;}
		    loop(bulperframe){
				let obj = CreateShotA1(width/2 + r*cos(theta), height/2 + r*sin(theta), 1.5, theta, graphic, 30);
				ang = rand(70,110);
				if(phase1){
					if(ObjMove_GetX(obj)< width/2 && ObjMove_GetX(obj)>= width/2 -width/16){ang= rand(90, 110);}
					else if(ObjMove_GetX(obj)> width/2 && ObjMove_GetX(obj)<= width/2 +width/8){ang= rand(70, 90);}
					ObjMove_AddPatternA3(obj, 60, rand(1, 2), ang, 0.02, 0, 4, 271);
				}
				if(!phase1){
				    if(ObjMove_GetX(obj)< width/2 && ObjMove_GetX(obj)>= width/2 -width/16){ang= rand(90, 110);}
					else if(ObjMove_GetX(obj)> width/2 && ObjMove_GetX(obj)<= width/2 +width/16){ang= rand(70, 90);}
					ObjMove_AddPatternA2(obj, 60, rand(1, 2), ang, 0.02, 0, 4);
				}
				theta+= (360-15)/num;
				r= theta* width/(2*360);
			}
			yield;
		}
	}
	case(-1){
	    loop(num){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=limit){return;}
		    loop(bulperframe){
				let obj =CreateShotA1(width/2 - r*cos(theta), height/2 + r*sin(theta), 1.5, 180-theta,graphic, 30);
				ang= rand(70,110);
				if(ObjMove_GetX(obj)< width/2 && ObjMove_GetX(obj)>= width/2 -width/16){ang= rand(90, 110);}
				else if(ObjMove_GetX(obj)> width/2 && ObjMove_GetX(obj)<= width/2 +width/8){ang= rand(70, 90);}
				
				if(phase1){ObjMove_AddPatternA3(obj, 60, rand(1, 2), ang, 0.02, 0, 4, 271);}
				else{ObjMove_AddPatternA2(obj, 60, rand(1, 2), ang, 0.02, 0, 4);}
				theta+= (360-15)/num;
				r= theta* width/(2*360);
			}
			yield;
		}
	}
	case(2){
	    loop(num){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=limit){return;}
		    loop(bulperframe){
				let obj=CreateShotA1(width/2 - r*cos(theta), height/2 - r*sin(theta), 2.2, theta+180, graphic, 30);
				theta+= (360-15)/num;
				r= theta* width/(3*360);
			}
			yield;
		}
	}
	case(-2){
	    loop(num){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=limit){return;}
		    loop(bulperframe){
				let obj=CreateShotA1(width/2 + r*cos(theta), height/2 - r*sin(theta), 2.2, -theta, graphic, 30);
				theta+= (360-15)/num;
				r= theta* width/(3*360);
			}
			yield;
		}
	}
}
	
	
	
	
	
	