
#ScriptVersion[3]
#Title["Spellcard 2"]
#Text[" Moriya Sign - \" War of the Gods\""]
#System["./System.txt"]
#Background["./Background.dnh"]

let bossObj;
let BossX = 0;
let BossY = 0;
let shot = GetCurrentScriptDirectory ~ "se/Shot1.wav";
let wave = GetCurrentScriptDirectory ~ "se/BulletWave.wav";
let barrage = GetCurrentScriptDirectory ~ "se/barrage.wav";
let spelloff = GetCurrentScriptDirectory ~ "se/SpellOff.wav";
let imgBoss = GetCurrentScriptDirectory ~ "img/Kanako.png";

#include "./shot/Default_ShotConst.txt"
#include "./Render.dnh"

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

	//  Defines a Boss and Registers it.
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	
	Render;
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	//Textures the Boss and Indicates the Boss's True Center.
	ObjPrim_SetTexture(bossObj,imgBoss);
	ObjSprite2D_SetDestCenter(bossObj);
	
	// Moves Boss to XY Location at wanted Speed (Parameters:Obj,X,Y,V)
	ObjMove_SetDestAtWeight(bossObj,GetScreenWidth/2,GetScreenHeight/2,15,10);
	
	mainTask; //Runs Maintask
	
}

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

}

@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,20);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,BossX,BossY,20);
	
	yield;

}

@Finalize{
}

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


task mainTask{
	end;
	wait(80);
	CutIn("Moriya Sign - \"War of the Gods\"");
	let sceneObj = GetEnemyBossSceneObjectID;
	ObjEnemyBossScene_StartSpell(sceneObj);
	wait(80);
	ConcentrationQ;
	wait(70);
	fire;
	fire2;
	fire3;
	fire4;
}

task fire{
		let dir = 0;
		let x = 0;
		let i = 0;
		let a = 2;
		let spin = 0;
		loop{
			ascent(i in 0..40){
					while(x<a && !Obj_IsDeleted(bossObj)){
						Play(shot);
						reflectshot(-30+spin,BossY-30*sin(spin),0,dir-90,0.1,0,0,41,30);

						reflectshot(-30+spin,BossY+30*sin(spin),0,dir+90,0.1,0,0,44,30);
						
						x++;
						dir+=360/a;
						spin+=14;
					}
					wait(1);
					x = 0;
					dir = 0;
				}
					spin = 0;
			wait(240);
		}
}


task fire2{
		wait(720);
		let dir = 0;
		let x = 0;
		let i = 0;
		let a = 2;
		let spin = 0;
		loop{
			ascent(i in 0..30){
					while(x<a && !Obj_IsDeleted(bossObj)){
						Play(shot);
						reflectshot(670-spin,(BossY-50)+40*sin(spin),0,dir-90,0.1,0,0,45,30);

						reflectshot(670-spin,(BossY-50)-40*sin(spin),0,dir+90,0.1,0,0,46,30);
						
						x++;
						dir+=360/a;
						spin+=16;
					}
					wait(2);
					x = 0;
					dir = 0;
			}
					spin = 0;
			wait(230);
		}
}

task fire3{
		wait(1440);
		let dir = 0;
		let x = 0;
		let i = 0;
		let a = 2;
		let spin = 0;
		loop{
			ascent(i in 0..28){
					while(x<a && !Obj_IsDeleted(bossObj)){
						Play(shot);
						reflectshot(-30+spin,(BossY-50)-20*sin(spin),0,dir-90,0.1,0,0,90,30);

						reflectshot(-30+spin,(BossY-50)+20*sin(spin),0,dir+90,0.1,0,0,91,30);
						
						x++;
						dir+=360/a;
						spin+=18;
					}
					wait(2);
					x = 0;
					dir = 0;
			}
					spin = 0;
			wait(240);
		}
}

task fire4{
		wait(2130);
		let dir = 0;
		let x = 0;
		let i = 0;
		let a = 2;
		let spin = 0;
		loop{
			ascent(i in 0..28){
					while(x<a && !Obj_IsDeleted(bossObj)){
						Play(shot);
						reflectshot(670-spin,(BossY-75)+30*sin(spin),0,dir-90,0.1,0,0,93,30);

						reflectshot(670-spin,(BossY-75)-30*sin(spin),0,dir+90,0.1,0,0,95,30);
						
						x++;
						dir+=360/a;
						spin+=16;
					}
					wait(2);
					x = 0;
					dir = 0;
			}
					spin = 0;
			wait(230);
		}
}
		
		task reflectshot(x,y,v,dir,acc,ang,max,grap,del){
						let obj = CreateShotA1(x,y,v,dir,grap,del);
						
						ObjRender_SetPosition(obj,x,y,0);
						ObjMove_SetSpeed(obj,v);
						ObjMove_SetAngle(obj,dir);
						ObjMove_SetAcceleration(obj,acc);
						ObjMove_SetAngularVelocity(obj,ang);
						ObjMove_SetMaxSpeed(obj,max);
						ObjShot_SetGraphic(obj,grap);
						ObjShot_SetDelay(obj,del);
						
						if(!Obj_IsDeleted(obj)){
							
							wait(120);
							Play(wave);
							ObjMove_SetAcceleration(obj,0.1);
							ObjMove_SetMaxSpeed(obj,3);
						}
		}
	

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());
}