/*#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Doremy (Spell 2)"]
#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 angleInc = 0;
let inc = 0;

let gr = 0;
let train = true;
let dir = 1;

let id = "spell2";

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

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

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

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

	Pattern1; Pattern2; Pattern3;
	//MovementLoop(100, 180);
}

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

	ObjEnemy_SetIntersectionCircleToPlayer(objBoss, GetBX, GetBY, 16);
	ObjEnemy_SetIntersectionCircleToShot(objBoss, GetBX, GetBY, 24);

	ObjEnemy_SetDamageRate(objBoss, 1, 1);
	if(start){
		ObjEnemy_SetDamageRate(objBoss, 20, 10);
		inc += 4.1328682;
	}
	
	Start;
	RainbowEff;

	frame++;

	yield;
}

task Pattern1{
	let angle = 0; let inc = 45; // pardon my spaghetti code
	loop{
		if(start){
			train = true; Attack = true;
			angle = GetAngleToPlayer(objBoss);
			BlinkDLaser(angle, 60*1.5); Play(ShotSE4, 30); Play(ChargeSE2, 30); 
			loop(3){ Charge(45); wait(45); }
			loop(60*2/4){
				while(Obj_IsDeleted(objBoss)){ yield; }
				Train1(angle, inc, 2); Play(ShotSE2, 30);
				inc += 4;
				wait(4);
			}
			inc = 45;
			Attack = false;
			Movement(100);

			Charge(60); wait(60);
			train = false; dir++; 
			wait(60);

			loop(3){	
				angle = GetAngleToPlayer(objBoss);
				BlinkDLaser(angle, 60*1.5); Play(ShotSE4, 30);  Charge(60); 
				wait(60);
				loop(60*1/4){
					while(Obj_IsDeleted(objBoss)){ yield; }
					Train1(angle, 75, 0); Play(ShotSE2, 30);
					wait(4);
				}
				//Movement(50);
				wait(60*0.75);
			}
			Movement(100);
			wait(60*2);

			Charge(60);
			wait(60);
			angle = GetAngleToPlayer(objBoss)-180; Attack = true;
			ascent(i in 1..3){ // i turns the angle halfway
				BlinkDLaser2(angle, i);  //PlaySE(LaserSE2);
				loop(60*2/4){
					while(Obj_IsDeleted(objBoss)){ yield; }
					Train2(angle, i); Play(ShotSE2, 30);
					wait(4);
				}
				wait(60*0.5);
			}
			wait(60*0.5);
			Attack = false;
			Movement(100);
			wait(60*2); 
		}
		yield;
	}
}
task Pattern2{
	let angle = 0;
	loop{
		if(start && !train){
			Loop1(angle); Play(ShotSE1, 30);
			if(dir % 2 == 0){ angle += 3; }
			else{ angle -= 3; }
			wait(15);
		}
		yield;
	}
}
task Pattern3{
	loop{
		if(start && !train){
			wait(60);
			Ring;
		}
		yield;
	}
}

task RainbowEff{
	gr += 0.25; if(gr == 9){ gr = 0; }
}

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

task Start{
	if(frame == 60){
		Spell("Rainbow Sign \"Dream Station Train Derailment\"", id);
		ObjEnemyBossScene_StartSpell(objScene);
		Cutin;
		wait(60);
		Charge(90);
		Attack = true;
		wait(100);
		EShockwave(GetBX,GetBY, 0.09);
		Play(ExpSE2, 30);
		start=true;
		Attack=false;
	}
}

task Train1(angle, w, sspd){ // w is width of the half-square
	let angleT1 = angle-90;
	let r = cos(180/4) / cos(((angleT1 - angle-90) % (360/4)) - (180/4)); // trig

	loop(9){
		r = cos(180/4) / cos(((angleT1 - angle-90) % (360/4)) - (180/4)); 
		let shot = CreateShotA2(GetBX+w*r*cos(angleT1), GetBY+w*r*sin(angleT1), sspd, angle, 0.3, 8, SCALE_RED+gr, 20);
		ObjRender_SetBlendType(shot, BLEND_ADD_RGB);
		angleT1 += 180/(9-1);
		//wait(1);
	}
} 

task Ring{
	let angleT = rand(0,360);
	loop(32){
		let shot = CreateShotA2(GetBX+20*cos(angleT), GetBY+18*sin(angleT), 5, angleT, -0.1, 4, BALL_M_PURPLE_INV, 0);
		ObjRender_SetBlendType(shot, BLEND_ADD_RGB);
		angleT += 360/32;
	}
}

task Train2(angle, i){
	loop(6){
		while(Obj_IsDeleted(objBoss)){ yield; }
		Train1(angle+(360/6/i), 75, 0);
		angle += 360/6;
		wait(20);
	}
}

task BlinkDLaser(angle, f){
	let dl = DelayLaser(GetBX, GetBY, angle, 1000, 100, 1, BEAM_RED, f);
	loop(f/2/3){
		Obj_SetVisible(dl, 0);
		wait(3);
		Obj_SetVisible(dl, 1);
		wait(3);
	}
}

task BlinkDLaser2(angle, i){
	loop(6){
		while(Obj_IsDeleted(objBoss)){ yield; }
		BlinkDLaser(angle+(360/6/i), 60);
		angle += 360/6;
		Play(ShotSE4, 30);
		wait(20);
	}
}

task Loop1(angle){
	loop(9){
		while(Obj_IsDeleted(objBoss)){ yield; }
		let shot = CreateShotA2(GetBX+40*cos(angle), GetBY+40*sin(angle), 7, angle, -0.08, 3, BALL_BS_BLUE_INV, 10);
		ObjRender_SetBlendType(shot, BLEND_ADD_RGB);
		angle += 360/9;
		yield;
	}
}