#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["JunkoSpell2"]
#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
let frame = 0; 
	let frame2 = 0;
	let angle = 0;
	let angleAcc = 0;
	let radius = 0;

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

@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("\"Cleansing of Impure Lands\"");
	ObjEnemy_SetDamageRate(objBoss,10,3);
	fireA;
	borders;
	countdown;
}

let tbot = 0;
let ttop = 0;
let tleft = 0;
let tright = 0;

let cbot = 999;
let ctop = 999;
let cleft = 999;
let cright = 999;

let bmt = 30;

task fireA {
	let n = 5;
	let s = 6;
	let graphic = 399;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		loop(n){
			junkoattack;
			wait(10);
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
			let bullet = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), s, GetAngleToPlayer(objBoss), graphic, 0);
			explode(bullet,ldn);
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(shot1, shot1v);}
			wait(45);
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
			bmovement(25, 35, 5, 10, bmt);
			wait(50);
		}
		junkoattack;
		chargeeffect;
		wait(70);
		let angle = GetAngleToPlayer(objBoss)-90;
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
		loop(3){
			let bullet = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), s, angle, graphic, 0);
			ObjShot_SetSpellResist(bullet, true);
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(shot1, shot1v);}
			explode(bullet,ldn);
			angle += 90;
		}
		fw = 7;
		loop(2){
			wait(70);
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
			bmovement(25, 35, 0, 10, bmt);
			wait(95);
		}
		wait(120);
		junkoattack;
		chargeeffect;
		wait(70);
		loop(3){
			junkoattack;
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
			let bullet = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), s, GetAngleToPlayer(objBoss), graphic, 0);
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(shot1, shot1v);}
			explode(bullet,ldn);
			wait(20);
		}
		fw = 8;
		n = 5;
		wait(80);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
		bmovement(30, 60, 0, 10, bmt+5);
		wait(100);
	}
}

let ldn = 8;

task explode(bullet,n){
	Obj_SetRenderPriorityI(bullet, Obj_GetRenderPriorityI(bullet)+1);
	ObjShot_SetAutoDelete(bullet, false);
	enddelete(bullet);
	bottomy(bullet,n);
	topy(bullet,n);
	leftx(bullet,n);
	rightx(bullet,n);
}

task removeindicator(indicator,angle, place){
	let check = 93483;
	wait(1);
	loop{
		if(angle == 90){check = ctop;}
		if(angle == 270){check = cbot;}
		if(angle == 0){check = cleft;}
		if(angle == 180){check = cright;}
	    wait(1);
		if(check < 1){
			Obj_Delete(indicator);
		}
	}
}

task countdown{
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		if(ctop > 0){ctop += -1;}
		if(cbot > 0){cbot += -1;}
		if(cleft > 0){cleft += -1;}
		if(cright > 0){cright += -1;}

		if(ctop == 0){ttop = 0; topflag = 0;}
		if(cbot == 0){tbot = 0; botflag = 0;}
		if(cleft == 0){tleft = 0; leftflag = 0;}
		if(cright == 0){tright = 0; rightflag = 0;;}
		wait(1);
	}
}

let botflag = 0;
let topflag = 0;
let leftflag = 0;
let rightflag = 0;

task bottomb{
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		let x = 0;
		let y = 0;
		if(tbot > 0 && botflag == 0 && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			x = 0;
			y = 443;
			createindicators(x, y, 270, 384/20, 0, 20, 1);
			botflag = 1;
		}
		if(tbot > 1 && botflag == 1 && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			x = 0;
			y = 438;
			createindicators(x, y, 270, 384/20, 0, 20, 2);
			botflag = 2;
		}
		if(tbot > 2 && botflag == 2 && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			x = 0;
			y = 433;
			createindicators(x, y, 270, 384/20, 0, 20, 3);
			botflag = 3;
		}
		wait(1);
	}
}

let duration = 400;
let ds = 0.9;
let maxs = 1.4;
let thirds = 1.2;
let ftime = 0;

