#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spellcard 1"]
#Text[" Mastery Art - [r]\"Dragon of the East\" "]
#System["./System.txt"]
#Background["./Background.dnh"]
#Player["./player/YukaMarisa/YukaMarisa.dnh"]

let bossObj;
let attack = false;
let BossX = 0;
let BossY = 0;

let shot = GetCurrentScriptDirectory ~ "se/Shot1.wav";
let shot3 = GetCurrentScriptDirectory ~ "se/Shot3.wav";
let wave = GetCurrentScriptDirectory ~ "se/BulletWave.wav";
let barrage = GetCurrentScriptDirectory ~ "se/barrage.wav";
let eff = GetCurrentScriptDirectory ~ "se/Eff1.wav";
let Eff2 = GetCurrentScriptDirectory ~ "se/Eff2.wav";
let Eff4 = GetCurrentScriptDirectory ~ "se/Eff4.wav";
let laser = GetCurrentScriptDirectory ~ "se/Laser3.wav";
let Anim_Attack = 0;

#include "./Render.dnh"
#include "./shot/AllStarShot_Const.dnh"

@Initialize{
	// Initialize: Works when starting up Danmakufu PH3

	//  Defines a Boss and Registers it.
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	
	NameFuto;
	AnimateBossFuto(bossObj);
	ObjMove_SetDestAtWeight(bossObj,GetStgFrameWidth/2,GetStgFrameHeight/2-64,15,5);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	
	mainTask; //Runs Maintask
	
}

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

}

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

}

@Finalize{


}

// Your best friend. FOREVER!!! The Wait Function
function wait(w) { loop(w){ yield;} }
task mainTask{
	end;
	wait(10);
	ObjMove_SetDestAtWeight(bossObj,GetStgFrameWidth/2,GetStgFrameHeight/2-64,15,5);
	wait(80);
	CutIn("Mastery Art - \"Dragon of the East\"");
	let sceneObj = GetEnemyBossSceneObjectID;
	ObjEnemyBossScene_StartSpell(sceneObj);
	wait(160);
	ConcentrationQ;
	wait(80);
	fire;
	movement;
	loop{
		wait(280);
		ConcentrationQ;
		wait(80);
		fire2;
		wait(210);
		
	}
}

task movement{
	while(!Obj_IsDeleted(bossObj)){
		RandMove(bossObj,70,70,15,10,192,128,GetStgFrameWidth-192,GetStgFrameHeight/2-128);
		wait(180);
	}
}

