#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spellcard 2 Hard"]
#Text[" Mystery Sign - Shrouded in the Vapours of Hatred "]

let bossObj;
let BossX = 0;
let BossY = 0;
let imgBoss = GetCurrentScriptDirectory ~ "IMG/ghost.png";

#include "script/default_system/Default_ShotConst.txt"
#include "./Render.txt"

@Initialize{
	// Initialize: Works when starting up Danmakufu PH3
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);

	//  Defines a Boss and Registers it.
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	
	
	//Textures the Boss and Indicates the Boss's True Center.
	ObjPrim_SetTexture(bossObj,imgBoss);
	renderBoss;
	ObjSprite2D_SetDestCenter(bossObj);
	
    //Starts a Spellcard.
	LoadSound(GetCurrentScriptDirectory ~ "SE/spell.wav");
	PlaySE(GetCurrentScriptDirectory ~ "SE/spell.wav");
	let sceneObj = GetEnemyBossSceneObjectID;
	ObjEnemyBossScene_StartSpell(sceneObj);
	
	// Moves Boss to XY Location at wanted Speed (Parameters:Obj,X,Y,V)
	ObjMove_SetDestAtSpeed(bossObj,GetStgFrameWidth/2,GetStgFrameHeight/2,5);
	
	mainTask; //Runs Maintask
	
}

@Event{
	// Event: Sets the Boss's Specs
	
		// Sets Boss's Life and Timer
		alternative(GetEventType() )
		case(EV_REQUEST_LIFE) {
			SetScriptResult(4500);
		}
		case(EV_REQUEST_TIMER) {
			SetScriptResult(40);
		}
		case(EV_REQUEST_SPELL_SCORE) {
			SetScriptResult(50000);
		}

}

@MainLoop{
	// MainLoop: Runs throughout the script.
	
	// Indicates that BossX = GetEnemyX and BossY = GetEnemyY
	BossX = ObjMove_GetX(bossObj);
	BossY = ObjMove_GetY(bossObj);
	
	// Sets the Boss's Collision Circle(Hitbox) For the Shots and Player.
	ObjEnemy_SetIntersectionCircleToShot(bossObj,BossX,BossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,BossX,BossY,24);
	
	
	
	yield;
	
}

@Finalize{


}

// Your best friend. FOREVER!!! The Wait Function
function wait(w) { loop(w){ yield;} }

task mainTask{
	end;
	MagicCircle;
	CutIn("Mystery Sign \"Shrouded in the Mists of Death\"");
	wait(210);
	fire3;
	fire5;
}

task fire3{
	let spin = 0;
	let up = 185;
	
	loop{
	let soundObj = ObjSound_Create;
	ObjSound_Load(soundObj,GetCurrentScriptDirectory ~ "SE/shot5.wav");
	ObjSound_SetVolumeRate(soundObj,75);
	ObjSound_Play(soundObj);
		let angle = 2560*cos(spin);
		
		let obj = CreateShotA1(BossX,BossY,1.25,angle,up,30);
		
		let obj1 = CreateShotA1(BossX,BossY,1.25,-angle+180,up,30);
			
			spin+=1;
			up+=(1/20);
		wait(4);
	if(up >= 189){
		up = 185;
	}
	if(spin == 360){
		spin = 0;
	}
	}
	
}

task fire5{
	let spin = 0;
	let dir = 0;
	let x = 0;
	let i = 0;
	let up = 189;
	loop{
		ascent(i in 0..1){
	ConcentrationQ;
			while(x<44){
	
	LoadSound(GetCurrentScriptDirectory ~ "SE/effect.wav");
	PlaySE(GetCurrentScriptDirectory ~ "SE/effect.wav");
				mist(BossX+20*cos(dir-spin*67),BossY+20*sin(dir-spin*67),5,dir-spin*67,-0.1,0,0.5,30);
				
				dir+=360/44;
				x++;
				spin+=1;
				up+=(1/32);
			}
			x = 0;
			dir = 0;
			if(up >= 193){
				up = 189;
			}
			wait(5);
		}
		wait(220);
	}
	if(spin == 360){
		spin = 0;
	}
	
}


task mist(x,y,v,dir,accel,angdir,maxv,delay){
	let mist = ObjShot_Create(OBJ_SHOT);
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjShot_Regist(mist);
	
	ObjMove_SetPosition(mist,GetStgFrameWidth/2,GetStgFrameHeight/2);
	ObjMove_SetSpeed(mist,v);
	ObjMove_SetAngle(mist,dir);
	ObjMove_SetAcceleration(mist,accel);
	ObjMove_SetAngularVelocity(mist,angdir);
	ObjMove_SetMaxSpeed(mist,maxv);
	ObjShot_SetDelay(mist,delay);
	ObjShot_SetSpellResist(mist,true);
	ObjShot_SetGraphic(mist,255);
	Obj_SetVisible(mist,false);
	ObjShot_SetIntersectionEnable(mist,false);
	
	let mistpath = GetCurrentScriptDirectory ~ "IMG/MistSprite.png";
	let spin = 0;
	
	ObjPrim_SetTexture(obj,mistpath);
	LoadTexture(mistpath);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriority(obj,0.5);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAlpha(obj,200);
	
	while(!Obj_IsDeleted(obj) && !Obj_IsDeleted(mist)){
		ObjRender_SetPosition(obj,ObjMove_GetX(mist),ObjMove_GetY(mist),120);
		RemoveTexture(obj);
		ObjRender_SetAngleXYZ(obj,0,0,dir+spin);

		spin+=1;
		if(spin == 360){
			spin = 0;
		}
		yield;
	}
	
	ObjShot_FadeDelete(obj);
	ObjShot_FadeDelete(mist);
	RemoveTexture(obj);
	Obj_SetVisible(obj,false);
	
	loop{
	ObjRender_SetPosition(obj,ObjMove_GetX(mist),ObjMove_GetY(mist),120);
	yield;
	}
	
	ObjShot_SetDeleteFrame(obj,600);
	ObjShot_SetDeleteFrame(mist,600);
	Obj_SetVisible(obj,false);
	
	while(Obj_IsDeleted(obj) || Obj_IsDeleted(mist)){
	ObjShot_FadeDelete(obj);
	ObjShot_FadeDelete(mist);
	Obj_SetVisible(obj,false);
		yield;
	}
}

task end{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){ yield;}

	Obj_Delete(bossObj);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	

	CloseScript(GetOwnScriptID());
}