#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["JunkoSpell7"]
#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(1500);
    }
    case(EV_REQUEST_TIMER){ //Sets attack time
        SetScriptResult(99);
    }
    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
	 //moves boss to destination
	 ObjMove_SetDestAtFrame(objBoss, GetCenterX(), GetCenterY(), 1);
 //declares spellcard
 	changedsfx;
	TDrawLoop; //calls TDrawLoop task
	LSFinalize; //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 {
	wait(70);
	chargeeffect2;
	wait(30);
	chargeeffect;
	wait(50);
	toggleaura(1);
	ObjMove_SetDestAtFrame(objBoss, GetCenterX(), 120, 40);
	healthknubs;
	spellcutin("");
	ObjEnemy_SetDamageRate(objBoss, 16, 0);
	lifecheck;
	wavechange;
	junkoattack;
	fireA;
	while(hp < 3){wait(2);}
	fireB;
	ObjEnemy_SetDamageRate(objBoss, 8, 0);
	while(hp < 5){wait(2);}
	fireC;
	ObjEnemy_SetDamageRate(objBoss, 9, 0);
}

task wavechange{
	let v = 60;
	wait(600);
	if(hp<2){hp = 2;}
	wait(600);
	if(hp<3){hp = 3; SE_Play(shot2,v); DeleteShotAll(TYPE_ALL, TYPE_FADE);}
	wait(600);
	if(hp<4){hp = 4;}
	wait(600);
	if(hp<5){hp = 5; SE_Play(shot2,v); DeleteShotAll(TYPE_ALL, TYPE_FADE);}
	return;
}

task lifecheck{
	let v = 60;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 1250){wait(2);}
	if(hp<2){hp = 2;}
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 1000){wait(2);}
	if(hp<3){hp = 3; SE_Play(shot2,v); DeleteShotAll(TYPE_ALL, TYPE_FADE);}
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 750){wait(2);}
	if(hp<4){hp = 4;}
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 500){wait(2);}
	if(hp<5){hp = 5; SE_Play(shot2,v); DeleteShotAll(TYPE_ALL, TYPE_FADE);}
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){wait(2);}
	hp+=1;
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	return;
}

let hp = 1;
let acc = 0.1;

task fireA {
	let posneg = 1;
	let n = 10;
	let n2 = 10;
	let ap = 0;
	let angle = 180;
	let ss = 4;
	let ms = 2;
	let f = 50;
	let graphic = 539;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		loop(n2){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
			loop(n){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
				let hpc = hp;
				if(hp > 4){graphic = 89;}
				let b1 = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), ss, angle+ap, -acc, 0, graphic, 0);
				if(hp > 1){ObjRender_SetBlendType(b1, BLEND_ALPHA);}
				bcommand(b1,acc,ms,f,hpc);
				if(ap != 0){let b2 = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), ss, angle-ap, -acc, 0, graphic, 0);
					if(hp > 1){ObjRender_SetBlendType(b2, BLEND_ALPHA);}
					bcommand(b2,acc,ms,f,hpc);
				}
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(kira3,50);}
				angle+= 360/n;
				}
			}
			wait(10);
			ap += 360/n/n2 *posneg;
		}
		ap = 0;
		posneg *= -1;
		angle += 1;
	}
}
task fireB{
	let posneg = 1;
	let n = 35;
	let n2 = 2;
	let ap = 0;
	let angle = 90;
	let ss = 4.5;
	let ms = 3;
	let graphic = 309;
	let f = 60;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		loop(n2){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
			loop(n){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
				let hpc = hp;
				if(hp > 4){graphic = 308;}
				let b1 = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), ss, angle+ap, -acc, 0, graphic, 0);
				if(hp < 4){ObjRender_SetBlendType(b1, BLEND_ADD_ARGB);}
				bcommand(b1,acc,ms,f,hpc);
				Obj_SetRenderPriorityI(b1, Obj_GetRenderPriorityI(b1)+1);
				angle+= 360/n;
				}
			}
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(shot1,35);}
			wait(35);
			ap += 360/n/2;
		}
		ap = 0;
		posneg *= -1;
		angle -= 2;
	}
}

