#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell4"]
#Text["Miasma &quot;Contained Illness&quot;"]


let objBoss;

let BossX;
let BossY;

let AttackX;
let AttackY;

let wall;
let Delete=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;

} 

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(4500);
     	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(60);
	}
	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 mainTask { 
Diseasecontroller;
wait(30);
SpellCard("Miasma &quot;Contained Illness&quot;");
	loop{
ObjEnemy_SetDamageRate(objBoss, 100, 100);
	WebSling;
	wait(40);
	attack=true;
	wait(20);
	DiseaseShot;
	DiseaseShot2;
	wait(20);
	attack=false;
	wait(10);
	ObjMove_SetDestAtWeight(objBoss,GetPlayerX,GetStgFrameHeight/3+rand(-50,5), 15, 4);
	wait(60);
	}
}

task DiseaseShot{
	ascent(j in 0..8){
		ascent(i in 0..5){
		let obj = CreateShotA1(AttackX, AttackY, 1, 0+(i*360/5)+(j*45), ORB_PURPLE, 5);
		Disease(obj, 3, 60);
		ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
		}
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spirit.wav", 60);
		wait(50);
	}
	wait(120);
}

function DiseaseShot2{
	ascent(j in 0..6){
		ascent(i in 0..118){
		let obj = CreateShotA1(AttackX, AttackY, rand(1, 1.1), 0+(i*360/118)+(j*360/24), ICE_PURPLE, 5);
		ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
		}
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 60);
		wait(70);
	}
	wait(120);
}

task WebSling{
wall = 0;
let angle = GetAngleToPlayer(objBoss)+45;
let speed = 5;
let counter = 0;
loop(12){
let obj = CreateStraightLaserA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 0, 0, 15, 3600, C_LASER_GRAY, 0);
ObjShot_SetAutoDelete(obj, false);
SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/laserlen.wav", 60);
Web(obj);
Disease(obj, 1.75, 30);
RelativeVelocity(obj,speed*cos(angle),speed*sin(angle),0,0, true);	
angle+=360/12;
}

	task Web(obj){
		while(!Obj_IsDeleted(obj)){
		ObjStLaser_SetAngle(obj, angleToObject(obj, objBoss));
		ObjLaser_SetLength(obj, GetObjectDistance(obj,objBoss));
		if(Delete==1){break;}
		yield;
		}
		ObjShot_FadeDelete(obj);
	}
}

task RelativeVelocity(tar,x,y,xscaling,yscaling,stopping){
	let defaultx = x;
	let defaulty = y;
	while(!Obj_IsDeleted(tar)){
	let posy = ObjMove_GetY(tar);
	let posx = ObjMove_GetX(tar);
    ObjMove_SetX(tar, posx+x);
	ObjMove_SetY(tar, posy+y);
	x+=xscaling;
	y+=yscaling;
		if(trunc(ObjMove_GetX(tar))>GetStgFrameWidth){
		break; 
		}
		if(trunc(ObjMove_GetX(tar))<0){
		break; 
		}
        if(trunc(ObjMove_GetY(tar))>GetStgFrameHeight){
		break; 
		}
		if(trunc(ObjMove_GetY(tar))<0){
		break; 
		}
	yield;
	}
	wait(460);
	ObjShot_FadeDelete(tar);
}

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

function SpellCard(name){
	let Cutin = GetCurrentScriptDirectory ~ "Functions/img/Emotions/Yam1.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;}
    Obj_Delete(objBoss);
	wait(1);
	SetCommonData("deathsound",1);
	GetCommonData("LeftoverStacks",GetCommonData("Stacks",0));
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
} 