task createindicators(x,y, angle, xplus, yplus, n, place){
	let ci = 0;
	loop(n+1){
		let indicator = CreateShotA2(x, y, 0, angle, 0 , 
			0, 702, 9999);
		removeindicator(indicator,angle, place);
		enddelete(indicator);
		if(ci == 0){createwaves(indicator, x, y, angle, xplus, yplus, n, place); ci = 1;}
		x += xplus;
		y += yplus;
	}
}

task createwaves(indicator,x, y, angle, xplus, yplus, n, place){
	while(!Obj_IsDeleted(indicator)){
		let s = ds;
		let fx = x;
		let fy = y;
		let fxplus = 0;
		let fyplus = 0;
		n = 7;
		if(place == 2){s = maxs;}
		if(place == 3){s = thirds;}
		if(x == 0){fx += rand(-10, 38); fxplus = 384/fw;} //384
		if(y == 0){fy += rand(-10,46); fyplus = 448/fw;} //448
		if(fi == 0){
			loop(fw+1){
				createfire(fx,fy,s,angle);
				fx+= fxplus;
				fy+= fyplus;
			}
			wait(100);
		}
		else{fi += -1; wait(45);}
		yield;
		
	}
}

let fw = 8;

let fi = 1;
task createfire(fx, fy, s, angle){
	let check = 312923178;
	wait(rand(0,75));
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
	if(angle == 90){check = ctop;}
	if(angle == 270){check = cbot;}
	if(angle == 0){check = cleft;}
	if(angle == 180){check = cright;}
	if(check != 0){let fire = CreateShotA2(fx, fy, s, angle, 0, 2, 1061, 0); 
		enddelete(fire);
	}
}

task topb{
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		let x = 0;
		let y = 0;
		if(ttop > 0 && topflag == 0 && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			x = 0;
			y = 5;
			createindicators(x, y, 90, 384/20, 0, 20, 1);
			topflag = 1;
		}
		if(ttop > 1 && topflag == 1 && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			x = 0;
			y = 10;
			createindicators(x, y, 90, 384/20, 0, 20, 2);
			topflag = 2;
		}
		if(ttop > 2 && topflag == 2 && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			x = 0;
			y = 15;
			createindicators(x, y, 90, 384/20, 0, 20, 3);
			topflag = 3;
		}
		wait(1);
	}
}

task leftb{
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		let x = 0;
		let y = 0;
		if(tleft > 0 && leftflag == 0 && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			x = 5;
			y = 0;
			createindicators(x, y, 0, 0, 448/20, 20, 1);
			leftflag = 1;
		}
		if(tleft > 1 && leftflag == 1 && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			x = 10;
			y = 0;
			createindicators(x, y, 0, 0, 448/20, 20, 2);
			leftflag = 2;
		}
		if(tleft > 2 && leftflag == 2 && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			x = 15;
			y = 0;
			createindicators(x, y, 0, 0, 448/20, 20, 3);
			leftflag = 3;
		}
		wait(1);
	}
}

task rightb{
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		let x = 0;
		let y = 0;
		if(tright > 0 && rightflag == 0 && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			x = 379;
			y = 0;
			createindicators(x, y, 180, 0, 448/20, 20, 1);
			rightflag = 1;
		}
		if(tright > 1 && rightflag == 1 && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			x = 374;
			y = 0;
			createindicators(x, y, 180, 0, 448/20, 20, 2);
			rightflag = 2;
		}
		if(tright > 2 && rightflag == 2 && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
			x = 369;
			y = 0;
			createindicators(x, y, 180, 0, 448/20, 20, 3);
			rightflag = 3;
		}
		wait(1);
	}
}

task borders{
	bottomb;
	topb;
	leftb;
	rightb;
}

