#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["JunkoSpell1"]
#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(500);
    }
    case(EV_REQUEST_TIMER){ //Sets attack time
        SetScriptResult(65);
    }
    case(EV_REQUEST_SPELL_SCORE){ //Spellcard bonus
        SetScriptResult(1000000);
    }
}

@Initialize{
	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

	TDrawLoop; //calls TDrawLoop task
	TFinalize; //calls TFinalize task
	MainTask; //calls MainTask task
	SetCommonData("Card",true);
	//SetSpellText("&quot;Pure Light of the Palm&quot;", "",20);
}

@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 {
	chargeeffect;
	wait(50);
	toggleaura(1);
	spellcutin("\"Pristine Display of Power\"");
	ObjEnemy_SetDamageRate(objBoss,8, 3);
	junkoattack;
	fireB;
	wait(60);
	fireA;
}

task fireA {
	let n = 46;
	let s = 3.1;
	let ms = 1.2;
	let acc = -0.05;
	let angle = 0;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
		loop(n){
			let bullet = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), s, angle, acc, ms, 539, 0);
			enddelete(bullet);
			angle += 360/n;	
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(shot1, shot1v);}
		angle += 360/n/4 * posneg2;
		wait(30);
	}
}

let posneg2 = 1;
let en = 55;
let ems = 2.3;

let en2 = en;
let ems2 = ems;

task fireB{
	let s = 5;
	let acc = -0.1;
	let angle = 0;
	let c = 0;
	let c2 = 0;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
		if(c < 1){angle = rand(-70,-120); c+= 1;}
		else{angle = rand(70,120); c = 0;}
		if(c2 < 6){
			en = en2;
			ems = ems2;
			st = 0;
			bullet1(s,angle,acc,1);
			bullet1(s,angle,acc,-1);
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(shot2, shot2v);}
			wait(85);
			c2+=1;
		}
		else{
			wait(20);
			chargeeffect;
			wait(55);
			posneg2 *= -1;
			en = 50;
		    ems = 1.5;
		    st = 1;
			bullet2(s,acc);
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(shot2, shot2v);}
			wait(255);
			c2 = 0;
		}

		
	}
}

task bullet1(s,angle,acc,posneg){
	let bullet = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), s, 90+(angle*posneg), acc, 0, 400, 0);
	enddelete(bullet);
	explode(bullet);
}

task bullet2(s,acc){
	let n = 0;
	bullet1(s,n,acc,1);
	bullet1(s,n+180,acc,-1);
	n+= 45;
	loop(3){
		bullet1(s,n,acc,1);
		bullet1(s,n,acc,-1);
		n+= 45;
	}
		
}

let st = 0;

task explode(bullet){
	let s = 0;
	let acc = 0.1;
	let ln = 20;
	let sc = 1;
	while(ObjMove_GetSpeed(bullet) > 1.2){yield;}
	loop(ln){
		ObjRender_SetScaleXYZ(bullet, sc, sc, 1);
		sc -= 1/ln;
		wait(1);
	}
	let x = ObjMove_GetX(bullet);
	let y = ObjMove_GetY(bullet);
	if(!Obj_IsDeleted(bullet) && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		Obj_Delete(bullet);
		let angle = rand(0,360);
		if(st == 1){angle = 0;}
		loop(en){
			let bullet = CreateShotA2(x, y, s, angle, acc, ems, 535, 0);
			enddelete(bullet);
			angle += 360/en;	
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(shot2, shot2v);}
	}
}