#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Nonspell 4"]
#Text[""]
#BGM["./../bgm/Boss.ogg"]
#Background["./../BG.txt"]
#Player["./../player/Ultima's DDC Reimu B/ReimuH.dnh", "./../player/gt_medicine/PracticeP.txt"]
#System["./../System/Default_System.txt"]

#include "./../lib/Shot/ZUNShot/ZUNShot_Const.txt"

#include "./../lib/Render/EnemyMark.txt"
#include "./../FxnSFX.dnh"
#include "./../lib/Funcs.txt"
#include "./../lib/GoodLib.txt"
#include "./../lib/Render/MagicCircle.txt"

#include "./../lib/Render/Gizmo's Touhou Boss Sprite Library/GizmoSpriteLibrary.txt"
#include "./../lib/Render/Gizmo's Touhou Boss Sprite Library/GizmoFSpriteLibrary.txt"

let objBoss;
let pass;

let count = 0;
let countx = 0;

let shot = SoundSFX(GetCurrentScriptDirectory~"./../lib/se/shot1.wav");
let shot2 = SoundSFX(GetCurrentScriptDirectory~"./../lib/se/shot2.wav");
let charge = SoundSFX(GetCurrentScriptDirectory~"./../lib/se/charge.wav");
let discharge = SoundSFX(GetCurrentScriptDirectory~"./../lib/se/famdie.wav");
let laa = SoundSFX(GetCurrentScriptDirectory~"./../lib/se/wuon.wav");

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(4000);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(42);
    }
    case(EV_REQUEST_SPELL_SCORE){
        SetScriptResult(1000000);
    }
}
@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	
	SetCommonData("Pretty", false);
	SetCommonData("Sun", true);
	BFX_Init();
	
    ObjMove_SetDestAtFrame(objBoss, gx(), 70, 30);
	TDrawLoop;
    TFinalize;
}

@MainLoop{
    ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); 
    ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 24);
    if(count == 0){
        round_bullets;
        count = 0;
    } 
    count++;
    yield;
}

task TDrawLoop{
	renderAlicePCB(objBoss);
	DrawName("Alice Margatroid", "YELLOW");
	EnemyMarker(objBoss);
	magiccircle(objBoss, 1.75, 0, 150, 100, true);
}

task Enemy(type, x, y, frame, vo){
	let objEnemy;
	objEnemy = ObjEnemy_Create(OBJ_ENEMY);
    ObjEnemy_Regist(objEnemy);
	
	ObjMove_SetX(objEnemy, bx);
	ObjMove_SetY(objEnemy, by);
	
	hitbox(objEnemy, 20);
	
	pass = 0;
	Obj_SetValue(pass, "die", 1);
	
	let ex = 0;
	let ey = 0;
	let ang = 0;
	let rang = 90;
	
	let b = 2;
	let num = 1;
	
	//1=B; 2=R
	renderTinyGoreF(objEnemy, type);
	magiccircle(objEnemy, 0.5, 191, 64, 64, false);
	
	ObjMove_SetX(objEnemy, bx);
	ObjMove_SetY(objEnemy, by);
	ObjMove_SetDestAtFrame(objEnemy, x, y, frame);
	
	wait(frame);
		ex = ObjMove_GetX(objEnemy);
		ey = ObjMove_GetY(objEnemy);
	//DelayLaser(ex, ey, gg, 500, 50, 1, 24, 60*3);
	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		if(Obj_GetValueD(pass, "die", 1) == 0){Obj_Delete(objEnemy);}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
		
		//rang = GetAngleToPlayer(objEnemy);
		ascent(A in 0..60){
			if(Obj_GetValueD(pass, "die", 1) == 0){Obj_Delete(objEnemy);}
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
			
			ex = ObjMove_GetX(objEnemy);
			ey = ObjMove_GetY(objEnemy);
			
			ascent(i in 0..b){
				ang = rang + 360/b*i;
				
				ascent(j in -(num)..num){
					DelayLaser(ex, ey, ang + j, 550, 40, 1, 18, 60*2);
				}
			}
			
			wait(60*1/10);
			
			ascent(i in 0..b){
				ang = rang + 360/b*i;
				
				CreateStraightLaserA1(ex, ey, ang, 550, 30, 60*0.65, 18, 60*1);
			}
			//wait(60*1);
			bitch();
			ObjSound_SetVolumeRate(laa, 90);
			rang += vo;
		}

		wait(60*4);
	}
	
	Obj_Delete(objEnemy);
	
	task bitch(){
		wait(60 * 1);
		PlaySFX(laa);
	}
	
	task hitbox(obj, rad){
		wait(frame);
		while(!Obj_IsDeleted(obj)){
			ObjEnemy_SetIntersectionCircleToShot(objEnemy, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), rad); 
			ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), rad);
			
			yield;
		}
	}
}

task round_bullets{
ObjEnemy_SetDamageRate(objBoss, 0, 0);
wait(60);
PlaySFX(charge);
Concentration(bx, by, 60*1.5, 150, 2, 10/2, 1, BLEND_ADD_ARGB, 255, 255, 0, 8);
wait(60*1.25);
PlaySFX(discharge);
ConcentrationExplosion(bx, by, 60*1.5, 250, 8/4, 1, BLEND_ADD_ARGB, 179, 179, 0, 8);
ObjEnemy_SetDamageRate(objBoss, 80, 40);
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
    let rang = 0;
	
	//{
		let ang;
		
		let l = 57;
		let b = 14;
		
		let n = b*1.5;
	//}
	
	Enemy(2, bx-120, by, 60*1/3, +7);
	Enemy(2, bx+120, by, 60*1/3, -7);
	
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		Obj_SetValue(objBoss, "cast", 1);
		wait(60*1);
		
		ascent(i in 0..60){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
			
			PlaySFX(shot);
			ObjSound_SetVolumeRate(shot, 70);
			
			rang = GetAngleToPlayer(objBoss);
			ascent(c in 0..40){
				ascent(a in 0..b){
					ang = rang + 360/b*a;
					
					let L = CreateShotA1(bx, by, 1/3 + c/n, ang, 56, 0);
				}
				rang += l;
			}
			
			l *= -1;
			wait(60*8);
		}
	
		Obj_SetValue(objBoss, "cast", 0);
		wait(60 * 4);
    }
}

task DelayLaser(x, y, ang, l, w, dt, graph, delay){
	let objlaser = CreateStraightLaserA1(x, y , ang, l, w, dt, graph, delay);
	ObjStLaser_SetSource(objlaser, false);
	//ObjStLaser_SetAngle(objlaser, angtoP(objBoss));
	
	loop(delay-1){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){Obj_Delete(objlaser); return;}
		yield;
	}
	Obj_Delete(objlaser);
}

task TFinalize {
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}