#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell2"]
#Text["Woop"]


let objBoss;

let BossX;
let BossY;

let AttackX;
let AttackY;

#include "./Functions/Index.txt"
#include "./Functions/Dialogue.txt"


@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	RenderClause(objBoss);
	BossShotSound(objBoss, 50, 600);
	DrawName("Wilhelm von Clausewitz Halcyon HISUIMARUUUU", [255, 0, 255], 12.7);
	BottomEnemyMarker(objBoss, objBoss);
	MagicRing(objBoss, false);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/4, 5, 4);
	mainTask;
	end;

} 

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(4500);
     	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(80000000);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
} 

@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	
	ObjEnemy_SetIntersectionCircleToShot(objBoss,BossX,BossY,21);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,BossX,BossY,16);
	yield;
} 

@Finalize {

} 

task mainTask { 
SpellCard("&quot;Planetarial &quot;Rings&quot; that &quot;Revolve&quot; around Giants&quot;");
let angle = 0;
GravScale;

	loop(60){
		ascent(i in 0..6){
		CreateShotA1(BossX, BossY, 2, angle+(i*360/6), PELLET_PURPLE, 0);
		//CreateShotA1(BossX, BossY, 2, -angle+(i*360/6), PELLET_PURPLE, 0);
		}
		angle+=4;
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/e_shot_00.wav", 60);
		wait(6);
	}
	loop{
		ascent(i in 0..6){
		CreateShotA1(BossX, BossY, 2, angle+(i*360/6), PELLET_PURPLE, 0);
		CreateShotA1(BossX, BossY, 2, -angle*3+(i*360/6), PELLET_PURPLE, 0);
		}
		angle+=4;
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/e_shot_00.wav", 60);
		wait(6);
	}
}

task GravScale{
wait(120);
Grav(50, 2, 5, 6);
Rings;
}

task Rings{
	loop{
		ascent(i in 0..42){
		let obj = CreateShotA2(BossX, BossY, 1, 0+(i*360/42), 0.005, 5, C_BULLET_PURPLE, 0);
		ObjMove_SetAngularVelocity(obj, 2);
		Unangulate(obj);
		}
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/e_shot_00.wav", 60);
		wait(60);
	}
	task Unangulate(obj){
	wait(60);
	ObjMove_SetAngularVelocity(obj, 0);
	}
}

task Grav(rad, mult, off, amm){
SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/laser_2.wav", 60);
		ascent(i in 0..amm){
		let obj = GravityWell;
		ObjRender_SetPosition(obj, BossX+rad*cos(i+360/amm), BossY+rad*sin(i+360/amm), 0);
		move(obj,i);
		}
	task move(obj,i){
	let angle = 0;
		loop{
		ObjRender_SetPosition(obj, BossX+rad*cos((i*360/amm)+angle), BossY+rad*sin((i*360/amm)+angle), 0);
		ObjRender_SetAngleZ(obj, ObjRender_GetAngleZ(obj)+2);
		Well(obj, i, angle,rad, amm);
		angle+=0.5*mult;
		rad+=0.1*sin(angle/3);
		yield;
		}
	}
	task Well(obj,i, ang, rad, amm){
		let shot =  GetShotIdInCircleA2(BossX+rad*cos((i*360/amm)+ang), BossY+rad*sin((i*360/amm)+ang),20,TARGET_ALL);
		if(length(shot)>0){
			let i = 0;
			loop(length(shot)){
				let pos	= [ObjMove_GetX(shot[i]),ObjMove_GetY(shot[i])];
				ObjMove_SetAngle(shot[i], ObjMove_GetAngle(shot[i])+off);
				i++;
			}
		}
	yield;
	}
}

function SpellCard(name){
	let Cutin = GetCurrentScriptDirectory ~ "Functions/img/Emotions/Cla1.png";
	ObjCutin_SetSpellcardS3(name,255, 0, 255);
	ObjCutin_LaunchS3("YABUSAME", Cutin, "Normal");
	TCallBg;
	bspell = true;
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	wait(210);
}


task end{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
    Obj_Delete(objBoss);
	wait(1);
	SetCommonData("deathsound",1);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
} 
