/*#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Doremy (Spell 4)"]
#Text["pink blob girl"]
#System["./data/system/Awalys_System.dnh"]
#Player["./data/player/Sanae by Awalys/player.dnh"]
#Background["./data/background/Boss_BG.dnh"]*/

#include"./data/shot/CrispBright4/Shotconstants.dnh"
#include"./data/lib/lib_main.dnh"
#include"./data/lib/Boss_Anim.dnh"
#include"./data/lib/shot_lib.dnh"

let frame = 0;
let time = 0;
let timer = 0;
let move = 0;

let stars = false;
let butterflies = false;
let hearts = false;

let phase2 = 0;
let phase3 = 0;
let phase4 = 0;

let inc = 90;
let a = 90;

let id = "spell4";

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(8000/5);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(99);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(999000000);
	}
}

@Initialize{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);

    ObjMove_SetDestAtWeight(objBoss, cx, 80, 15, 6);

	TFinalizeFinal(id);
	StartSlow(TARGET_ALL, 60);
	BossMarker;
	MagicCircle(objBoss);
	BossRender;
	Name("Doremy Sweet");

	Pattern1; Pattern2; Pattern3; Pattern4;
	//SpawnClouds;
	PhasesTime;
}

@MainLoop{
	SetShotAutoDeleteClip(256, 256, 256, 256);

	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		ObjEnemy_SetIntersectionCircleToPlayer(objBoss, GetBX, GetBY, 16);
		ObjEnemy_SetIntersectionCircleToShot(objBoss, GetBX, GetBY, 24);
	}

	ObjEnemy_SetDamageRate(objBoss, 1, 1);
	if(start){
		ObjEnemy_SetDamageRate(objBoss, 20, 5);
		inc += 2.02535;
	}
	
	Start;
	Phases;

	frame++;

	yield;
}

task Pattern1{
	loop{
		if(start){
			Rainbows;
			wait(8);
		}
		yield;
	}
}
task Pattern2{
	loop{
		if(start && stars){
			Stars;
			wait(8);
		}
		yield;
	}
}
task Pattern3{
	loop{
		if(start && butterflies){
			Butterflies;
			wait(8);
		}
		yield;
	}
}
task Pattern4{
	loop{
		if(start && hearts){
			Hearts;
			wait(8);
		}
		yield;
	}
}

task SpawnClouds{
	loop{
		if(start){
			EClouds;
			wait(60*4);
		}
		yield;
	}
}

task Charge(frame){
	nTestCharge(GetBX, GetBY, frame, 200, 0, 255);
	Play(ChargeSE, 40);
}

task Start{
	if(frame == 60){
		Spell("Flashback \"A Dream During One's Final Moments\"", id);
		ObjEnemyBossScene_StartSpell(objScene);
		Cutin;
		wait(60);
		Charge(90);
		Play(ChargeSE, 40);
		Attack = true;
		wait(100);
		//EShockwave(GetBX,GetBY, 0.09);
		//Play(ExpSE2, 40);
		start=true;
		//Attack=false;
	}
}

task Phases{
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 6000/5){
		phase2 ++;
	}
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 4000/5){
		phase3 ++;
	}
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 2000/5){
		phase4 ++;
	}

	if(phase2 == 1){
		EShockwave(GetBX, GetBY, 0.09);
		EExplosion(GetBX, GetBY, 300);
		Play(ExpSE2, 40);
		stars = true;
	}
	if(phase3 == 1){
		EShockwave(GetBX, GetBY, 0.09);
		EExplosion(GetBX, GetBY, 300);
		Play(ExpSE2, 40);
		butterflies = true;
	}
	if(phase4 == 1){
		EShockwave(GetBX, GetBY, 0.09);
		EExplosion(GetBX, GetBY, 300);
		Play(ExpSE2, 40);
		hearts = true;
	}
}
task PhasesTime{
	while(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 20){ yield; }
	loop(2){ phase2++; phase3++; phase4++; yield; }
}

