#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell5"]
#Text["Web Weaving &quot;String Shot&quot;"]


let objBoss;

let BossX;
let BossY;

let AttackX;
let AttackY;

let wall;
let Delete=0;
let counta = 0;
let Player = 0;

#include "./Functions/Index.txt"
#include "./Functions/Dialogue.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);
ObjEnemy_SetDamageRate(objBoss, 0, 0);
	DrawName("Yamame Kurodani", [200, 200, 20], 11);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/4+50, 5, 4);
	DialogueBoss;
	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 DialogueBoss{
Diseasecontroller;
Player = 0;
if(GetCommonData("Reimu",1)==1){Player = 0}
if(GetCommonData("Marisa",0)==1){Player = 1}
if(GetCommonData("Sakuya",0)==1){Player = 2}
if(GetCommonData("Sanae",0)==1){Player = 3}
if(GetCommonData("Youmu",0)==1){Player = 4}

	if(Player==0){
	DialogueReimu;
	}
	if(Player==1){
	DialogueMarisa;
	}
	if(Player==2){
	DialogueSakuya;
	}
	if(Player==3){
	DialogueSanae;
	}
	if(Player==4){
	DialogueYoumu;
	}
}

function DialogueReimu{
wait(50);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
SetCommonData("PlayerCutin",Characters[Player][0]);
SetCommonData("PlayerTalk",0);
DialogueCutinPlayer;
DialogueCutinBoss;
DialogueBox;
Dialogue(0, "Had enough?", 0, 0,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
Dialogue(1, "No, But I expected no less from the [r]Shrine maiden herself.", 1, 0,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	

Dialogue(1, "Have at it!", 1, 0,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	SetCommonData("EndDialogue",1);
mainTask;
}
function DialogueMarisa{
wait(50);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
SetCommonData("PlayerCutin",Characters[Player][0]);
SetCommonData("PlayerTalk",0);
DialogueCutinPlayer;
DialogueCutinBoss;
DialogueBox;
Dialogue(0, "You're quite a thing y'know that?", 1, 3,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
Dialogue(1, "Heheh, This is it...", 1, 0,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/4, 5, 4);
Dialogue(1, "It's time for you to succumb!", 1, 0,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);

DialogueCutinBoss;	
Dialogue(0, "How cliche!", 1,2,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	SetCommonData("EndDialogue",1);
mainTask;
}
function DialogueSakuya{
wait(50);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
SetCommonData("PlayerCutin",Characters[Player][0]);
SetCommonData("PlayerTalk",0);
DialogueCutinPlayer;
DialogueCutinBoss;
DialogueBox;
Dialogue(0, "It's funny to think how...", 2, 0,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
Dialogue(0, "You've been so quiet and nice[r]all this time...", 2, 0,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
Dialogue(1, "People change. You know that?", 1, 0,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
Dialogue(1, "Anyway, My Trump cards!", 1, 0,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	SetCommonData("EndDialogue",1);
mainTask;
}
function DialogueSanae{
wait(50);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
SetCommonData("PlayerCutin",Characters[Player][0]);
SetCommonData("PlayerTalk",0);
DialogueCutinPlayer;
DialogueCutinBoss;
DialogueBox;
Dialogue(0, "WOOOOO!",3, 3,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
Dialogue(1, "just shut up and bleed please", 1, 6,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
Dialogue(0, "SELF DEFENSE DANAMKUS GOOOOO!",3, 3,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	SetCommonData("EndDialogue",1);
mainTask;
}
function DialogueYoumu{
wait(50);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
SetCommonData("PlayerCutin",Characters[Player][0]);
SetCommonData("PlayerTalk",0);
DialogueCutinPlayer;
DialogueCutinBoss;
DialogueBox;

Dialogue(1, "Impressive how a human is still[r]standing against my disease.", 1, 0,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
Dialogue(0, "IM NOT HUMAN AAAAAAAAAAAAAAAAA", 4, 7,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	

Dialogue(0, "Who cares, Here go fully die.", 1, 0,  0,0,0);

	wait(5);
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/shot.wav", 200);
	wait(5);
	
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	SetCommonData("EndDialogue",1);
mainTask;
}

task mainTask { 
wait(30);
SpellCard("Web Weaving &quot;String Shot&quot;");
ObjEnemy_SetDamageRate(objBoss, 100, 100);
	loop{
	attack=true;
	WebSling;
	wait(20);
	attack=false;
		loop(7){
		ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2+rand(-100,100),GetStgFrameHeight/3+rand(-50,75), 15, 4);
		wait(60);
		}
	wait(160);
		counta++;
	}
}

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

	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 Spread(obj){
	wait(60);
	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(x+(i*8)*cos(angle),y+(i*8)*sin(angle),0,angle+rand(-5,5),ICE_GRAY,5);
	ObjMove_AddPatternA3(obj2, 60, NO_CHANGE, NO_CHANGE, 0.1, rand(-0.3, 0.3), 100, NO_CHANGE);
	ObjMove_AddPatternA3(obj2, 90, NO_CHANGE, NO_CHANGE, -0.02, 0, 1, NO_CHANGE);
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 60);
	if(counta>0){
	ascent(i in 0..len/10){
	let obj2 = CreateShotA2(x+(i*10)*cos(angle),y+(i*10)*sin(angle),3,angle, 0.05, 3, ICE_PURPLE,5);
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spirit.wav", 60);
	}
	if(counta>1){
	let obj2 = CreateShotA2(x,y,0.5, GetAngleToPlayer(obj), 0.01, 3, LARGE_PURPLE,5);
	Disease(obj2, 1.75, 30);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 60);
	}
	wait(110);
	}
	}
}

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