#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Nonspell 2"]
#Text[" Kanako Yasaka "]
#System["./System.txt"]
#Background["./Background.dnh"]

let bossObj;
let BossX = 0;
let BossY = 0;
let shot = GetCurrentScriptDirectory ~ "se/Shot1.wav";
let shot2 = GetCurrentScriptDirectory ~ "se/Shot2.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";
let px = GetPlayerX;
let py = GetPlayerY;
let ex = BossX;
let ey = BossY;

#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;
	Play(spelloff);
	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-128,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(3000);
		}
		case(EV_REQUEST_TIMER) {
			SetScriptResult(30);
		}

}

@MainLoop{
	// MainLoop: Runs throughout the script.
	
	// Indicates that BossX = GetEnemyX and BossY = GetEnemyY
	BossX = ObjMove_GetX(bossObj);
	BossY = ObjMove_GetY(bossObj);
	let px = GetPlayerX;
	let py = GetPlayerY;
	let ex = BossX;
	let ey = BossY;
	
	// 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);
	ConcentrationQ;
	wait(80);
	Onbashira;
	wait(10);
}

task Onbashira{
	let onba = GetCurrentScriptDirectory ~ "img/Onbashira.png";
	let alpha = 0;
	let angletoplayer = atan2(py - ey, px - ex);
	
	let onbaObj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(onbaObj,onba);
	ObjSprite2D_SetSourceRect(onbaObj,0,0,72,64);
	ObjSprite2D_SetDestRect(onbaObj,0,0,72,64);
	ObjSprite2D_SetDestCenter(onbaObj);
	Obj_SetRenderPriorityI(onbaObj,31);
	ObjRender_SetAlpha(onbaObj,0);
	ObjRender_SetScaleXYZ(onbaObj,1,1,0);

	let onbaObj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(onbaObj2,onba);
	ObjRender_SetAngleY(onbaObj2,180);
	ObjSprite2D_SetSourceRect(onbaObj2,0,0,72,64);
	ObjSprite2D_SetDestRect(onbaObj2,0,0,72,64);
	ObjSprite2D_SetDestCenter(onbaObj2);
	Obj_SetRenderPriorityI(onbaObj2,31);
	ObjRender_SetAlpha(onbaObj2,0);
	ObjRender_SetScaleXYZ(onbaObj2,1,1,0);
	
	ObjRender_SetPosition(onbaObj,BossX-32,BossY,0);
	ObjRender_SetPosition(onbaObj2,BossX+32,BossY,0);
	Play(barrage);
	loop(25.5){
		ObjRender_SetAlpha(onbaObj,alpha);
		ObjRender_SetAlpha(onbaObj2,alpha);
		alpha+=10;
		yield;
	}
	wait(15);
	ConcentrationQ;
	wait(80);
	fire;
	fire2;
	fire3;
	loop{
	ObjRender_SetPosition(onbaObj,BossX-32,BossY,0);
	ObjRender_SetPosition(onbaObj2,BossX+32,BossY,0);
	yield;
	}
	
	task fire{
		angletoplayer = atan2(py - ey, px - ex);
		let x = 0;
		let dir = 0;
		let spin = 0;
		loop{
		ascent(i in 0..20){
			while(x<12){
				Play(shot);
				let obj = CreateShotA1(ObjRender_GetX(onbaObj)-36,ObjRender_GetY(onbaObj)-32,3,dir,41,10);
				ObjMove_AddPatternA3(obj,0,6,dir-215,-0.25,0,0,41);
				ObjMove_AddPatternA3(obj,60,6,dir-i*13.5,-0.25,i/640,3,41);
				
				let obj1 = CreateShotA1(ObjRender_GetX(onbaObj2)+36,ObjRender_GetY(onbaObj2)-32,3,dir,41,10);
				ObjMove_AddPatternA3(obj1,0,6,dir+215,-0.25,0,0,41);
				ObjMove_AddPatternA3(obj1,60,6,dir+i*13.5,-0.25,-i/640,3,41);
						
				spin+=21/21;
				dir+=196/12;
				x++;
			}
			dir = 0;
			x = 0;
			wait(3);
		}
		wait(60);
		}
	}
	
	task fire2{
		let x = 0;
		let dir = 0;
		let spin = 0;
		loop{
		ascent(i in 0..5){
			while(x<12){
			Play(shot);
				onbashot(ObjRender_GetX(onbaObj)-36,ObjRender_GetY(onbaObj)-16,6,dir-215,-0.25,0,44,10);
				
				onbashot(ObjRender_GetX(onbaObj2)+36,ObjRender_GetY(onbaObj2)-16,6,dir+215,-0.25,0,44,10);
						
				spin+=(196/12)/12;
				dir+=196/12;
				x++;
			}
			dir = 0;
			x = 0;
			wait(6);
		}
		wait(55);
		}
	}
	
	task fire3{
		let angle = 0;
		let spin = 0;
		
		loop{
		Play(shot2);
			CreateShotA1(BossX-8,BossY-8,4,155+angle,46,10);
			CreateShotA1(BossX-8,BossY,4,145+angle,46,10);
			CreateShotA1(BossX-8,BossY+8,4,135+angle,46,10);
			
			CreateShotA1(BossX+8,BossY-8,4,25-angle,46,10);
			CreateShotA1(BossX+8,BossY,4,35-angle,46,10);
			CreateShotA1(BossX+8,BossY+8,4,45-angle,46,10);
			spin+=1;
			angle = 25*sin(spin*6);
			wait(5);
		}
	}
	
	task onbashot(x,y,v,dir,acc,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_SetMaxSpeed(obj,max);
			ObjShot_SetGraphic(obj,grap);
			ObjShot_SetDelay(obj,del);
			
			if(!Obj_IsDeleted(obj)){
				wait(60);
				Play(wave);
				ObjMove_SetAngle(obj,atan2(GetPlayerY - ObjRender_GetY(obj), GetPlayerX - ObjRender_GetX(obj)));
				ObjMove_SetSpeed(obj,6);
				ObjMove_SetAcceleration(obj,0);
			}
		}
	
}

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

	CloseScript(GetOwnScriptID());
}