#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["JunkoSpell4"]
#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(), 130, 40); //moves boss to destination
	toggleaura(1);
	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 {
	chargeeffect;
	wait(50);
	spellcutin("\"Pure Vengeful Hell\"");
	ObjEnemy_SetDamageRate(objBoss,9,3);
	wait(10);
	fireA;
	movement;
}

task fireA {
	let graphic = 1061;
	let angle = 90;
	let n1 = 44;
	let s = 2.35;
	let t = 12;
	let t2 = 10;
	let an = 3.1;
	let c = 0;
	let posneg = 1;
	darken;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
		loop(n1){
			let b = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), s, angle, 539,0);
			enddelete(b);
			angle += 360/n1;
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(shot1, shot1v);}
		wait(t2);
		angle += 360/n1/an*posneg;
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
		loop(n1){
			let fire = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), s, angle, 1062,0);
			enddelete(fire);
			ObjRender_SetBlendType(fire, BLEND_ADD_ARGB);
			angle += 360/n1;
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(shot1, shot1v);}
		angle += 360/n1/an*posneg;
		if(c == 1){posneg *= -1; c = 0;}
		else{c+=1;}
		wait(t);
	}
}

task movement{
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		wait(210);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
		bmovement(50, 70, 5, 10, 30);
	}
}

task darken{
	let path1 = GetCurrentScriptDirectory() ~ "img/black.png";
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj1, BLEND_ALPHA);
    Obj_SetRenderPriority(obj1, 0.25);
    ObjPrim_SetTexture(obj1, path1);
    ObjSprite2D_SetSourceRect(obj1, 0, 0, 512, 512);
    ObjSprite2D_SetDestRect(obj1, -256, -256, 256, 256);
    ObjRender_SetPosition(obj1, GetCenterX(), GetCenterY(), 1);
   	enddelete(obj1);
  	let alpha = 0;
  	let n = 30;
  	let ap = 60/n;
  	loop(n){
    	ObjRender_SetAlpha(obj1, alpha);
    	alpha += ap;
    	wait(1);
    }
}