#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell6"]
#Text["&quot;Prey Trapped on The Spider's Web&quot;"]


let objBoss;

let BossX;
let BossY;

let AttackX;
let AttackY;

let wall;
let Delete=0;
let count = 0;
let pointer;
let movspd = 0;
let finished = 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(90);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
	case(EV_PLAYER_REBIRTH){
		SetCommonData("Stacks",0);
	}
	case(EV_REQUEST_IS_DURABLE_SPELL){
		SetScriptResult(true);
	}
} 

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

@Finalize {

} 

task mainTask { 
Diseasecontroller;
wait(30);
SpellCard("&quot;Prey Trapped on The Spider's Web&quot;");

	attack=true;
	WebSling;
	wait(160);
	attack=false;
	wait(60);
	loop(5){
	Suck(30);
	wait(50);
	}
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/2, 5, 4);
	loop(2){
	Suck(30);
	wait(50);
	}
	SetForbidPlayerShot(true);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth+100,0, 5, 4);
	Trapped;
	wait(60);
	
wait(10);
movspd = 15;
let webrad = 37;
let points = 12;
Shooting;
ascent(j in 1..3){
		ascent(i in 0..points){
		SpiderWebStrand(j*webrad, i*360/points);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2+(300-(j*webrad))*cos(i*360/points),GetStgFrameHeight/2+(300-(j*webrad))*sin(i*360/points),movspd);
		wait(movspd);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2+(300-(j*webrad-webrad))*cos(i*360/points+360/points),GetStgFrameHeight/2+(300-(j*webrad-webrad))*sin(i*360/points+360/points),movspd);
		wait(movspd);
		}
		ascent(i in 0..points+1){
		SpiderWebStrand(-j*webrad, i*360/points);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2+(300-(j*webrad))*cos(i*360/points),GetStgFrameHeight/2+(300-(j*webrad))*sin(i*360/points),movspd);
		wait(movspd);
		}
	}
ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth,GetStgFrameHeight/2, 5, 4);
wait(60);
ascent(j in 3..5){
		ascent(i in 0..points){
		Blast;
		SpiderWebStrand(j*webrad, i*360/points);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2+(300-(j*webrad))*cos(i*360/points),GetStgFrameHeight/2+(300-(j*webrad))*sin(i*360/points),movspd);
		wait(movspd);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2+(300-(j*webrad-webrad))*cos(i*360/points+360/points),GetStgFrameHeight/2+(300-(j*webrad-webrad))*sin(i*360/points+360/points),movspd);
		wait(movspd);
		}
		ascent(i in 0..points+1){
		SpiderWebStrand(-j*webrad, i*360/points);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2+(300-(j*webrad))*cos(i*360/points),GetStgFrameHeight/2+(300-(j*webrad))*sin(i*360/points),movspd);
		wait(movspd);
		}
	}
wait(60);
Shooting2;
ascent(j in 5..7){
		ascent(i in 0..points){
		Blast;
		SpiderWebStrand(j*webrad, i*360/points);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2+(300-(j*webrad))*cos(i*360/points),GetStgFrameHeight/2+(300-(j*webrad))*sin(i*360/points),movspd);
		wait(movspd);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2+(300-(j*webrad-webrad))*cos(i*360/points+360/points),GetStgFrameHeight/2+(300-(j*webrad-webrad))*sin(i*360/points+360/points),movspd);
		wait(movspd);
		}
		ascent(i in 0..points+1){
		SpiderWebStrand(-j*webrad, i*360/points);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2+(300-(j*webrad))*cos(i*360/points),GetStgFrameHeight/2+(300-(j*webrad))*sin(i*360/points),movspd);
		wait(movspd);
		}
	}
ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/4, 5, 4);
finished++;
wait(60);
ShootingFinal;
ShootingFinal2;
}

task Blast{
CreateShotA2(BossX, BossY, 0.1, angleToObject(pointer, objBoss)+180,0.007, 20, LARGE_RED, 10);
SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 60);
}

task ShootingFinal{
	loop{
	ascent(i in 0..8){
	CreateShotA2(BossX, BossY, 0.1, (i*360/8) ,0.007, 20, LARGE_RED, 10);
	}
	ascent(j in 0..5){
	CreateShotA1(BossX, BossY, 1+(j/12), GetAngleToPlayer(objBoss), SMALL_RED, 5);
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 60);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spirit.wav", 60);
	wait(80);
	}
}

task ShootingFinal2{
let counter = 0;
	let random = rand(0,360);
	loop{
	ascent(i in 0..12){
	CreateShotA1(BossX, BossY, 1, 0+random+(i*360/12), ICE_BLUE, 5);
	}
	counter++;
	if(counter>12){
	random = rand(0,360);
	counter=0;
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 60);
	wait(3);
	}
}

task Shooting{
	loop{
		while(ObjMove_GetSpeed(objBoss)>0){
		ascent(i in 0..2){
		CreateShotA1(BossX, BossY, 1, ObjMove_GetAngle(objBoss)+90+(180*i), ICE_BLUE, 5);
		}
		if(finished==1){break;}
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 60);
		wait(4);
		}
	if(finished==1){break;}
	wait(2);
	}
}

