#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["HecatiaSpell4"]
#Text["By Lushie/Lusushime"]
#include ".\Functions.dnh"
#Background[".\Stage7bg.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);
    	if(pd == 0){
    		deletesprites;
    	}
    	
    }
}


let spelltime = 50;
let spelllife = 500;

task deletesprites{
	wait(60);
    ObjRender_SetAlpha(esprite1, 0);
    ObjRender_SetAlpha(esprite2, 0)
}
@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
	hecatiaform = 2;
	bossanimation("hecatia");

	TDrawLoop; //calls TDrawLoop task
	LSFinalize2;
	MainTask; //calls MainTask task
}

@MainLoop{
	ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 62); 
	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);
	
	let pdc = pdcount;
	pointdevicepositions;
	wait(130);
	move = 3;
	wait(30);
	if(pdc == pdcount){
		startattacking;
	}
}

let fn = 0;
// if(n2 != 1){angle2 -= ap/2*(n2-1);}
task startattacking{
	pointdevicepositions;
	pdcount2 = pdcount;
	let pdc = pdcount;
	bmoveto(GetCenterX, 120, 60);
	setauracolor("red");
	spellcutin("Authority \"Queen of All Hells\"", "hecatia");

	SetShotAutoDeleteClip(32, 32, 32+384-(rez*384), 32+448-(rez*448));
	
	let wt = 200;
	fn = 0;
	if(pdc == pdcount){
		spelldamage(2, 14, 40);
		fireA(pdc);
		wait(wt);
		if(lifecheck(pdc) == false){return;}
		SE_Play4(change, changev-5);
		enemy(esprite1, -dx, 1, pdc);
		fn += 1;
		fireB(pdc);
		wait(wt);
		if(lifecheck(pdc) == false){return;}
		SE_Play4(change, changev-5);
		enemy(esprite2, dx, 2, pdc);
		fn += 1;
		fireC(pdc);
	}
}
let dx = 100;
let dt = 70;

task fireA(pdc) {
	let n = 8;
	let s = 2;
	let acc = -0.04;
	let ms = -2;
 	let t = 3;

 	let bn = 8;
 	let ln = bn+1;
 	let ap = 2.6;
 	let ap2 = 360/n*bn/4;

	let graphic = 1066;
	let angle = 270;
	let c = 0;
	if(diff == 1){
		bn = 8;
		n = 7;
		ln = bn+2;
		ap2 = 360/n*bn/4;
		t = 4;
	}
	if(diff == 2){
		bn = 8;
		n = 7;
		ln = bn+1;
		ap2 = 360/n*bn/3.9;
		t = 3;
	}
	if(diff == 3){
		bn = 8;
		n = 8;
		ln = bn+1;
		ap2 = 360/n*bn/4;
		t = 3;
	}
	while(lifecheck(pdc) == true){
		if(lifecheck(pdc) == false){return;}
		if(c < bn){
			loop(n){
				let bullet = CreateShotA2(BossX, BossY, s, angle, acc, ms, graphic, 0);
				ObjMove_SetAngularVelocity(bullet, -0.3);
				Obj_SetRenderPriorityI(bullet, Obj_GetRenderPriorityI(bullet)-1);
				ObjRender_SetBlendType(bullet, BLEND_ADD_ARGB);
				angle += 360/n;
			}
			
		}
		else if(c == ln){
			c = 0;
			angle += ap2;
		}
		c+= 1;
		angle += ap;
		SE_Play(shot1, shot1v);
		wait(t);
	}
}