task fire{
	let spin = 0;
		while(!Obj_IsDeleted(bossObj)){
			wait(20);
			Play(barrage);
			let r = rand(32,GetStgFrameWidth-32);
			let r1 = rand(32,GetStgFrameWidth-32);
			let r2 = rand(32,GetStgFrameWidth-32);
			let r3 = rand(32,GetStgFrameWidth-32);
			ascent(i in 0..8){
			
				move;
				move1;
				move2;
				move3;
				task move{
					wait(20);
				let obj = [CreateShotA1(r-8,-10,3,90,5,10),CreateShotA1(r-3,0,3,90,11,10),CreateShotA1(r+3,0,3,90,11,10),CreateShotA1(r+8,-10,3,90,5,10)];
						ObjMove_SetAngularVelocity(obj[0],-1);
						ObjMove_SetAngularVelocity(obj[1],-1);
						ObjMove_SetAngularVelocity(obj[2],-1);
						ObjMove_SetAngularVelocity(obj[3],-1);
						wait(30);
					while(!Obj_IsDeleted(obj)){
						ObjMove_SetAngularVelocity(obj[0],1);
						ObjMove_SetAngularVelocity(obj[1],1);
						ObjMove_SetAngularVelocity(obj[2],1);
						ObjMove_SetAngularVelocity(obj[3],1);
						wait(60);
						ObjMove_SetAngularVelocity(obj[0],-1);
						ObjMove_SetAngularVelocity(obj[1],-1);
						ObjMove_SetAngularVelocity(obj[2],-1);
						ObjMove_SetAngularVelocity(obj[3],-1);
						wait(60);
					}
				}
				task move1{
					wait(40);
				let obj1 = [CreateShotA1(r1-8,-10,3,90,5,10),CreateShotA1(r1-3,0,3,90,11,10),CreateShotA1(r1+3,0,3,90,11,10),CreateShotA1(r1+8,-10,3,90,5,10)];
						ObjMove_SetAngularVelocity(obj1[0],-1);
						ObjMove_SetAngularVelocity(obj1[1],-1);
						ObjMove_SetAngularVelocity(obj1[2],-1);
						ObjMove_SetAngularVelocity(obj1[3],-1);
						wait(30);
					while(!Obj_IsDeleted(obj1)){
						ObjMove_SetAngularVelocity(obj1[0],1);
						ObjMove_SetAngularVelocity(obj1[1],1);
						ObjMove_SetAngularVelocity(obj1[2],1);
						ObjMove_SetAngularVelocity(obj1[3],1);
						wait(60);
						ObjMove_SetAngularVelocity(obj1[0],-1);
						ObjMove_SetAngularVelocity(obj1[1],-1);
						ObjMove_SetAngularVelocity(obj1[2],-1);
						ObjMove_SetAngularVelocity(obj1[3],-1);
						wait(60);
					}
				}
				task move2{
					wait(60);
				let obj2 = [CreateShotA1(r2-8,-10,3,90,5,10),CreateShotA1(r2-3,0,3,90,11,10),CreateShotA1(r2+3,0,3,90,11,10),CreateShotA1(r2+8,-10,3,90,5,10)];
						ObjMove_SetAngularVelocity(obj2[0],-1);
						ObjMove_SetAngularVelocity(obj2[1],-1);
						ObjMove_SetAngularVelocity(obj2[2],-1);
						ObjMove_SetAngularVelocity(obj2[3],-1);
						wait(30);
						ObjMove_SetAngularVelocity(obj2[0],1);
						ObjMove_SetAngularVelocity(obj2[1],1);
						ObjMove_SetAngularVelocity(obj2[2],1);
						ObjMove_SetAngularVelocity(obj2[3],1);
						wait(30);
					while(!Obj_IsDeleted(obj2)){
						ObjMove_SetAngularVelocity(obj2[0],-1);
						ObjMove_SetAngularVelocity(obj2[1],-1);
						ObjMove_SetAngularVelocity(obj2[2],-1);
						ObjMove_SetAngularVelocity(obj2[3],-1);
						wait(60);
						ObjMove_SetAngularVelocity(obj2[0],1);
						ObjMove_SetAngularVelocity(obj2[1],1);
						ObjMove_SetAngularVelocity(obj2[2],1);
						ObjMove_SetAngularVelocity(obj2[3],1);
						wait(60);
					}
				}
				task move3{
					wait(80);
				let obj3 = [CreateShotA1(r3-8,-10,3,90,5,10),CreateShotA1(r3-3,0,3,90,11,10),CreateShotA1(r3+3,0,3,90,11,10),CreateShotA1(r3+8,-10,3,90,5,10)];
						ObjMove_SetAngularVelocity(obj3[0],1);
						ObjMove_SetAngularVelocity(obj3[1],1);
						ObjMove_SetAngularVelocity(obj3[2],1);
						ObjMove_SetAngularVelocity(obj3[3],1);
						wait(30);
					while(!Obj_IsDeleted(obj3)){
						ObjMove_SetAngularVelocity(obj3[0],-1);
						ObjMove_SetAngularVelocity(obj3[1],-1);
						ObjMove_SetAngularVelocity(obj3[2],-1);
						ObjMove_SetAngularVelocity(obj3[3],-1);
						wait(60);
						ObjMove_SetAngularVelocity(obj3[0],1);
						ObjMove_SetAngularVelocity(obj3[1],1);
						ObjMove_SetAngularVelocity(obj3[2],1);
						ObjMove_SetAngularVelocity(obj3[3],1);
						wait(60);
					}
				}
				wait(5);
				spin+=10;
			}
		}
}