task bottomy(bullet,n){
	while(ObjMove_GetY(bullet) < 448){
		yield;
	}
	if(tbot != 3){tbot += 1;}
	cbot = duration;
	let x = ObjMove_GetX(bullet);
	let y = 448+15;
	Obj_Delete(bullet);
	createlaser1(x, y+100, 270, 612, 45, 120, 1003, 0);
	createlaser2(x, y, 8, 230, 70, 30, 1019, 0, 32, 310, lv, 1);
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(change, changev);}
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(laser1, laser1v);}
	wait(n);
	createlaser2(x, y, 8, 310, 70, 30, 1019, 0, 32, 230, -lv, 1);
}

task topy(bullet,n){
	while(ObjMove_GetY(bullet) > -1){
		yield;
	}
	if(ttop != 3){ttop += 1;}
	ctop = duration;
	let x = ObjMove_GetX(bullet);
	let y = -15;
	Obj_Delete(bullet);
	createlaser1(x, y-100, 90, 612, 45, 120, 1003, 0);
	createlaser2(x, y, 8, 50, 70, 30, 1019, 0, 32, 130, lv, 1);
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(change, changev);}
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(laser1, laser1v);}
	wait(n);
	createlaser2(x, y, 8, 130, 70, 30, 1019, 0, 32, 50, -lv, 1);

}

task rightx(bullet,n){
	while(ObjMove_GetX(bullet) < 384 || ObjMove_GetY(bullet) > 447){
		yield;
	}
	wait(1);
	while(Obj_IsDeleted(bullet)){
		yield;
	}
	if(tright!= 3){tright += 1;}
	cright = duration;
	let x = 384+15;
	let y = ObjMove_GetY(bullet);
	Obj_Delete(bullet);
	createlaser1(x+100, y, 180, 612, 45, 120, 1003, 0);
	createlaser2(x, y, 8, 140, 70, 30, 1019, 0, 32, 220, lv, 1);
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(change, changev);}
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(laser1, laser1v);}
	wait(n);
	createlaser2(x, y, 8, 220, 70, 30, 1019, 0, 32, 140, -lv, 1); 
}

task leftx(bullet,n){
	while(ObjMove_GetX(bullet) > -1 || ObjMove_GetY(bullet) > 447){
		yield;
	}
	wait(1);
	while(Obj_IsDeleted(bullet)){
		yield;
	}
	if(tleft != 3){tleft += 1;}
	cleft = duration;
	let x = -15;
	let y = ObjMove_GetY(bullet);
	Obj_Delete(bullet);
	createlaser1(x-100, y, 0, 612, 45, 120, 1003, 0);
	createlaser2(x, y, 8, 320, 70, 30, 1019, 0, 32, 40, lv, 1);
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(change, changev);}
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(laser1, laser1v);}
	wait(n);
	createlaser2(x, y, 8, 40, 70, 30, 1019, 0, 32, 320, -lv, 1);
}

task createlaser1(x, y, angle, l, w, f, graphic, delay){
	f = 70;
	let scale = w;
	let laser = CreateStraightLaserA1(x, y, angle ,l, w, f, 3,delay);
	enddelete(laser);
	wait(f);
	while(!Obj_IsDeleted(laser)){
		scale -= 2;
		if(scale<0){scale=0;}
		ObjLaser_SetRenderWidth(laser, scale);
		
		wait(1);
	}

}


let lv = 4;

task createlaser2(x, y, s, angle1, l, w, graphic, delay, f, angle2, v, n){
	l = 30;
	w = 30;
	s = 12;
	f = 19;
	let obj = CreateCurveLaserA1(x, y, s, angle1, l, w, graphic, delay);
	enddelete(obj);
	ObjCrLaser_SetTipDecrement(obj, 0);
	ObjMove_AddPatternA2(obj, 0, s, angle1, 0, v, s);
	ObjMove_AddPatternA2(obj, f, s, angle2, 0, -v, s);
	ObjMove_AddPatternA2(obj, f*2, s, angle1, 0, v, s);
	wait(95);
	Obj_Delete(obj);
}