
#ScriptVersion[3]
#Title["YuyukoS3"]
#Text["YuyukoS3"]
#Player[DEFAULT]
#System["./system/Default_System.txt"]
#Background["./BG.txt"]

#include"./Functions/Index.txt"
#include"./SetBoss.txt"
#include"./Shot/AllStarShot_Const.dnh"
#include"./system/Default_Effect.Txt"


let objBoss;
let GCSD=GetCurrentScriptDirectory~"";
let enemy;
let bSpell=false;
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let idle = true;
let atk = false;
let fog = 0;
let action = false;
let objScene=GetEnemyBossSceneObjectID();
let color = [276,277,278];
let color2 = [51,53,55];
let dcolor = [308,309,310];
@Initialize
{
Sound(GCSD ~ "Little.mp3");
SetBoss("Yuyuko",true);
}

@MainLoop
{
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),25);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	yield;

}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(3000);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(75);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(35000000);
	}
}

//----------------------------------------------------
task mainTask{
	magiccircle(objBoss,1,-1);
	DamageRateAtFrame(55,25,180);
	ObjMove_SetDestAtWeight(objBoss,stgx,140,10,7);
	Cutin("\"Sphere Bloom\"",15,"SHINKI");
	ObjEnemyBossScene_StartSpell(objScene);
	Sound(GCSD ~ "Butterfly.mp3");
	wait(90);
	TShot1;
	wait(1200);
	DamageRateAtFrame(100,50,0);
	}
//----------------------------------------------------

task TShot1
{
	ArrowSet;
	PlaySound("Charge5",95,0);
	ShinkiCharge(45,"AQUA");
	wait(90);
	PlaySound("Boon1",100,0);
	Sphere(GetEnemyX-100,stgy,726,1);
	Sphere(GetEnemyX+100,stgy,727,-1);
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
	wait(90);
	LargeShot;
	TMovement(50,10,3);
	wait(90);
	LargeShot;
	}
}

task LargeShot
{
	PlaySound("Shot6",95,0);
	ascent(i in 0..12+Level*3)
	{
		let angle = angleToPlayer+i*360/(12+Level*3);
		TShotA2(GetEnemyX+30*cos(angle),GetEnemyY+30*sin(angle),0.5,angle,0.01,1.5,777,12);
	}
}

	task ArrowSet
	{
		ascent(i in 0..12+Level*3)
		{
			Arrow(i*360/(12+Level*3)+angleToPlayer+12);
		}
	}
	

task Arrow(angle)
{
	ascent(i in -3..4)
	{
		ascent(j in 0..4)
		{
		let v = 1.35-(|i|)/9+j*0.85;
		let obj = TShotA1(GetEnemyX,GetEnemyY,v/5,angle+i*1.5+j*12,53,8);
		ObjMove_AddPatternA2(obj,0,NO_CHANGE,NO_CHANGE,v/60,0,v);
		if(i==0)
		{
			obj = TShotA1(GetEnemyX,GetEnemyY,v/5,angle+i*1.5+j*12+12,745,8);
			ObjMove_AddPatternA2(obj,0,NO_CHANGE,NO_CHANGE,v/60,0,v);
		}
		}
	}
}

task Sphere(px,py,corecolor,way){
	let ang1=0;
	let ang2=0;
	let rad=0;
	let fcount = 0;
	let count = 0;
		ascent(i in 0..16+Level*4){
			ascent(j in 0..7+Level){
				let color = corecolor;
				Point(i*360/(16+Level*4),j*180/(7+Level),color);
			}
		}
		while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
		{
			if(fcount<180){rad=224*sin(fcount/2);}
			fcount+=0.5;
			ang1=fcount/2.5;
			ang2=fcount/2.75*way;
			yield;
		}
			task Point(a,b,co){
			let obj=TShotA1(GetEnemyX,GetEnemyY,0,0,co,0); //These will be real bullets on foreground of sphere
			let backobj=TShotA1(GetEnemyX,GetEnemyY,0,0,co,0); // These will be fake bullets on background
			ObjRender_SetAlpha(backobj,64);
			if(co==726)
			{
				ObjRender_SetColor(obj,255,192+32*sin(a+90),255);
				ObjRender_SetColor(backobj,255,192+32*sin(a+90),255);
			}
			else if(co==727)
			{
				ObjRender_SetColor(obj,192+32*sin(a+90),255,255);
				ObjRender_SetColor(backobj,192+32*sin(a+90),255,255);
			}
			ObjShot_SetSpellResist(obj,true);
			ObjShot_SetSpellResist(backobj,true);
			ObjShot_SetAutoDelete(obj,false);
			ObjShot_SetAutoDelete(backobj,false);
			let x0; let y0; let z0; let x; let y; let z; let xtmp;
			while(!Obj_IsDeleted(obj)){
				if(rad<192+60){
					//Definition of sphere points. See http://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates
					x0=rad*sin(a)*cos(b);
					y0=rad*sin(a)*sin(b);
					z0=rad*cos(a);
				}
				//Rotation around X and Y axis. See http://en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions
				x=x0;
				y=y0*cos(ang1)-z0*sin(ang1);
				z=y0*sin(ang1)+z0*cos(ang1);
				xtmp=x; // Needed to save x value for z calculation
				x=x*cos(ang2)+z*sin(ang2);
				z=-xtmp*sin(ang2)+z*cos(ang2);
				ObjRender_SetAngleXYZ(obj,0,0,atan(y/x)*2);
				if(z>=0){
					//Show bullet, hide effect
					ObjMove_SetPosition(obj,px+x,py+y);
					ObjMove_SetPosition(backobj,-100,-100);
					ObjShot_SetIntersectionEnable(backobj,true);
				}else{
					//Show effect, hide bullet
					ObjMove_SetPosition(backobj,px+x,py+y);
					ObjMove_SetPosition(obj,-100,-100);
					ObjShot_SetIntersectionEnable(backobj,false);
				}
				yield;
			}
		}
	}

//----------------------------------------------------

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		EndBoss(1);
		yield;
		CloseScript(GetOwnScriptID());
	}