task fire2{
	let spin = 0;
		wait(20);
			
			let r = GetPlayerX;
			Play(Eff4);
			Anim_Attack = 1;
			let obj = CreateStraightLaserA1(r,0,90,500,60,75,1057,160);
			
			count;
			
			task count{
				let count = 0;
				loop(200){
					if(count==160){
					Anim_Attack = 0;
					ObjMove_SetSpeed(bossObj,0);
						fire;
						Play(laser);
					}
				count+=1;
				yield;
				}
				count = 0;
			}
			
			ascent(i in 0..10){
			
				move;
				
				task move{
					wait(20);
			Play(Eff2);
				let obj = [CreateShotA1(r-8,-10,4,92.5,6,10),CreateShotA1(r-3,0,4,92.5,4,10),CreateShotA1(r+3,0,4,92.5,4,10),CreateShotA1(r+8,-10,4,92.5,6,10)];
				let obj1 = [CreateShotA1(r-8,-10,4,87.5,6,10),CreateShotA1(r-3,0,4,87.5,4,10),CreateShotA1(r+3,0,4,87.5,4,10),CreateShotA1(r+8,-10,4,87.5,6,10)];
						
						ObjShot_SetSpellResist(obj[0],true);
						ObjShot_SetSpellResist(obj[1],true);
						ObjShot_SetSpellResist(obj[2],true);
						ObjShot_SetSpellResist(obj[3],true);
						ObjShot_SetSpellResist(obj1[0],true);
						ObjShot_SetSpellResist(obj1[1],true);
						ObjShot_SetSpellResist(obj1[2],true);
						ObjShot_SetSpellResist(obj1[3],true);
						
						ObjMove_SetAngularVelocity(obj[0],-1);
						ObjMove_SetAngularVelocity(obj[1],-1);
						ObjMove_SetAngularVelocity(obj[2],-1);
						ObjMove_SetAngularVelocity(obj[3],-1);
						ObjMove_SetAngularVelocity(obj1[0],1);
						ObjMove_SetAngularVelocity(obj1[1],1);
						ObjMove_SetAngularVelocity(obj1[2],1);
						ObjMove_SetAngularVelocity(obj1[3],1);
						wait(30);
					while(!Obj_IsDeleted(obj)){
						ObjMove_SetAngularVelocity(obj[0],1.25);
						ObjMove_SetAngularVelocity(obj[1],1.25);
						ObjMove_SetAngularVelocity(obj[2],1.25);
						ObjMove_SetAngularVelocity(obj[3],1.25);
						ObjMove_SetAngularVelocity(obj1[0],-1.25);
						ObjMove_SetAngularVelocity(obj1[1],-1.25);
						ObjMove_SetAngularVelocity(obj1[2],-1.25);
						ObjMove_SetAngularVelocity(obj1[3],-1.25);
						wait(30);
						ObjMove_SetAngularVelocity(obj[0],-1.25);
						ObjMove_SetAngularVelocity(obj[1],-1.25);
						ObjMove_SetAngularVelocity(obj[2],-1.25);
						ObjMove_SetAngularVelocity(obj[3],-1.25);
						ObjMove_SetAngularVelocity(obj1[0],1.25);
						ObjMove_SetAngularVelocity(obj1[1],1.25);
						ObjMove_SetAngularVelocity(obj1[2],1.25);
						ObjMove_SetAngularVelocity(obj1[3],1.25);
						wait(30);
					}
				}
				wait(4);
				spin+=10;
			}
			
			task fire{
				let x = 0;
				let a = 64;
				let dir = 0;
				let spin = 0;
				
					while(x<a){
						let obj = CreateShotB2(r,GetStgFrameHeight,rand(-2,2),rand(-3,-5.5),0,0.033,0.125,3.25,838,0);
						let obj1 = CreateShotB2(r,GetStgFrameHeight,rand(-2,2),rand(-3,-5.5),0,0.033,0.125,3.25,832,0);
						ObjShot_SetSpellResist(obj,true);
						ObjShot_SetSpellResist(obj1,true);
						x++;
						dir+=360/a;
						spin+=10/a;
					}
					x = 0;
					dir = 0;
					wait(300);
					
			}

}

task end{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){ yield;}
	
	Obj_Delete(bossObj);
	Anim_Attack = 0;
	
	loop(2){
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	yield;
	}
	

	CloseScript(GetOwnScriptID());
}