task fireC{
	let n = 20;
	let angle = 90;
	let d = 60;
	let f = 150;
	let w = 27;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		loop(2){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){return;}
			loop(n){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
				let hpc = hp;
				let laser = CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), angle, 512, w, f, 1001, d);
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(laser1,60);}
				laserdelete(laser,hpc);
				angle += 360/n;
				Obj_SetRenderPriorityI(laser, Obj_GetRenderPriorityI(laser)-1);
				shrinklaser(laser,d+f,w);
				}
			}
			angle += 360/n/2;
			wait(d);
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(laser1,60);}
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){SE_Play(change,80);}
			wait(f-30);
		}
	}

}

task bcommand(b,acc,ms,f,hpc){
	ObjMove_AddPatternA2(b, f, NO_CHANGE, NO_CHANGE, 0.1, 0, ms);
	let an = 20;
	let am =  0;
	/*if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){Obj_Delete(b);}
	if(hpc > 5){hpc = 5;}
	if(hpc == 1 || hpc == 2){
		while(hp < 3 && !Obj_IsDeleted(b)){wait(3);}
		ObjShot_SetIntersectionEnable(b, false);
		ObjMove_AddPatternA2(b, 0, 0, 0, 0, 0, 0);
		loop(an){
			ObjRender_SetAlpha(b, 255-am);
			am += 255/an;
			wait(1);
		}
		Obj_Delete(b);
	}
	if(hpc == 3 || hpc == 4){
		while(hp < 5 && !Obj_IsDeleted(b)){wait(3);}
		ObjShot_SetIntersectionEnable(b, false);
		ObjMove_AddPatternA2(b, 0, 0, 0, 0, 0, 0);
		loop(an){
			ObjRender_SetAlpha(b, 255-am);
			am += 255/an;
			wait(1);
		}
		Obj_Delete(b);
	}
	if(hpc > 4){
		while(hp < 6 && !Obj_IsDeleted(b)){wait(3);}
		ObjShot_SetIntersectionEnable(b, false);
		ObjMove_AddPatternA2(b, 0, 0, 0, 0, 0, 0);
		loop(an*2){
			ObjRender_SetAlpha(b, 255-am);
			am += 255/an;
			wait(1);
		}
		Obj_Delete(b);
	}*/
	return;
}

task laserdelete(b,hpc){
	let an = 20;
	let am =  0;
	/*if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){Obj_Delete(b);}
	if(hpc > 5){hpc = 5;}
	if(hpc > 4){
		while(hp < 6 && !Obj_IsDeleted(b)){wait(3);}
		ObjShot_SetIntersectionEnable(b, false);
		ObjMove_AddPatternA2(b, 0, 0, 0, 0, 0, 0);
		loop(an*2){
			ObjRender_SetAlpha(b, 255-am);
			am += 255/an;
			wait(1);
		}
		Obj_Delete(b);
	}*/
	return;
}

task shrinklaser(laser,d,w){
	wait(d);
	let scale = w;
	while(!Obj_IsDeleted(laser)){
		scale -= 2;
		if(scale<0){scale=0;}
		ObjLaser_SetRenderWidth(laser, scale);
		wait(1);
	}
	return;
}

task healthknubs{
	let path = GetCurrentScriptDirectory ~ "img/lifebar.png";
	let angle = -90;
	let angle2 =  300;
	let pn = 1;
	loop(2){
		angle+= 360/3;
		pn +=2;
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		Obj_SetRenderPriority(obj, 0.69);
		ObjPrim_SetTexture(obj, path);
		ObjSprite2D_SetSourceRect(obj, 23, 0, 31, 11);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetScaleXYZ(obj, 0.5, 0.8, 0.8);
		ObjRender_SetAngleZ(obj, angle2);
		knubcommand(obj,pn,angle);
		angle2 = 230;

	}
}

task knubcommand(obj,pn,angle){
	let cosin = 59;
	while(hp < pn){
		ObjRender_SetPosition(obj, ObjMove_GetX(objBoss) + cosin * cos(angle), ObjMove_GetY(objBoss) + cosin * sin(angle), NO_CHANGE);
		wait(1);
	}
	Obj_Delete(obj);
}