#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spellcard 3"]
#Text[" Mononobe vs Toyosatomimi - [r]\"Clash of the Clans\" "]
#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 weirdsound = GetCurrentScriptDirectory ~ "se/um2.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(10500);
		}
		case(EV_REQUEST_TIMER) {
			SetScriptResult(80);
		}

}

@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-32,15,5);
	wait(80);
	CutIn("Mononobe vs Toyosatomimi - \"Clash of the Clans\"");
	let sceneObj = GetEnemyBossSceneObjectID;
	ObjEnemyBossScene_StartSpell(sceneObj);
	wait(160);
	ConcentrationQ;
	wait(80);
	loop{
	initiate;
	wait(360);
	ConcentrationQ;
	wait(80);
	part1;
	wait(1080);
	ConcentrationQ;
	wait(80);
	part2;
	wait(480);
	ConcentrationQ;
	wait(80);
	part3;
	wait(1580);
	ConcentrationQ;
	wait(80);
	loop(3){
	part4;
	wait(180);
	}
	wait(180);
	ConcentrationQ;
	wait(80);
	}
}

task initiate{
			Anim_Attack = 1;
	let spin = 0;
	let dir = 0;
	let a = 8;
	let b = 6;
	let s = 0;
	let x = 0;
	let i = 0;
	
	ascent(i in 0..6){
		while(x<a){
		
			Play(shot);
			BurstBullet(BossX+0*cos(dir+i*4),BossY+0*sin(dir+i*4),1+i/5,dir+i*4,363,10);
			BurstBullet(BossX+0*cos(dir-i*4),BossY+0*sin(dir-i*4),1+i/5,dir-i*4,363,10);
			
			task BurstBullet(x,y,v,ang,graphic,delay){
				let burstObj = CreateShotA1(x,y,v,ang,graphic,delay);
				ObjMove_SetAcceleration(burstObj,-0.01);
				ObjMove_SetMaxSpeed(burstObj,0);
				
				wait(160);
				if(!Obj_IsDeleted(burstObj)){
				Play(wave);
					loop(b){
						CreateShotA1(ObjMove_GetX(burstObj)+0*cos(dir),ObjMove_GetY(burstObj)+0*sin(dir),1,dir,182,20);
						dir+=360/b;
					}
					Obj_Delete(burstObj);
				}
			}
			
			dir+=360/a;
			x++;
			spin+=2/a;
		}
		x = 0;
		dir = 0;
		wait(1);
		Anim_Attack = 0;
	}
}

task part1{
	let spin = 0;
	let dir = 0;
	let a = 22;
	let x = 0;
	
	ascent(i in 0..3){
		ascent(k in 0..4){
			Anim_Attack = 1;
				while(x<a){

					go(k);
					
					task go(num){
						if(num == 1 || num == 3){
						Play(shot);
						let obj = CreateShotA1(BossX+0*cos(dir),BossY+0*sin(dir),0.75+k/5,dir,861,10);
						Play(shot);
						let obj1 = CreateShotA1(BossX+0*cos(dir),BossY+0*sin(dir),0.75+k/5,dir+((360/a)/2),861,10);
						
					
						wait(120);
						Play(wave);
						ObjMove_SetAngularVelocity(obj,-0.2);
						}
						else{
						Play(shot);
						let obj = CreateShotA1(BossX+0*cos(dir),BossY+0*sin(dir),0.75+k/5,dir,861,10);
						Play(shot);
						let obj1 = CreateShotA1(BossX+0*cos(dir),BossY+0*sin(dir),0.75+k/5,dir+((360/a)/2),861,10);
						
					
						wait(120);
						Play(wave);
						ObjMove_SetAngularVelocity(obj,0.2);
						}
					}
					
					dir+=360/a;
					x++;
					spin+=2/a;
				}
				
				x = 0;
				dir = 0;
				wait(1);
				Anim_Attack = 0;
		}
		wait(360);
	}
}