task fireB(pdc){
	let n = 20;
	let posneg = 1;
	let ll = 600;
	let w = 18;
	let dt = 50;
	let lt = 40;
	let st = 20;
	let t = 120+dt+lt;
	if(diff == 1){
		dt = 60;
		lt = 35;
		n = 15;
	}
	if(diff == 2){
		dt = 50;
		lt = 40;
		n = 18;
	}
	if(diff == 3){
		dt = 50;
		lt = 45;
		n = 22;
	}

	let bullet = CreateShotA1(192-dx, BossY, 0, 0, 130, 0);
	Obj_SetVisible(bullet, false);
	ObjShot_SetSpellResist(bullet, true);
	wait(dt);
	if(lifecheck(pdc) == false){return;}
	chargeeffect3("blue", 192-dx, BossY);
	wait(30);
	if(lifecheck(pdc) == false){return;}
	while(lifecheck(pdc) == true){
		if(lifecheck(pdc) == false){return;}
		let angle = GetAngleToPlayer(bullet) + 360/n/2*posneg;
		let array = [];
		loop(n){
			let laser = CreateStraightLaserA1(192-dx, BossY, angle, 0, w, lt, 7, dt);
			angle += 360/n;
			array = array ~ [laser];
		}
		SE_Play2(laser1, laser1v);
		laserspawn(array);


		if(posneg == 0){posneg = 1;}
		else{posneg = 0;}
		wait(t);

		task laserspawn(array){
			let l = length(array);
			let cl = 0;
			loop(st){
				if(lifecheck(pdc) == false){return;}
				cl += ll/st;
				ascent(i in 0..l){
					ObjLaser_SetLength(array[i], cl);
				}
				yield;
			}
			ascent(i in 0..l){
				ObjLaser_SetLength(array[i], ll);
			}
			SE_Play2(laser1, laser1v);
			wait(lt+dt-st);
			if(lifecheck(pdc) == false){return;}
			let scale = w;
			let sm = w/st;
			let alpha = 255;
			loop(st){
				scale -= sm;
				alpha -= 255/st;
				ascent(i in 0..l){
					ObjLaser_SetRenderWidth(array[i], scale);
					ObjRender_SetAlpha(array[i], alpha);
				}
				yield;
			}
			ascent(i in 0..l){
				Obj_Delete(array[i]);
			}
		}
	}
}

task fireC(pdc){
	let x = 192 + dx;
	let n = 9;
	let ap = 360/n/4.5;
	let s = 1;
	let t = 8;
	let d = 30;
	let angle = rand(0,360);
	wait(dt);
	if(lifecheck(pdc) == false){return;}
	chargeeffect3("yellow", x, BossY);
	wait(30);
	if(lifecheck(pdc) == false){return;}
	if(lifecheck(pdc) == false){return;}
	if(diff == 1){
		n = 7;
		t = 9;
	}
	if(diff == 2){
		n = 9;
		t = 7;
	}
	if(diff == 3){
		n = 11;
		t = 7;
	}
	while(lifecheck(pdc) == true){
		let array = [];
		loop(n){
			let bullet = CreateShotA2(x + d * cos(angle), BossY + d*sin(angle), s, angle, 0, 0, 230, 0);
			array = array ~ [bullet];
			ObjMove_SetAngularVelocity(bullet, 0);
			Obj_SetRenderPriorityI(bullet, Obj_GetRenderPriorityI(bullet)-2);
			angle += 360/n;
		}
		spawncustomarray2(array, 1, 1.5, 70, 255, 12, 0);
		angle -= ap;
		wait(t);
	}
}

