#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell3"]
#Text["Cave Spider &quot;Acrobatic Web Sling&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);
	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(75);
	}
	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("Cave Spider &quot;Acrobatic Web Sling&quot;");
ObjEnemy_SetDamageRate(objBoss, 100, 100);
	loop{
	SetCommonData("Flinged",0);
	attack=true;
	wait(20);
	WebElevator;
	wait(60);
	SetCommonData("BossAngle",180);
	attack=false;
	WebSlinger;
	WebSlinger;
	WebSlinger;
	SetCommonData("BossAngle",0);
	ObjMove_SetDestAtWeight(objBoss, BossX, BossY, 0, 0);
	ObjMove_SetPosition(objBoss, GetPlayerX, -100);
	wait(60);
		loop(4){
		WebSling;
			loop{
			if(GetCommonData("Flung",false)==true){yield; SetCommonData("Flung",false); break;}
			yield;
			}
		wait(60);
		}
	wait(40);
	Delete=1; wait(1); Delete=0;
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/4+50, 7, 4);
	wait(130);
	}
}

function WebElevator{
let obj = CreateStraightLaserA1(AttackX,AttackY, 270, 0, 15, 3600, C_LASER_GRAY, 0);
SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/laserlen.wav", 60);
	ascent(i in 0..60){
	ObjLaser_SetLength(obj, i*3);
	ObjMove_SetPosition(obj, AttackX-2, AttackY-5);
	yield;
	}
	ascent(i in 0..100){
	ObjMove_SetPosition(obj, AttackX-2, AttackY-5);
	ObjMove_SetPosition(objBoss,BossX, BossY-i);
	if(ObjMove_GetY(objBoss)<-100){break;}
	yield;
	}
	Obj_Delete(obj);
}

function WebSlinger{
ObjMove_SetPosition(objBoss, GetPlayerX, -100);
let obj = CreateStraightLaserA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 0, 0, 15, 3600, C_LASER_GRAY, 0);
	ObjShot_SetAutoDelete(obj, false);
	ObjMove_SetAngle(objBoss, 270);
	ObjMove_SetSpeed(objBoss, -7);
	ObjMove_SetAcceleration(objBoss, 0.05);
	ObjMove_SetMaxSpeed(objBoss, 5);
	let count = 0;
	let angle = 0;
	loop(320){
	ObjStLaser_SetAngle(obj, angleToObject(obj, objBoss));
	ObjLaser_SetLength(obj, GetObjectDistance(obj,objBoss));
	
	
	let random = truncate(rand(0,6));
		if(count==2&&ObjMove_GetSpeed(objBoss)<1){
		let a = CreateShotA2(BossX, BossY, 0.1, 0+angle, 0.01, 5, ICE_RED, 2);
		let b = CreateShotA2(BossX, BossY, 0.1, 180-angle, 0.01, 5, ICE_BLUE, 2);
			if(random>3){
			Disease(a, 1.2, 24); 
			Disease(b, 1.2, 24); 
			}
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 60);
		count=0;
		}
		if(count==8&&ObjMove_GetSpeed(objBoss)>1){
			ascent(i in 0..12){
			random = truncate(rand(0,6));
			let a = CreateShotA2(BossX, BossY, 0, 90-(12*6)+(i*12)+random, 0.02, 2, ICE_PURPLE, 2);
			if(random>4){
			Disease(a, 1.2, 24); 
			}
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 60);
			}
		count=0;
		}
		
	angle+=9;
	count++;
	yield;
	}
	Obj_Delete(obj);
}

task WebSling{
wall = 0;
let angle = GetAngleToPlayer(objBoss);
let speed = 5;
let counter = 0;
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);
RelativeVelocity(obj,speed*cos(angle),speed*sin(angle),0,0, true);	

	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){
		SetCommonData("Flung",true);
		wall = 3;
		WebSmash(tar);
		break; 
		}
		if(trunc(ObjMove_GetX(tar))<0){
		SetCommonData("Flung",true);
		wall = 1;
		WebSmash(tar);
		break; 
		}
        if(trunc(ObjMove_GetY(tar))>GetStgFrameHeight){
		SetCommonData("Flung",true);
		wall = 2;
		WebSmash(tar);
		break; 
		}
		if(trunc(ObjMove_GetY(tar))<0 && GetCommonData("Flinged",0)==1){
		SetCommonData("Flung",true);
		wall = 4;
		WebSmash(tar);
		break; 
		}
	yield;
	}
	SetCommonData("Flinged",1);
	if(wall == 1){ObjMove_SetDestAtWeight(objBoss, ObjMove_GetX(tar)+40, ObjMove_GetY(tar), 2, 10);}
	if(wall == 2){ObjMove_SetDestAtWeight(objBoss, ObjMove_GetX(tar), ObjMove_GetY(tar)-40, 2, 10);}
	if(wall == 3){ObjMove_SetDestAtWeight(objBoss, ObjMove_GetX(tar)-40, ObjMove_GetY(tar), 2, 10);}
	if(wall == 4){ObjMove_SetDestAtWeight(objBoss, ObjMove_GetX(tar), ObjMove_GetY(tar)+40, 2, 10);}
}

task WebSmash(obj){
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 60);
	ascent(i in 0..64){
	let random = rand(0,6);
	let obj2 = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(1,1.4), 0+(i*360/64), ICE_GRAY, 0);
	if(random>5){Disease(obj2, 1.2, 24); }
	}
}


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());
} 