task part2{
			Anim_Attack = 1;
	let spin = 90;

				let obj = CreateShotA1(BossX,BossY,4,45,855,10);
				let obj1 = CreateShotA1(BossX,BossY,4,55,855,10);
				let obj2 = CreateShotA1(BossX,BossY,4,70,855,10);
				let obj3 = CreateShotA1(BossX,BossY,4,110,855,10);
				let obj4 = CreateShotA1(BossX,BossY,4,125,855,10);
				let obj5 = CreateShotA1(BossX,BossY,4,135,855,10);
				Play(wave);
				
				check1;
				check2;
				check3;
				check4;
				check5;
				check6;
				
				wait(100);
				
				shoot;
				
				task shoot{
					let dir = 0;
					let x = 0;
					let s = 1;
					ascent(i in 0..25){
						Play(shot);
						while(x<4){
						CreateShotA1(ObjRender_GetX(obj),GetStgFrameHeight-i*20,0.875,spin,s,10);
						CreateShotA1(ObjRender_GetX(obj1),GetStgFrameHeight-i*20,0.875,spin,s,10);
						CreateShotA1(ObjRender_GetX(obj2),GetStgFrameHeight-i*20,0.875,spin,s,10);
						CreateShotA1(ObjRender_GetX(obj3),GetStgFrameHeight-i*20,0.875,-spin,s,10);
						CreateShotA1(ObjRender_GetX(obj4),GetStgFrameHeight-i*20,0.875,-spin,s,10);
						CreateShotA1(ObjRender_GetX(obj5),GetStgFrameHeight-i*20,0.875,-spin,s,10);
						dir+=360/4;
						x++;
						spin+=1243/4;
						}
						x = 0;
						dir = 0;
						wait(2);
						s+=1/4;
						if(s==16){
							s = 1;
						}
					}
					
				}
				task check1{
				while(!Obj_IsDeleted(obj)){
					if(ObjMove_GetY(obj) >= GetStgFrameHeight){
						Play(wave);
						ObjMove_SetSpeed(obj,0);
						wait(200);
						ObjShot_FadeDelete(obj);
						break;
					}
					yield;
				}
				}
				task check2{
				while(!Obj_IsDeleted(obj1)){
					if(ObjMove_GetY(obj1) >= GetStgFrameHeight){
						Play(wave);
						ObjMove_SetSpeed(obj1,0);
						wait(200);
						ObjShot_FadeDelete(obj1);
						break;
					}
					yield;
				}
				}
				task check3{
				while(!Obj_IsDeleted(obj2)){
					if(ObjMove_GetY(obj2) >= GetStgFrameHeight){
						Play(wave);
						ObjMove_SetSpeed(obj2,0);
						wait(200);
						ObjShot_FadeDelete(obj2);
						break;
					}
					yield;
				}
				}
				task check4{
				while(!Obj_IsDeleted(obj3)){
					if(ObjMove_GetY(obj3) >= GetStgFrameHeight){
						Play(wave);
						ObjMove_SetSpeed(obj3,0);
						wait(200);
						ObjShot_FadeDelete(obj3);
						break;
					}
					yield;
				}
				}
				task check5{
				while(!Obj_IsDeleted(obj4)){
					if(ObjMove_GetY(obj4) >= GetStgFrameHeight){
						Play(wave);
						ObjMove_SetSpeed(obj4,0);
						wait(200);
						ObjShot_FadeDelete(obj4);
						break;
					}
					yield;
				}
				}
				task check6{
				while(!Obj_IsDeleted(obj5)){
					if(ObjMove_GetY(obj5) >= GetStgFrameHeight){
						Play(wave);
						ObjMove_SetSpeed(obj5,0);
						wait(200);
						ObjShot_FadeDelete(obj5);
						break;
					}
					yield;
				}
				}
			wait(1);
		
		Anim_Attack = 0;
		wait(120);
}