task Shooting2{
let counter = 0;
	loop{
		while(ObjMove_GetSpeed(objBoss)>0){
		counter++;
		if(counter==5){
		ascent(i in 0..2){
		ascent(j in 0..5){
		CreateShotA1(BossX, BossY, 1+(j/6), ObjMove_GetAngle(objBoss)+90+(180*i), SMALL_RED, 5);
		}
		}
		counter=0;
		}
		if(finished==1){break;}
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spirit.wav", 60);
		wait(3);
		}
	if(finished==1){break;}
	wait(2);
	}
}

task SpiderWebStrand(rad, ang){
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);
ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
ObjCrLaser_SetTipDecrement(obj, 0);
ObjShot_SetAutoDelete(obj, false);
Web(obj,rad, ang);

	task Web(obj,rad, ang){
		loop(movspd+1){
		ObjStLaser_SetAngle(obj, angleToObject(obj, objBoss));
		ObjLaser_SetLength(obj, GetObjectDistance(obj,objBoss));
		yield;
		}
		/*while(GetCommonData("turn",0)==0){yield;}
		wait(rad);
		let angle = 0;
		let lasangle = ObjStLaser_GetAngle(obj);
		let curve = 0;
		loop{	
		ObjMove_SetPosition(obj, GetStgFrameWidth/2+(rad)*cos(angle+ang),GetStgFrameHeight/2+(rad)*sin(angle+ang));
		ObjStLaser_SetAngle(obj, lasangle+angle);
		angle+=0.05*curve;
		if(curve<10){curve+=0.05}
		yield;
		}*/
		loop{yield;}
		ObjShot_FadeDelete(obj);
	}
}

task Trapped{
	let v = 2;
    loop{
	let radius = GetObjectDistance(GetPlayerObjectID,pointer);
    let ang = atan2(GetStgFrameHeight/2-GetPlayerY,GetStgFrameWidth/2-GetPlayerX);
        if(GetPlayerX!=GetStgFrameWidth/2||GetPlayerY!=GetStgFrameHeight/2 && radius>100){
        ObjMove_SetPosition(GetPlayerObjectID, GetPlayerX + v * cos ( ang ) , GetPlayerY + v * sin ( ang ) );
        }
		if(radius<=100){v=2;}
		if(radius>110){v=3;}
		if(radius>120){v=4;}
		if(radius>130){v=5;}
		if(radius>140){v=6;}
		if(radius>150){v=7;}
    yield;
    }
}

task Suck(time){
	let v = 0;
    ascent(i in 0..time){
	
    let ang = atan2(GetStgFrameHeight/2-GetPlayerY,GetStgFrameWidth/2-GetPlayerX);
        if(GetPlayerX!=GetStgFrameWidth/2||GetPlayerY!=GetStgFrameHeight/2){
        ObjMove_SetPosition(GetPlayerObjectID, GetPlayerX + v * cos ( ang ) , GetPlayerY + v * sin ( ang ) );
        }
    v+=0.3;
    yield;
    }
}

task WebSling{
wall = 0;
let angle = 180;
let speed = 5;
let counter = 0;
let obj = CreateStraightLaserA1(AttackX,AttackY, 0, 0, 15, 1337000000, C_LASER_GRAY, 0);
ObjShot_SetAutoDelete(obj, false);
SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/laserlen.wav", 60);
Web(obj);
Movement(obj);


	task Web(obj){
	let beep = 0;
		while(!Obj_IsDeleted(obj)){
		ObjStLaser_SetAngle(obj, angleToObject(obj, objBoss));
		ObjLaser_SetLength(obj, GetObjectDistance(obj,objBoss));
		beep++;
		if(beep>500){break;}
		yield;
		}
		pointer = CreateShotA1(BossX, BossY, 0, 0, 0, 0);
		Obj_SetVisible(pointer, 0);
		ObjShot_SetIntersectionEnable(pointer, 0);
		while(!Obj_IsDeleted(obj)){
		ObjStLaser_SetAngle(obj, angleToObject(obj, pointer));
		ObjLaser_SetLength(obj, GetObjectDistance(obj,pointer));
		yield;
		}
	}
	
	task Movement(obj){
	ObjMove_SetAngle(obj, angle);
	ObjMove_SetSpeed(obj, 0.1);
	ObjMove_SetAcceleration(obj, 0.07);
	ObjMove_SetMaxSpeed(obj, 125);
	ObjMove_SetAngularVelocity(obj, 12);
	wait(180);
	ObjMove_SetSpeed(obj, ObjMove_GetSpeed(obj)/2);
	loop(120){
	ObjShot_SetIntersectionEnable(obj, 0);
	ObjMove_SetAngle(obj, GetAngleToPlayer(obj));
	ObjMove_SetAcceleration(obj, 0);
	ObjMove_SetAngularVelocity(obj, 0);
	yield;
	}
	ObjMove_SetPosition(obj, GetPlayerX, GetPlayerY);
	loop{
	ObjMove_SetPosition(obj, GetPlayerX, GetPlayerY);
	yield;
	}
	}
}

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