#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spellcard 1 Lunatic"]
#Text[" Horror Sign - Unexpected Plot Twist "]

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);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	//Textures the Boss and Indicates the Boss's True Center.
	ObjPrim_SetTexture(bossObj,imgBoss);
	renderBoss;
	ObjSprite2D_SetDestCenter(bossObj);
	
	LoadSound(GetCurrentScriptDirectory ~ "SE/spell.wav");
	
    //Starts a Spellcard.
	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(5000);
		}
		case(EV_REQUEST_TIMER) {
			SetScriptResult(60);
		}
		
		case(EV_REQUEST_SPELL_SCORE) {
			SetScriptResult(10000);
		}

}

@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);
	
	SetShotAutoDeleteClip(10,10,10,10);
	yield;

}

@Finalize{


}

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

task mainTask{
	end;
	MagicCircle;
	CutIn("Horror Sign \"Unexpected Plot Twist\"");
	wait(240);
	loop{
	ConcentrationQ;
	wait(110);
	fire;
	wait(150);
	shake;
	wait(150);
	fire2;
	wait(200);
	}
}

task fire{
	let spin = 0;
	let dir = 0;
	let x = 0;
	let i = 0;
	ascent(i in 0..30){
		LoadSound(GetCurrentScriptDirectory ~ "SE/shot.wav");
	PlaySE(GetCurrentScriptDirectory ~ "SE/shot.wav");
		while(x<8){
		let obj = CreateShotA1(BossX+(100-(spin))*cos(dir+spin),BossY+(100-(spin))*sin(dir+spin),1.5,dir,194,30);
		ObjMove_SetAngularVelocity(obj,0.5);
		ObjMove_SetAcceleration(obj,-0.01);
		ObjMove_SetMaxSpeed(obj,-3);
		
		let obj1 = CreateShotA1(BossX+(100-(spin))*cos(dir-spin),BossY+(100-(spin))*sin(dir-spin),1.5,dir,194,30);
		ObjMove_SetAngularVelocity(obj1,-0.5);
		ObjMove_SetAcceleration(obj1,-0.01);
		ObjMove_SetMaxSpeed(obj1,-3);
		
		dir+=360/8;
		x++;
		spin++;
		}
		
		x = 0;
		dir = 0;
		yield;
	}
	if(spin == 359){
		spin = 0;
	}
}

task fire2{
	let spin = 0;
	let dir = 0;
	let x = 0;
	let i = 0;
	
	ascent(i in 0..9){
		while(x<12){
		LoadSound(GetCurrentScriptDirectory ~ "SE/wave.wav");
		PlaySE(GetCurrentScriptDirectory ~ "SE/wave.wav");
		let obj = CreateShotA1(BossX+(80-(i*3))*cos(dir-i*14),BossY+(80-(i*3))*sin(dir-i*14),3,dir+135-i,205,30);
		ObjMove_SetAngularVelocity(obj,-0.3);
		ObjMove_SetAcceleration(obj,-0.05);
		ObjMove_SetMaxSpeed(obj,-2);
		
		let obj1 = CreateShotA1(BossX+(80-(i*3))*cos(dir+i*14),BossY+(80-(i*3))*sin(dir+i*14),3,dir-135+i,205,30);
		ObjMove_SetAngularVelocity(obj1,0.3);
		ObjMove_SetAcceleration(obj1,-0.05);
		ObjMove_SetMaxSpeed(obj1,-2);
		
		
		dir+=360/12;
		x++;
		spin++;
		}
		
		x = 0;
		dir = 0;
		wait(25);
	}
	if(spin == 359){
		spin = 0;
	}
}


task shake{
	let spin = 0;
		while(!Obj_IsDeleted(bossObj)){
			loop(100){
			Set2DCameraFocusX(192 + rand(-spin/100, spin/100));
			Set2DCameraFocusY(224 + rand(-spin/100, spin/100));
			yield;
			spin++;
			}
			yield;
		}
		loop(10){
			spin-=5;
			yield;
		}
		wait(10);
		Reset2DCamera();
	}
	
task end{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){ yield;}

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

	CloseScript(GetOwnScriptID());
}