#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell7"]
#Text["&quot;Tsuchigumo's Pandemic&quot;"]


let objBoss;

let BossX;
let BossY;

let AttackX;
let AttackY;

let wall;
let Delete=0;
let count = 0;
let movspd = 1;

let stage = 0;
let stage2 = 0;

#include "./Functions/Index.txt"


@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	renderyamame(objBoss);
	BossShotSound(objBoss, 100, 600);
	FlameAura(objBoss,[255,25,25],2);
	BottomEnemyMarker(objBoss, objBoss);
	MagicRing(objBoss,false);
	DiseaseBoss(objBoss, 4, 70);
	DiseaseBoss(objBoss, 6, 120);
	DrawName("Yamame Kurodani", [200, 200, 20], 11);
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/4+50, 5, 4);
	mainTask;
	end;

	SetForbidPlayerShot(false);
} 

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(10000);
     	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(80000000);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
	case(EV_PLAYER_REBIRTH){
		SetCommonData("Stacks",0);
	}
} 

@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	
	AttackX = BossX+10;
	AttackY = BossY-20;
	
	ObjEnemy_SetIntersectionCircleToShot(objBoss,BossX,BossY,21);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,BossX,BossY,16);
	yield;
} 

@Finalize {

} 

task Staging{
let HPTreshold = 8000;
wait(90);
ObjEnemy_SetDamageRate(objBoss, 80, 40);
	loop{
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < HPTreshold){
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death2.wav", 100);
		HPTreshold-=2000;
		DeleteShotAll(TYPE_SHOT, TYPE_FADE);
		stage++;
		}
	yield;
	}
}

task mainTask { 
Diseasecontroller;
Staging;
wait(50);
SpellCard("&quot;Tsuchigumo's Pandemic&quot;");
Web;
}

function angleToObject(obj,obj2) {
 let dir = atan2(ObjMove_GetY(obj2)-ObjMove_GetY(obj),ObjMove_GetX(obj2)-ObjMove_GetX(obj));
 return dir;
}

task Web{
wait(10);
movspd = 5;
ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2,GetStgFrameHeight/4,5);
wait(5);
let webrad = 40;
let points = 14;
loop{
ascent(j in count+1..count+2){
		ascent(i in 0..points){
		WebSlingVertical(count, i*360/points);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2+(j*webrad)*cos(i*360/points),GetStgFrameHeight/4+(j*webrad)*sin(i*360/points),movspd);
		wait(movspd);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2+(j*webrad-webrad)*cos(i*360/points+360/points),GetStgFrameHeight/4+(j*webrad-webrad)*sin(i*360/points+360/points),movspd);
		wait(movspd);
		}
		ascent(i in 0..points+1){
		WebSlingHorizontal(count, i*360/points);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2+(j*webrad)*cos(i*360/points),GetStgFrameHeight/4+(j*webrad)*sin(i*360/points),movspd);
		wait(movspd);
		}
	}
	ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2,GetStgFrameHeight/4,5);
	wait(20);
	ShootNormally(stage);
	ObjEnemy_SetDamageRate(objBoss, 90, 9);
	while(stage2==stage){
	yield;
	}
	stage2++;
	count++;
	ConcentrationA1(objBoss,"Nothing",40,2.2,120,[255, 122, 255]);
	wait(90);
	DiseaseBoss(objBoss, 4+(stage*2), 70);
	ExplosionA2(objBoss,"CHERRY",[255, 122, 255],[255, 122, 255]);
	wait(60);
}
SetCommonData("turn",1);
}

task ShootNormally(stg){
	while(stg==stage){
	
		if(stg==0){
			ascent(i in 0..10){
			let obj = CreateShotA1(BossX, BossY, 2+(i/4), GetAngleToPlayer(objBoss), SMALL_PURPLE, 5);
			Disease(obj, 1, 20);
			}
		}
		
		if(stg==1){
		ascent(j in 0..3){
			ascent(i in 0..10){
			let obj = CreateShotA1(BossX, BossY, 2+(i/4), GetAngleToPlayer(objBoss)-(5)+(j*5), SMALL_PURPLE, 5);
			Disease(obj, 1, 20);
			}
		}
		}
		
		if(stg==2){
		ascent(j in 0..3){
			ascent(i in 0..10){
			let obj = CreateShotA1(BossX, BossY, 2+(i/4), GetAngleToPlayer(objBoss)-(5)+(j*5), LARGE_PURPLE, 5);
			Disease(obj, 1.75, 30);
			}
		}
		}

		if(stg>=3){
		ascent(j in 0..3){
			ascent(i in 0..10){
			let obj = CreateShotA1(BossX, BossY, 2+(i/4), GetAngleToPlayer(objBoss)-(5)+(j*5), ORB_PURPLE, 5);
			ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
			Disease(obj, 3, 60);
			}
		}
		}
SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spirit.wav", 60);
	wait(120);
	}
}