let esprite1 = ObjPrim_Create(OBJ_SPRITE_2D);
let esprite2 = ObjPrim_Create(OBJ_SPRITE_2D);
task enemy(esprite, dx, form, pdc){
			
	let xp = 64;
	let yp = 96;

	let c = 8;
	let n = 4;
	
	if(form == 2){
		c = 4;
	}
	let x = n * xp;
	let x2 = x + xp + 1;
	let y = c*yp;
	let y2 = y + yp+1;
	
	let img = GetCurrentScriptDirectory~"./img/boss7.png"; 
	ObjPrim_SetTexture(esprite, img); 
	Obj_SetRenderPriorityI(esprite, Obj_GetRenderPriorityI(objBoss)-1);
	ObjSprite2D_SetSourceRect(esprite, x, y, x2, y2);
	ObjSprite2D_SetDestCenter(esprite);
	ObjRender_SetPosition(esprite, BossX, BossY, 1);
	ObjRender_SetAlpha(esprite, 255);
	
	let dxp = dx/dt;
	let dcx = 192;
	loop(dt){
		if(lifecheck(pdc) == false){return;}
		dcx += dxp;
		ObjRender_SetX(esprite, dcx);
		ObjRender_SetY(esprite, BossY+floaty);
		ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjRender_GetX(esprite), ObjRender_GetY(esprite), 62); 
		ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjRender_GetX(esprite), ObjRender_GetY(esprite), 24);
		yield;
	}
	ObjRender_SetX(esprite, 192+dx);
	loop(4){
		if(lifecheck(pdc) == false){return;}
		x = n * xp;
		x2 = x + xp+1;
		ObjRender_SetY(esprite, BossY+floaty);
		ObjSprite2D_SetSourceRect(esprite, x, y, x2, y2);
		ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjRender_GetX(esprite), ObjRender_GetY(esprite), 62); 
		ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjRender_GetX(esprite), ObjRender_GetY(esprite), 24);
		n -= 1;
		wait(5);
	}
	if(lifecheck(pdc) == false){return;}
	c = 6;
	if(form == 2){c = 3;}
	n = 1;
	y = c*yp;
	y2 = y + yp+1;
	let posneg = 1;
	if(form == 1){
		bossaura2(esprite, 40, 40, 255, 255);
	}
	else{
		bossaura2(esprite, 255, 200, 20, 205);
	}
	while(lifecheck(pdc) == true){
		x = n * xp;
		x2 = x + xp + 1;
		ObjRender_SetY(esprite, BossY+floaty);
		ObjSprite2D_SetSourceRect(esprite, x, y, x2, y2);
		ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjRender_GetX(esprite), ObjRender_GetY(esprite), 52); 
		ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjRender_GetX(esprite), ObjRender_GetY(esprite), 24);
		if(n == 0 && posneg == -1){posneg *= -1;}
		if(n == 6){posneg *= -1;}
		n += 1 * posneg;
		loop(6){if(lifecheck(pdc) == true){wait(1);}}
	}
	ObjRender_SetAlpha(esprite, 0);
}

task bossaura2(obj, r, g, b, salpha){
	let pdc = pdcount;
	let path = GetCurrentScriptDirectory~"./img/eff_aura.dds";
	while(lifecheck(pdc) == true){
		let aura3 = ObjPrim_Create(OBJ_SPRITE_2D);
		bossauradetail2(aura3,path);
		let angle = rand(0,360);
		ObjRender_SetAngleZ(aura3, angle);
		bossmoveaura2(aura3);
		wait(6);
	
		task bossmoveaura2(aura1){
			let n = 45;
			let alpha = salpha;
			let alpham = salpha/n;
			let scale = 0;
			let scalem = (3.5-scale)/n;
			ObjRender_SetPosition(aura1, ObjRender_GetX(obj), ObjRender_GetY(obj), 1);
			loop(n){
				if(Obj_IsDeleted(obj)){Obj_Delete(aura1); return;}
				//ObjRender_SetPosition(aura1, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)+floaty, 1);
				ObjRender_SetScaleXYZ(aura1, scale,scale, 1);
				ObjRender_SetAlpha(aura1, alpha);
				alpha -= alpham;
				scale += scalem;
				wait(1);
			}
			Obj_Delete(aura1);
		}

		task bossauradetail2(aura1,path){
			ObjRender_SetBlendType(aura1, BLEND_ADD_ARGB);
			ObjPrim_SetTexture(aura1, path);
			Obj_SetRenderPriorityI(aura1,29);
			ObjSprite2D_SetSourceRect(aura1, 56, 8, 90, 41);
			ObjSprite2D_SetDestCenter(aura1);
			ObjRender_SetColor(aura1, r, g, b);
			ObjRender_SetScaleXYZ(aura1, 0, 0, 1);
		}
	}
}

task chargeeffect3(color, x, y){
	if(lifecheck2 == true){
		chargeorb3(color, x, y);
		SE_Play(charge2,80);
	}
}

