#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["JunkoSpell5"]
#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(60);
    }
    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

	TDrawLoop; //calls TDrawLoop task
	TFinalize; //calls TFinalize task
	MainTask; //calls MainTask task
	SetCommonData("Card",true);
}

@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(60);
	chargeeffect;
	wait(55);
	spellcutin("\"Simple Danmaku for Silencing Brats\"");
	ObjEnemy_SetDamageRate(objBoss,17, 3);
	fireA;
}

task fireA {
	junkoattack;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		let angleT = GetAngleToPlayer(objBoss)+180;
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
		if(GetPlayerX() > ObjMove_GetX(objBoss)){
			createlaser(angleT, -1);
		}
		else{createlaser(angleT, 1);
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(laser1, laser1v);}
		wait(65);
	}
}

task createlaser(angleT, posneg){
	let graphic = 535;
	let n = 111;
	let s = 8.5;
	//95
	if(posneg == 1){graphic = 537;}
	let laser1 = CreateCurveLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), s, angleT, 
			25, 30, graphic, 0); 
	enddelete(laser1);
	//70,30
	let anglebullet = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), s, angleT, 1, 0);
	let anglebullet2 = CreateShotA1(GetPlayerX(), GetPlayerY(), 0, angleT, 1, 0);
	Obj_SetRenderPriority(anglebullet,0);
	ObjShot_SetIntersectionEnable(anglebullet, false);
	Obj_SetRenderPriority(anglebullet2,0);
	ObjShot_SetIntersectionEnable(anglebullet2, false);
	ObjCrLaser_SetTipDecrement(laser1, 0);
	laserdirections(laser1,anglebullet, anglebullet2, posneg, graphic);
}

let lv = 6.8;

task laserdirections(laser, anglebullet, anglebullet2, posneg, graphic){
	let x = GetPlayerX();
	let y = GetPlayerY();
	createbullet(laser,posneg, graphic);
	ObjMove_AddPatternA2(laser, 0, NO_CHANGE, NO_CHANGE, 0, posneg * -lv, 0);
	ObjMove_AddPatternA2(anglebullet, 0, NO_CHANGE, NO_CHANGE, 0, posneg * -lv, 0);
	wait(30);
	//ObjMove_AddPatternA2(anglebullet, 0, 0, NO_CHANGE, 0, posneg * -7, 0);
	let angle = GetAngleBetween(anglebullet2,anglebullet);
	ObjMove_AddPatternA2(laser, 0, NO_CHANGE, angle, 0, 0, 0);
	Obj_Delete(anglebullet);
	Obj_Delete(anglebullet2);
}

task createbullet(laser, posneg, graphic){
	let angle = ObjMove_GetAngle(laser);
	let count = 1;
	let s = 0;
	let ms = 1.4;
	let acc = 0.05;
	let delay = 15;
	let an = 41;
	let ap = 0;
	while(!Obj_IsDeleted(laser)){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
		let bullet = CreateShotA2(ObjMove_GetX(laser), ObjMove_GetY(laser), s, angle+ap, acc, ms, graphic, delay);
		if(count == 1){let bullet2 = CreateShotA2(ObjMove_GetX(laser), ObjMove_GetY(laser), s, angle-ap, acc, ms, graphic, delay);
		bulletdelete(bullet2);
	    count = 0;}
	    count += 1;
		ap += 360/an;
		bulletdelete(bullet);
		wait(1);
	}
}

task bulletdelete(bullet){
	enddelete(bullet);
	while(ObjMove_GetY(bullet) < 468 && !Obj_IsDeleted(bullet)){
		yield;
	}
	Obj_Delete(bullet);
	yield;
}