#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["JunkoNon4"]
#Text["By Lusus"]
#include ".\Functions.dnh"
#Background[".\JunkoBackground.dnh"]
#BGM[".\se/junko.ogg"]
#System[".\default_system/Default_System.txt"]

let objBoss; //global variable, can be used anywhere
let objScene = GetEnemyBossSceneObjectID(); //declares global variable enemy boss scene object


@Event{
	alternative(GetEventType())
    case(EV_REQUEST_LIFE){ //Sets boss's life
        SetScriptResult(2000);
    }
    case(EV_REQUEST_TIMER){ //Sets attack time
        SetScriptResult(40);
    }
    case(EV_REQUEST_SPELL_SCORE){ //Spellcard bonus
        SetScriptResult(1000000);
    }
}

@Initialize{
	toggleaura(1);
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS); //gives the variable a value, object ID that makes it a boss
	ObjEnemy_Regist(objBoss); //registers it as an enemy
	ObjMove_SetDestAtFrame(objBoss, GetCenterX(), 120, 40); //moves boss to destination

 //declares spellcard
	TDrawLoop; //calls TDrawLoop task
	TFinalize; //calls TFinalize task
	MainTask; //calls MainTask task
}

@MainLoop{
	ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); 
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 24);
	// CircleToShot means any player shoot hitting this will damage the boss, CircleToPlayer kills the player on colission
	yield; //necessary for task functions to work
}

task MainTask {
	wait(50);
	chargeeffect;
	wait(55);
	ObjEnemy_SetDamageRate(objBoss, 48, 34);
	fireA;
}

task fireA {
	let graphic = 539;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		let angle1 = rand(0,360);
		let angle2 = angle1;
		let c = 0;
		let n = 13;
		let n2 = 11;
		let s = 2;
		
		loop(1){ 
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
			loop(n){
				let bullet = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), s, angle1, graphic, 0);
				ObjShot_SetSpellResist(bullet, true);
				enddelete(bullet);
				loop(n2){
					let bullet2 = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), s, angle2, graphic, 0);
					gravity(bullet, bullet2, angle2,s);
					enddelete(bullet2);
					visible(bullet2,false);
					angle2 += 360/n2;
				}
				angle1 += 360/n;
				angle2 = angle1;
			}
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(shot1, shot1v);}
		}
		wait(40);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(kira3, kira3v);}
		wait(10);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){bmovement(35, 50, 0, 0, 35);}

		wait(100);
	}
}

task gravity(bullet,bullet2, angle2, s){
	wait(40);
	visible(bullet,false);
	visible(bullet2,true);
	let rad = 0;
	let ln = 75;
	let radp = 1.2;
	let s1 = s/4;
	let s2 = s/2;
	ObjMove_SetSpeed(bullet2, 0);
	while(!Obj_IsDeleted(bullet2)){
		ObjMove_SetSpeed(bullet, s1);
		loop(ln){
			ObjMove_SetPosition(bullet2, ObjMove_GetX(bullet) + rad * cos(angle2), ObjMove_GetY(bullet) + rad * sin(angle2));
			rad+=radp;
			wait(1);
		}
		ObjMove_SetSpeed(bullet, s2);

		loop(ln){
			ObjMove_SetPosition(bullet2, ObjMove_GetX(bullet) + rad * cos(angle2), ObjMove_GetY(bullet) + rad * sin(angle2));
			rad-=radp;
			wait(1);
		}
	}
	wait(500);
	Obj_Delete(bullet);
	Obj_Delete(bullet2);
}

task visible(bullet,v){
	ObjShot_SetAutoDelete(bullet, v);
	ObjShot_SetIntersectionEnable(bullet, v);
	Obj_SetVisible(bullet, v);
}