task WebSlingVertical(offset, ang){
let angle = GetAngleToPlayer(objBoss);
let speed = 5;
let counter = 0;
let obj = CreateStraightLaserA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 0, 0, 15, 1337000000, C_LASER_GRAY, 0);
ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
ObjCrLaser_SetTipDecrement(obj, 0);
ObjShot_SetAutoDelete(obj, false);
Web(obj, ang);
wait(30);
//Shooting(obj, offset);

	task Web(obj, ang){
		loop(movspd+1){
		ObjStLaser_SetAngle(obj, angleToObject(obj, objBoss));
		ObjLaser_SetLength(obj, GetObjectDistance(obj,objBoss));
		yield;
		}
		loop{yield;}
		ObjShot_FadeDelete(obj);
	}
	
	task Shooting(obj, offset){
	let boop = 0;
	offset++;
		while(!Obj_IsDeleted(obj)){
		let las = CreateLooseLaserA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 0, ObjStLaser_GetAngle(obj)+boop, 75, 15, L_LASER_GRAY, 5);
		ObjMove_SetAcceleration(las, 0.02); ObjMove_SetMaxSpeed(las, 5);
		wait(120-(20*offset));
		boop+=10*offset;
		}
	}
}

task WebSlingHorizontal(offset, ang){
let angle = GetAngleToPlayer(objBoss);
let speed = 5;
let counter = 0;
let obj = CreateStraightLaserA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 0, 0, 15, 1337000000, C_LASER_GRAY, 0);
ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
ObjCrLaser_SetTipDecrement(obj, 0);
ObjShot_SetAutoDelete(obj, false);
Web(obj, ang);
wait(30);
Shooting(obj, offset);

	task Web(obj, ang){
		loop(movspd+1){
		ObjStLaser_SetAngle(obj, angleToObject(obj, objBoss));
		ObjLaser_SetLength(obj, GetObjectDistance(obj,objBoss));
		yield;
		}
		loop{yield;}
		ObjShot_FadeDelete(obj);
	}
	task Shooting(obj, offset){
	let boop = 0;
	offset++;
		while(!Obj_IsDeleted(obj)){
		let x = ObjMove_GetX(obj);
		let y = ObjMove_GetY(obj);
		let angle = ObjStLaser_GetAngle(obj);
		let len = ObjLaser_GetLength(obj);
		ascent(i in 0..len/8){
		let obj2 = CreateShotA1(ObjMove_GetX(obj)+(i*8)*cos(angle),ObjMove_GetY(obj)+(i*8)*sin(angle),0,angle-90-boop,ICE_GRAY,5);
		ObjMove_AddPatternA3(obj2, 10, NO_CHANGE, NO_CHANGE, 0.3, 0, 100, NO_CHANGE);
		ObjMove_AddPatternA3(obj2, 20, NO_CHANGE, NO_CHANGE, -0.2, 0, 6-offset, NO_CHANGE);
		}
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 60);
		wait(60*offset);
		boop+=10*offset;
		}
	}
}

function SpellCard(name){
	let Cutin = GetCurrentScriptDirectory ~ "Functions/img/Emotions/Yam4.png";
	ObjCutin_SetSpellcardS3(name, 200, 200, 12);
	ObjCutin_LaunchS3("BYAKUREN", Cutin, "Normal");
	TCallBg2;
	bspell = true;
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	wait(210);
}

task end{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
   	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death.wav", 100);
	loop(11){
	SpellBurstA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),"Cherry",[255, 255, 255]);
	wait(7);
	}
	StopSlow(TARGET_ALL);
	wait(1);
	SetCommonData("BossX", ObjMove_GetX(objBoss));
	SetCommonData("BossY", ObjMove_GetY(objBoss));
    Obj_Delete(objBoss);
	SetCommonData("Died",1);
	wait(1);
	//SetCommonData("deathsound",1);
	GetCommonData("LeftoverStacks",GetCommonData("Stacks",0));
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
} 