task Rainbows{ // i was too lazy to change this poor coding ree
	ascent(i in 2..10){
		let shot1 = CreateShotA2(GetBX+60*cos(inc+a), GetBY+60*sin(inc+a), 0.01*i/8, inc+a, 0.08*i/8, 6*i/12, BALL_BS_RED_INV+i*0.9-1, 20);
		let shot2 = CreateShotA2(GetBX+60*cos(-inc+a), GetBY+60*sin(-inc+a), 0.01*i/8, -inc+a, 0.08*i/8, 6*i/12, BALL_BS_RED_INV+i*0.9-1, 20);
		ObjRender_SetBlendType(shot1, BLEND_ADD_ARGB);
		ObjRender_SetBlendType(shot2, BLEND_ADD_ARGB);
		ObjShot_SetSpellResist(shot1, true);
		ObjShot_SetSpellResist(shot2, true);
		Play(ShotSE1, 30);
	}
}

task Stars{
	if(stars){
		ascent(i in 3..6){
			let shot1 = CreateShotA2(GetBX, GetBY, 0.01*i/8, inc-i*3+a, 0.08*i/8, 6*i/12, STAR_M_RED_INV, 20);
			let shot2 = CreateShotA2(GetBX, GetBY, 0.01*i/8, -inc+i*3+a, 0.08*i/8, 6*i/12, STAR_M_RED_INV, 20);
			ObjMove_SetAngularVelocity(shot1, 0.2);
			ObjMove_SetAngularVelocity(shot2, -0.2);
			ObjRender_SetBlendType(shot1, BLEND_ADD_ARGB);
			ObjRender_SetBlendType(shot2, BLEND_ADD_ARGB);
			ObjShot_SetSpellResist(shot1, true);
			ObjShot_SetSpellResist(shot2, true);
			Play(ShotSE1, 30);
		}
	}
}

task Butterflies{
	if(butterflies){
		ascent(i in 3..6){
			let shot1 = CreateShotA2(GetBX, GetBY, 0.01*i/8, inc-i*3+a, 0.08*i/8, 6*i/12, BUTTERFLY_PURPLE_INV, 20);
			let shot2 = CreateShotA2(GetBX, GetBY, 0.01*i/8, -inc+i*3+a, 0.08*i/8, 6*i/12, BUTTERFLY_PURPLE_INV, 20);
			ObjRender_SetBlendType(shot1, BLEND_ADD_ARGB);
			ObjRender_SetBlendType(shot2, BLEND_ADD_ARGB);
			ObjMove_SetAngularVelocity(shot1, -0.2);
			ObjMove_SetAngularVelocity(shot2, 0.2);
			ObjShot_SetSpellResist(shot1, true);
			ObjShot_SetSpellResist(shot2, true);
			Play(ShotSE1, 30);
		}
	}
}

task Hearts{
	if(hearts){
		ascent(i in 3..6){
			let shot1 = CreateShotA2(GetBX+60, GetBY, 0.01*i/8, inc-i*0+a, 0.08*i/8, 6*i/12, HEART_PINK_INV, 20);
			let shot2 = CreateShotA2(GetBX-60, GetBY, 0.01*i/8, -inc+i*0+a, 0.08*i/8, 6*i/12, HEART_PINK_INV, 20);
			ObjRender_SetBlendType(shot1, BLEND_ADD_ARGB);
			ObjRender_SetBlendType(shot2, BLEND_ADD_ARGB);
			ObjShot_SetSpellResist(shot1, true);
			ObjShot_SetSpellResist(shot2, true);
			Play(ShotSE1, 30);
		}
	}
}

task EClouds{
	let inc = 0;
	let x = ObjMove_GetX(objBoss);
	let y = ObjMove_GetY(objBoss);
	let al = 45;

	let loc = GetCurrentScriptDirectory() ~ "./data/img/cloud.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, loc);
	ObjSprite2D_SetSourceRect(obj, 0,0,512,512);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 50);
	ObjRender_SetScaleXYZ(obj, 0.35, 0.35, 0);
	ObjRender_SetBlendType(obj, BLEND_SUBTRACT);
	ObjRender_SetAlpha(obj, al);
	ObjRender_SetColor(obj, rand(0,255), rand(50,150), rand(50,150));
	ObjRender_SetPosition(obj, x, y, 0);
	ObjRender_SetAngleZ(obj, rand(0,360));

	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		inc+=0.008;

		ObjRender_SetScaleXYZ(obj, inc, inc, 0);
		//ObjMove_SetSpeed(obj, 1);
		//ObjMove_SetAngle(obj, rand(0,360));
		yield;
	}
	loop(45){
		ObjRender_SetAlpha(obj, al);
		al--;
		yield;
	}
}