#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell3"]
#Text["Woop"]


let objBoss;

let BossX;
let BossY;

let AttackX;
let AttackY;

#include "./Functions/Index.txt"
#include "./Functions/Dialogue.txt"


@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	RenderClause(objBoss);
	BossShotSound(objBoss, 50, 600);
	DrawName("Wilhelm von Clausewitz Halcyon HISUIMARUUUU", [255, 0, 255], 12.7);
	BottomEnemyMarker(objBoss, objBoss);
	MagicRing(objBoss, false);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/4, 5, 4);
	mainTask;
	end;

} 

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(7500);
     	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(80000000);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
} 

@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	
	ObjEnemy_SetIntersectionCircleToShot(objBoss,BossX,BossY,21);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,BossX,BossY,16);
	yield;
} 

@Finalize {

} 

task mainTask { 
SpellCard("Void &quot;G-Free Dream&quot;");
let angle = 0;
let count = 0;
let count2 = 0;
Grav;
Gravi;
	ascent(i in 0..16){
	let obj = CreateShotB2(BossX, BossY, 0.2+(i/5), -1.5-(i/20), 0, 0.04, 5, 5, CG_BULLET_PURPLE, 0);
	let obj2 = CreateShotB2(BossX, BossY, -0.2-(i/5), -1.5-(i/20), 0, 0.04, 5, 5, CG_BULLET_PURPLE, 0);
	ObjShot_SetAutoDelete(obj, 0);
	ObjShot_SetAutoDelete(obj2, 0);
	ObjShot_SetSpellResist(obj, 1);
	ObjShot_SetSpellResist(obj2, 1);
	GravityBullet(obj);
	GravityBullet(obj2);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/e_shot_01.wav", 50);
	yield;
	}
}

let IsGravity = true;
task GravityBullet(obj){
	loop{
	let mult = 0;
		
	let dir = ObjMove_GetAngle(obj);
	if(cos(dir) > 0){mult=-1}
	if(cos(dir) < 0){mult=1}
		
	let xvel = ObjMove_GetSpeed(obj)*cos(ObjMove_GetAngle(obj));
	let yvel = ObjMove_GetSpeed(obj)*sin(ObjMove_GetAngle(obj));
		
	while(IsGravity==true){
		if(ObjMove_GetX(obj)<0||ObjMove_GetX(obj)>=GetStgFrameWidth){
		xvel = -xvel;
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/warning.wav", 50);
		}
		if(ObjMove_GetY(obj)<0||ObjMove_GetY(obj)>=GetStgFrameHeight){
		yvel = -yvel;
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/warning.wav", 50);
		}
			
		yvel+=0.02;
		ObjMove_AddPatternB1(obj, 0, xvel, yvel);
		yield;
	}
	yield;
		ObjMove_AddPatternA1(obj, 0, ObjMove_GetSpeed(obj), NO_CHANGE);
		let bounce = 1;
		while(IsGravity==false){ //Check out my epic stack of arguments!
	if(trunc(ObjRender_GetX(obj))>GetStgFrameWidth && bounce > 1){ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj)); ObjMove_SetSpeed(obj, ObjMove_GetSpeed(obj)*0.8); bounce=0; SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/warning.wav", 50);}
	if(trunc(ObjRender_GetX(obj))<0 && bounce > 1){ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj)); ObjMove_SetSpeed(obj, ObjMove_GetSpeed(obj)*0.8); bounce=0; SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/warning.wav", 50);}
    if(trunc(ObjMove_GetY(obj))>GetStgFrameHeight && bounce > 1){ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj)); ObjMove_SetSpeed(obj, ObjMove_GetSpeed(obj)*0.8); bounce=0; SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/warning.wav", 50);}
    if(trunc(ObjRender_GetY(obj))<0 && bounce > 1){ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj)); ObjMove_SetSpeed(obj, ObjMove_GetSpeed(obj)*0.8); bounce=0; SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/warning.wav", 50);}
		bounce+=0.1;
		yield;
		}
	yield;
	if(trunc(ObjRender_GetX(obj))>GetStgFrameWidth || trunc(ObjRender_GetX(obj))<0 || trunc(ObjMove_GetY(obj))>GetStgFrameHeight || trunc(ObjRender_GetY(obj))<0){Obj_Delete(obj);}
	}
}

task Grav{
wait(120);
	loop{
	let obj = GravityWell;
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/laser_0.wav", 90);
	IsGravity = false;
	Expand(obj);
	
	let obj2 = GravityWell;
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/laser_0.wav", 90);
	IsGravity = true;
	Expand(obj2);
	}
	
	function Expand(obj){
	let scale = 1;
	let alpha = 255;
		loop(240){
		ObjRender_SetPosition(obj, BossX, BossY, 0);
		ObjRender_SetScaleXYZ(obj, scale, scale, 1);
		ObjRender_SetAngleZ(obj, ObjRender_GetAngleZ(obj)+5);
		ObjRender_SetAlpha(obj, alpha*1.1);
		scale+=0.3;
		alpha--;
		yield;
		}
		Obj_Delete(obj);
	}
}

task Gravi{
	let xvel = 0;
	let yvel = 0;
	let speed;
	let gear = GetPlayerSpeed;
	SetPlayerSpeed(0,0);
	reset;
	
	while(!Obj_IsDeleted(objBoss)){
		if(GetVirtualKeyState(VK_SLOWMOVE) != KEY_FREE){speed = 1;}
		else{speed = 1.5;}
		if(IsPlayerSpellActive || IsGravity == true){SetPlayerSpeed(gear[0],gear[1]);  xvel = 0; yvel = 0;}
		else{SetPlayerSpeed(0,0);}
		if(ObjRender_GetX(GetPlayerObjectID)<=16||ObjRender_GetX(GetPlayerObjectID)>=GetStgFrameWidth-16||GetPlayerState!= STATE_NORMAL){
		xvel = -xvel;
		}
		if(ObjRender_GetY(GetPlayerObjectID)<=16||ObjRender_GetY(GetPlayerObjectID)>=GetStgFrameHeight-18||GetPlayerState!= STATE_NORMAL){
		yvel = -yvel;
		}
		
		if(GetPlayerState == STATE_NORMAL && !IsPlayerSpellActive && IsGravity == false){
			if(GetVirtualKeyState(VK_RIGHT) != KEY_FREE && xvel <= speed*25){xvel+=speed;}
			if(GetVirtualKeyState(VK_LEFT) != KEY_FREE && xvel >= -speed*25){xvel-=speed;}
			if(GetVirtualKeyState(VK_DOWN) != KEY_FREE && yvel <= speed*25){yvel+=speed;}
			if(GetVirtualKeyState(VK_UP) != KEY_FREE && xvel >= -speed*25){yvel-=speed;}
			
			ObjMove_SetPosition(GetPlayerObjectID,GetPlayerX+(xvel/10),GetPlayerY+(yvel/10));
			}
		
		yield;
	}
	task reset{
		while(!Obj_IsDeleted(objBoss)){
			yield;
		}
		SetPlayerSpeed(gear[0],gear[1]);
	}
}

function SpellCard(name){
	let Cutin = GetCurrentScriptDirectory ~ "Functions/img/Emotions/Cla1.png";
	ObjCutin_SetSpellcardS3(name,255, 0, 255);
	ObjCutin_LaunchS3("YABUSAME", Cutin, "Normal");
	TCallBg;
	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);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
} 
