#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["SeiranNon1"]
#Text["By Lushie/Lusushime"]
#include ".\Functions.dnh"
#Background[".\Stage1bg.dnh"]
#System[".\default_system/Default_System.txt"]
#Player[".\player/LoLK Marisa (Lushie)/MarisaH.dnh"]

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(spelllife);
    }
    case(EV_REQUEST_TIMER){ //Sets attack time
        SetScriptResult(spelltime);
    }
    case(EV_REQUEST_SPELL_SCORE){ //Spellcard bonus
        SetScriptResult(1000000);
    }
    case(EV_PLAYER_SHOOTDOWN){
    	pdreset(spelltime,spelllife);
    }
}

let spelltime = 35;
let spelllife = 3000;

@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

 //declares spellcard
 	bossanimation("seiran");
	TDrawLoop; //calls TDrawLoop task
	TFinalize; //calls TFinalize task
	MainTask; //calls MainTask task
}

@MainLoop{
	ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), bosshitbox); 
	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{
	startingposition(GetCenterX, 120);
	bmoveto(GetCenterX, 120, 60);
	wait(5);
	startattacking;
	
}

task startattacking{
	pointdevicepositions;
	pdplayerx = GetCenterX;
	pdplayery = 400;
	pdcount2 = pdcount;
	let pdc = pdcount;
	setauracolor("blue");
	chargeeffect2("red");
	wait(45);
	if(pdc == pdcount){
		spelldamage(35, 130, 13);
		fireA(pdc);
	}
}


task fireA(pdc) {
	let sangle = 90;
	let posneg = 1;
	
	let t = 5;
	let ap2 = 0;
	let ap3 = 30;
	
	let n2 = 9;
	let s = 3.5;
	let dt = 40;
	let dt2 = 10;
	let ms = 2;
	let dacc = -(s-ms)/dt;
	
	let od = 40;
	let wt = 999;
	let dp = 15;

	let ap = 23;
	let on = 9;

	if(diff == 2){
		t = 4;
		ap = 17;
		on = 12;
		n2 = 12;
		ms = 2.15;
		od = 50;
		dp = 14;

	}
	if(diff == 3){
		t = 3;
		ap = 14;
		on = 13;
		n2 = 13;
		ms = 2.3;
		od = 50;
		dp = 13;

	}
	while(lifecheck(pdc) == true){
		pyramid(GetAngleToPlayer(objBoss));
		wait(25);
		if(lifecheck(pdc) == false){return;}
		let n = on;
		
		move = 3;
		if(lifecheck(pdc) == false){return;}
		let sangle = rand(90+ap, 90-ap);
		let d = od;
		loop(n2){
			if(lifecheck(pdc) == false){return;}
			let angle = sangle;
			if(n != 1){angle = angle - ap/2*(n-1);}
			let array = [];
			loop(n){
				let bullet = CreateShotA2(BossX + d * cos(angle), BossY + d * sin(angle), s, angle, dacc, ms, 296, 0);
				ObjRender_SetBlendType(bullet, BLEND_ADD_ARGB);
				Obj_SetRenderPriorityI(bullet, 49);
				
				//ObjMove_AddPatternA2(bullet, dt, 0, ObjMove_GetAngle(bullet), acc, vel*posneg, ms);
				array = array ~ [bullet];
				angle += ap;
			}
			bulletcommand(array);
			spawncustomarray2(array, 1, 0.2, 255, 255, dt-dt2, 0);
			SE_Play(shot1, shot1v);
			n += 1;
			d -= dp;
			wait(t);
			
		}
		wait(10);
		if(lifecheck(pdc) == false){return;}
		bmovement(10, 25, 5, 15, 40);
		wait(50);
	}

	task pyramid(angle){
		let n2 = 1;
		let n = 7;
		let d = 0;
		move = 3;
		let s = 3.7;
		let acc = 0;
		let ms = s;
		let ap = 2.5;
		let t = 3;
		if(diff == 2){
			s = 4.5;
			n = 8;
			ap = 2.5;
		}
		if(diff == 3){
			s = 5.3;
			n = 9;
			ap = 2.5;
		}
		loop(n){
			if(lifecheck(pdc) == false){return;}
			let angle2 = angle;
			if(n2 != 1){angle2 -= ap/2*(n2-1);}
			let array = [];
			loop(n2){
				let bullet = CreateShotA2(BossX + d * cos(angle2), BossY + d * sin(angle2), s, angle2, acc, ms, 193, 0);
				array = array ~ [bullet];
				angle2 += ap;
			}
			spawncustomarray2(array, 1, 1.5, 70, 255, 12, 0);
			SE_Play2(shot1, shot1v);
			n2 += 1;
			wait(t);
		}
	}
	task bulletcommand(array){
		wait(dt-dt2);
		let l = length(array);
		if(lifecheck(pdc) == false){return;}
		ascent(i in 0..l){
			if(!Obj_IsDeleted(array[i])){ObjRender_SetBlendType(array[i], BLEND_ALPHA);}
		}
	}
	
}