task part3{
	let spin = 0;
	let spin3 = 0;
	let dir = 0;
	let dir2 = 0;
	let a = 80;
	let x = 0;
	
	loop(2){
			Anim_Attack = 1;
		while(x<a){
			let r = rand(0,380)+10;
			let spin2 = 0;
			let spin4 = 0;
		
			GlobeBullet(BossX+r*cos(dir),BossY+r*sin(dir),0,dir,860,0);
			GlobeBullet2(BossX+r*cos(dir),BossY+r*sin(dir),0,dir,860,420);
			
			task GlobeBullet(x,y,v,ang,graphic,delay){
				wait(delay);
				let obj = CreateShotA1(x,y,v,ang,graphic,0);
				ObjShot_SetIntersectionEnable(obj,false);
				ObjShot_SetAutoDelete(obj,false);
				ObjShot_SetSpellResist(obj,true);
				ObjRender_SetAlpha(obj,0);
				spin = 0;
				move;
				Play(wave);
				while(spin<100){
				ObjRender_SetAlpha(obj,spin);
				spin+=0.025;
				yield;
				}
				wait(90);
				Play(barrage);
				while(spin<255){
				ObjRender_SetAlpha(obj,spin);
				spin+=0.05;
				yield;
				}
				ObjShot_SetIntersectionEnable(obj,true);
				wait(240);
				ObjShot_FadeDelete(obj);
				task move{
					while(!Obj_IsDeleted(obj)){
					ObjMove_SetPosition(obj,BossX+r*cos(dir+spin2),BossY+r*sin(dir+spin2));
					dir+=360/a;
					spin2+=0.875;
					yield;
					}
				}
			}
			
			task GlobeBullet2(x,y,v,ang,graphic,delay){
				wait(delay);
				let obj1 = CreateShotA1(x,y,v,ang,graphic,0);
				ObjShot_SetIntersectionEnable(obj1,false);
				ObjShot_SetAutoDelete(obj1,false);
				ObjShot_SetSpellResist(obj1,true);
				ObjRender_SetAlpha(obj1,0);
				let spin3 = 0;
				let spin4 = 0;
				move2;
				Play(wave);
				while(spin3<100){
				ObjRender_SetAlpha(obj1,spin3);
				spin3+=4;
				yield;
				}
				wait(90);
				Play(barrage);
				while(spin3<255){
				ObjRender_SetAlpha(obj1,spin3);
				spin3+=4;
				yield;
				}
				ObjShot_SetIntersectionEnable(obj1,true);
				wait(240);
				ObjShot_FadeDelete(obj1);
				task move2{
					while(!Obj_IsDeleted(obj1)){
					ObjMove_SetPosition(obj1,BossX+r*cos(dir2-spin4),BossY+r*sin(dir2-spin4));
					dir2+=360/a;
					spin4+=0.875;
					yield;
					}
				}
			}
			
			dir+=360/a;
			dir2+=360/a;
			x++;
			spin+=1/a;
			spin3+=1/a;
		}
		
		x = 0;
		dir = 0;
		Anim_Attack = 0;
		wait(780);
	}
}

task part4{
	let spin = 0;
	let down = 0;
	let dir = 0;
	let a = 16;
	let x = 0;
			Anim_Attack = 1;
					while(x<a){
						move;
						
						task move{
								let obj = CreateShotA1(192+140*cos(dir),-140+140*sin(dir),1,90,857,10);
								let obj1 = CreateShotA1(GetStgFrameWidth-192+140*cos(dir),-140+140*sin(dir),1,90,861,10);
								let obj2 = CreateShotA1(64+140*cos(dir),-280+140*sin(dir),1,90,858,10);
								let obj3 = CreateShotA1(GetStgFrameWidth-64+140*cos(dir),-280+140*sin(dir),1,90,860,10);
								ObjShot_SetAutoDelete(obj1,false);
								ObjShot_SetAutoDelete(obj,false);
								ObjShot_SetAutoDelete(obj2,false);
								ObjShot_SetAutoDelete(obj3,false);
								delete;
								task delete{
									wait(440);
									ObjShot_FadeDelete(obj);
									ObjShot_FadeDelete(obj1);
									wait(140);
									ObjShot_FadeDelete(obj2);
									ObjShot_FadeDelete(obj3);
								}
								while(!Obj_IsDeleted(obj) || !Obj_IsDeleted(obj1) || !Obj_IsDeleted(obj2) || !Obj_IsDeleted(obj3)){
									ObjMove_SetPosition(obj,192+140*cos(dir+spin),-140+down+140*sin(dir+spin));
									ObjMove_SetPosition(obj1,GetStgFrameWidth-192+140*cos(dir-spin),-140+down+140*sin(dir-spin));
									ObjMove_SetPosition(obj2,64+140*cos(dir+spin),-280+down+140*sin(dir+spin));
									ObjMove_SetPosition(obj3,GetStgFrameWidth-64+140*cos(dir-spin),-280+down+140*sin(dir-spin));
									dir+=360/a;
									spin+=0.033;
									down+=0.1;
									yield;
								}
						}
					
						dir+=360/a;
						x++;
						spin+=16/a;
					}
					x = 0;
					dir = 0;
					Anim_Attack = 0;
}

task end{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){ yield;}
	let ex = ObjMove_GetX(bossObj);
	let ey = ObjMove_GetY(bossObj);
	Death(ex,ey);
	Obj_Delete(bossObj);
	
	SetAutoDeleteObject(true);
	
	loop(220){
	SetAutoDeleteObject(true);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	yield;
	}
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	

	CloseScript(GetOwnScriptID());
}