task chargeorb3(color, x, y){
	let r = 0;
	let b = 0;
	let g = 0;
	if(color == "red"){
		let c2 = 90;
		r = 235;
		b = c2;
		g = c2;
	}
	else if(color == "blue"){
		let c2 = 80;
		r = c2;
		b = 255;
		g = c2;
	}
	else if(color == "pink"){
		let c3 = 235;
		r = c3;
		b = c3;
		g = 50;
	}
	else if(color == "yellow"){
		let c3 = 155;
		r = c3;
		b = 30;
		g = c3;
	}
	let path = GetCurrentScriptDirectory~"./img/whitecircle.png";
	let charge = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(charge, BLEND_ADD_ARGB);
	ObjPrim_SetTexture(charge, path);
	ObjSprite2D_SetSourceRect(charge, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(charge);
	Obj_SetRenderPriorityI(charge,28);
	ObjRender_SetColor(charge, r, g, b);
	let n = 47;
	let alpha = 0;
	let endalpha = 180;
	let scale = 0.7;
	let am = (endalpha-alpha)/n * 2;
	let sm = scale/n;
	enddelete2(charge);
	chargeparticle3(x,y);
	loop(n){
		ObjRender_SetAlpha(charge,alpha);
		ObjRender_SetScaleXYZ(charge, scale, scale, 1);
		ObjRender_SetPosition(charge, x, y, 1);
		alpha += am;
		if(alpha > endalpha){alpha = endalpha;}
		scale -= sm;
		wait(1);
	}
	Obj_Delete(charge);
}

task chargeparticle3(x,y){
	let scale = 2.5;
	let angle = rand(0,360);
	let cosin = 150;
	let n = 3;
	let n2 = 7;
	loop(n2){
		loop(n){
			let path = GetCurrentScriptDirectory~"./img/maple.png";
			let leaf = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjRender_SetBlendType(leaf, BLEND_ADD_ARGB);
			ObjPrim_SetTexture(leaf, path);
			ObjSprite2D_SetSourceRect(leaf, 32, 0, 64, 32);
			ObjSprite2D_SetDestCenter(leaf);
			Obj_SetRenderPriorityI(leaf,24);
			ObjRender_SetAlpha(leaf, 120);
			ObjRender_SetScaleXYZ(leaf, scale, scale, 1);
			enddelete2(leaf);
			let angle2 = rand(0,360);
			ObjRender_SetAngleXYZ(leaf, angle2,angle2,angle2);
			ObjRender_SetPosition(leaf, x + cosin * cos(angle), y + cosin * sin(angle),1);
			moveparticle3(leaf,cosin,angle,angle2, x, y);
			angle += 360/n;
		}
		angle = rand(0,360);
		wait(4);
	}
}

task moveparticle3(leaf,cosin,angle,angle2, x, y){
	let n = 25;
	let cosinm = cosin/n;
	let sm = 2;
	loop(n){
		ObjRender_SetPosition(leaf, x + cosin * cos(angle), y + cosin * sin(angle),1);
		ObjRender_SetAngleXYZ(leaf, angle2,angle2,angle2);
		angle2 += sm;
		cosin -= cosinm;
		wait(1)
	}
	Obj_Delete(leaf);
}

task LSFinalize2{
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){ 
    	yield; 
    } 
    if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0) {
    	AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));
    	
    }
    move = 1;
    bulletclear(0);
    
    StartSlow(GetPlayerID(), 15);

    bmoveto(BossX-rand(30,40), BossY+rand(20,30), 120);
    SE_Play8(bossclear,60);
   
    wait(50);
    Obj_Delete(esprite1);
    Obj_Delete(esprite2);
    endparticle(BossX, BossY);
    Obj_Delete(objBoss);
    StopSlow(GetPlayerID());
    //shake(6, 60);
    
    SE_Play8(bossclear,40);
    Obj_Delete(bg1);
    Obj_Delete(bg2);
    Obj_Delete(bg3);
    Obj_Delete(bg4);
    wait(90);
    CloseScript(GetOwnScriptID()); 
    return;

}
