/*#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Toyasatomimi no Miko (Spell 3)"]
#Text["knock knock [r] get the door [r] it's religion"]
#System["./system/system/Awalys_System.txt"]
#Background["./system/background/back_miko.dnh"]
#Player["./system/player/Byakuren/Byakuren_A.txt"]*/

#include"./system/shot/CrispBright4/Shotconstants.dnh"
#include"./system/lib/lib_main.dnh"
#include"./system/lib/miko_aura.dnh"

let frame = 0;
let diff = 3;
let time = 0;
let timer = 0;
let move = 0;
let angleInc = 0;
let inc = 0;
let count = 0;
let count2 = 0;
let count3 = 0;

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(7500/4);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(999000000/2);
	}
}

@Initialize{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
    ObjMove_SetDestAtWeight(objBoss, cx, cy, 15,6);


	TFinalizeSpell;
	StartSlow(TARGET_ALL, 60);
	BossRender;
	BossMarker;
	MagicCircle(objBoss);
	Name("Toyasatomimi no Miko");
	//Movement;
	Pattern;
}


@MainLoop{
	let ex = ObjMove_GetX(objBoss);
	let ey = ObjMove_GetY(objBoss);

	ObjEnemy_SetIntersectionCircleToShot(objBoss, ex, ey, 32); 
	//ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ex, ey, 24);

	SetShotAutoDeleteClip(256, 256, 256, 256);

	ObjEnemy_SetDamageRate(objBoss,1,1);
	if(start){
		ObjEnemy_SetDamageRate(objBoss,25,12.5);
		angleInc+=2;
	}
	
	Start;

	frame++;

	yield;
}

task Pattern{
	loop{
		if(start){
			FireSphere(1);
			wait(110);
			FireCircle;
			FireLaser(1);
			wait(180);
			FireSphere(2);
			wait(110);
			FireCircle;
			FireLaser(2);
			wait(180);
		}
		yield;
	}
}

task Start{
	if(frame == 60){
		Spell("Astrology \"Guidance of the North Star\"");
		ObjEnemyBossScene_StartSpell(objScene);
		Aura;
		Cutin;
		wait(60);
		nTestCharge(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),90,255,200,0);
		PlaySE(ChargeSE);
		Attack = true;
		wait(100);
		EShockwave(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 0.09);
		PlaySE(ExpSE2);
		start=true;
		//Attack=false;
		//MagicCircle2;
	}
}	

task FireSphere(type){
	let radius = 15;
	loop(60){
		while(Obj_IsDeleted(objBoss)){yield;}
		let angleT = rand(0,360);
		let shot = CreateShotA1(ObjMove_GetX(objBoss)+radius*cos(angleT), ObjMove_GetY(objBoss)+radius*sin(angleT), 0, angleT, MIKO_ORB_BLUE, 0);
		BCommands(shot, radius, type);
		PlaySE(ShotSE2);
		radius += 5;
		yield;
	}
}
task BCommands(shot, radius, type){
	let alpha = 0;
	let spd = rand(1,2);
	ObjRender_SetAlpha(shot, 0);
	ObjShot_SetSpellResist(shot, true);
	ObjShot_SetIntersectionEnable(shot, false);
	ObjRender_SetBlendType(shot, BLEND_ADD_ARGB);
	ObjRender_SetScaleXYZ(shot, 0.7, 0.7, 1);
	MetalRotate(shot, type, ObjMove_GetAngle(shot), radius, spd/1.5, 0); 
	loop(15){ alpha += 100/15; ObjRender_SetAlpha(shot, alpha); yield; }
	wait(90);
	ObjRender_SetScaleXYZ(shot, 1, 1, 1);
	ObjShot_SetIntersectionEnable(shot, true);
	MetalRotate(shot, type, ObjMove_GetAngle(shot), radius, spd, 0); 
	ObjRender_SetAlpha(shot, 255);
	wait(90);
	ObjShot_FadeDelete(shot);
}

task FireLaser(type){
	let angleT = rand(0,360);
	loop(9){
		let laser = CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), angleT, 1000, 30, 60, LASER_BLUE, 130);
		PlaySE(LaserSE1);
		LCommands(laser, type);
		angleT += 360/9;
	}
}
task LCommands(laser, type){
	let spd = 10;
	loop(121){
		if(type == 1){ObjStLaser_SetAngle(laser, ObjStLaser_GetAngle(laser)+spd);}
		else if(type == 2){ObjStLaser_SetAngle(laser, ObjStLaser_GetAngle(laser)-spd);}
		spd -= 10/120;
		yield;
	}
}

task FireCircle{
	let angleT = rand(0,360);
	loop(16){
		CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 1.5, angleT, RICE_M_BLUE, 30);
		angleT += 360/16;
	}
}

task MetalRotate(shot, type, angle, radius, rspeed, mspeed){
	let inc = 0;
	if(type == 1){
		while(!Obj_IsDeleted(shot)){
			inc += rspeed;
			ObjMove_SetAngle(shot, ObjMove_GetAngle(shot)-rspeed);
			ObjMove_SetX(shot, ObjMove_GetX(objBoss)+radius*cos(angle-inc));
			ObjMove_SetY(shot, ObjMove_GetY(objBoss)+radius*sin(angle-inc)+mspeed);
			yield;
		}
	}
	else if(type == 2){
		while(!Obj_IsDeleted(shot)){
			inc += rspeed;
			ObjMove_SetAngle(shot, ObjMove_GetAngle(shot)+rspeed);
			ObjMove_SetX(shot, ObjMove_GetX(objBoss)+radius*cos(angle+inc));
			ObjMove_SetY(shot, ObjMove_GetY(objBoss)+radius*sin(angle+inc)+mspeed);
			yield;
		}
	}
}
