#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Dj spell 03 normal"]
#Text["Dj spell 03 normal"]

let globalObjectContainer = [];

let volObj = ObjShot_Create(OBJ_STRAIGHT_LASER);
let volObj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);
let volObj3 = ObjShot_Create(OBJ_STRAIGHT_LASER);
let volObj4 = ObjShot_Create(OBJ_STRAIGHT_LASER);
let volObj5 = ObjShot_Create(OBJ_STRAIGHT_LASER);
let volObj6 = ObjShot_Create(OBJ_STRAIGHT_LASER);

let arrowObj = ObjPrim_Create(OBJ_SPRITE_2D);
let arrowObj2 = ObjPrim_Create(OBJ_SPRITE_2D);
let arrowObj3 = ObjPrim_Create(OBJ_SPRITE_2D);
let arrowObj4 = ObjPrim_Create(OBJ_SPRITE_2D);
let arrowObj5 = ObjPrim_Create(OBJ_SPRITE_2D);
let arrowObj6 = ObjPrim_Create(OBJ_SPRITE_2D);

let voltrack = ObjText_Create;
let voltrack2 = ObjText_Create;

let snijEff = "script/thdcs/system/effects/circledj.png";
let arrowEff = "script/thdcs/system/effects/animarrow.png";
	
#include "script/thdcs/system/shotlibrary.txt"
#include "script/thdcs/functions/function_common.txt"
#include "script/thdcs/functions/function_global.txt"
#include "script/thdcs/functions/function_bosseff.txt"

@Initialize {
	SetCommonData("spellScriptID",GetOwnScriptID);
	renderBoss(3);
	setAura(bossObj,64,255,128);
	SetCommonData("bossCircleType",3);
	SetCommonData("bossAttackType",2);
	SetCommonData("bossBgType",3);
	if(GetAreaCommonData("system","gameLang",0) == 0) { SetCommonData("cardName","@uTEh`FbNv"); } 
	if(GetAreaCommonData("system","gameLang",0) == 1) { SetCommonData("cardName","Tuning uSound Checkv"); } 
	setInvulnerableTime(bossObj,500);
	
	ObjMove_SetDestAtSpeed(bossObj,192,120,5);
	SetShotAutoDeleteClip(16,16,16,16);
	SetAutoDeleteObject(true);
	mainTask;
	
	ObjRender_SetPosition(voltrack,30,100,0);
	ObjRender_SetPosition(voltrack2,30,120,0);
} 

@Event { 
	alternative(GetEventType()) 
	case(EV_REQUEST_LIFE) { 
		SetScriptResult(350);
	}
	case(EV_REQUEST_TIMER) { 
		SetScriptResult(72);
	}
	case(EV_REQUEST_SPELL_SCORE) {
	    let spellCardBonus = calculateAndGetSpellCardBonus();
		SetScriptResult(spellCardBonus);
	}	
	case(EV_USER+9999) {
		SetCommonData("spellcardID","dj03norm"); 
	}
} 

@MainLoop { yield; } 
@Finalize { } 


task mainTask { 
	yield;
	scriptEnding;
	PlaySFX(SFX_ALERT);
	control;
	beweeg;
	wait(140);
	tuning;
	tuning2;
	tuning3;
	wait(60);
	protune;
} 

// sfx
task swooshSFX {
	wait(60);
	PlaySFX(SFX_SWOOSH1);
	wait(90);
	PlaySFX(SFX_KIRA01);	
}

task swooshSFX2 {
	wait(60);
	PlaySFX(SFX_SWOOSH1);
	wait(90);
	PlaySFX(SFX_KIRA01);	
}

// move around randomly
task beweeg {
	wait(180);
	let dir = 0;
	let kleur = 239;
	while(!Obj_IsDeleted(bossObj)) {  
		ObjMove_RandomPosWeight(bossObj,10,10,15,30,GetClipMinX+128,GetClipMinY+96,GetClipMaxX-96,GetCenterY-64);
		wait(90);
		PlaySFX(SFX_KIRA00);
		ascent(i in 0..24) {
			CreateShotA1(bossX+12*cos(dir),bossY+12*sin(dir),1,dir,kleur,15);
			dir+=360/24;
		}
		kleur++;
		dir = 0;
		if(kleur == 246) { kleur = 239; }
		ObjMove_SetDestAtWeight(bossObj,GetPlayerX,120+rand_int(-20,20),20,35);
		wait(180);
	}
}

// summon the volume buttons
task control {
	//dummyEff(arrowObj,32,GetClipMinX+32); ObjRender_SetAlpha(arrowObj,255);	
	//dummyEff(arrowObj6,362,GetClipMinX+32); ObjRender_SetAlpha(arrowObj6,255);	
	//dummyEff(arrowObj2,98,GetClipMinX+32); ObjRender_SetAlpha(arrowObj2,255);	
	//dummyEff(arrowObj5,296,GetClipMinX+32); ObjRender_SetAlpha(arrowObj5,255);	
	//dummyEff(arrowObj3,164,GetClipMinX+32); ObjRender_SetAlpha(arrowObj3,255);	
	//dummyEff(arrowObj4,230,GetClipMinX+32); ObjRender_SetAlpha(arrowObj4,255);	
	volumeControl(volObj,0,GetClipMaxY);
	volumeBarAnim(volObj);
	volumeControl(volObj6,330,GetClipMaxY);
	volumeBarAnim(volObj6);	
	wait(15);
	volumeControl(volObj2,66,GetClipMaxY);
	volumeBarAnim(volObj2);	
	volumeControl(volObj5,264,GetClipMaxY);
	volumeBarAnim(volObj5);		
	wait(15);
	volumeControl(volObj3,132,GetClipMaxY);
	volumeBarAnim(volObj3);		
	volumeControl(volObj4,198,GetClipMaxY);	
	volumeBarAnim(volObj4);		
}

// Tuning the volume buttons, initial movement summon
task tuning {
	PlaySFX(SFX_SWOOSH);
	ascent(i in 0..128) {
			ObjMove_SetY(volObj,ObjMove_GetY(volObj)-3);
			ObjMove_SetY(volObj6,ObjMove_GetY(volObj6)-3);
			wait(1);
	}
	swooshSFX;
	vuur; vuur6;
}
task tuning2 { 
	wait(30);
	PlaySFX(SFX_SWOOSH);
	ascent(i in 0..128) {
			ObjMove_SetY(volObj2,ObjMove_GetY(volObj2)-3);
			ObjMove_SetY(volObj5,ObjMove_GetY(volObj5)-3);
			wait(1);
	}	
}
task tuning3 { 
	wait(60);
	PlaySFX(SFX_SWOOSH);
	ascent(i in 0..128) {
			ObjMove_SetY(volObj3,ObjMove_GetY(volObj3)-3);
			ObjMove_SetY(volObj4,ObjMove_GetY(volObj4)-3);
			wait(1);
	}
}

// =============================================================================
task protune {
	ObjRender_SetAlpha(arrowObj,0);	ObjRender_SetAlpha(arrowObj6,0);	
	ObjRender_SetAlpha(arrowObj2,0);	ObjRender_SetAlpha(arrowObj5,0);	
	ObjRender_SetAlpha(arrowObj3,0);	ObjRender_SetAlpha(arrowObj4,0);	
	wait(240);
	while(!Obj_IsDeleted(bossObj)) { 
		PlaySFX(SFX_SWOOSH);
		ascent(i in 0..32) {
				ObjMove_SetY(volObj,ObjMove_GetY(volObj)+6); 
				ObjMove_SetY(volObj6,ObjMove_GetY(volObj6)+6); 	
				wait(1);
		}
		swooshSFX;
		vuur; vuur6;
		wait(120);
		vuur2; vuur5;	
		PlaySFX(SFX_SWOOSH);	
		ascent(i in 0..32) {
				ObjMove_SetY(volObj,ObjMove_GetY(volObj)-6); 
				ObjMove_SetY(volObj6,ObjMove_GetY(volObj6)-6); 	
				wait(1);
		}	
		swooshSFX2;
		vuur3; vuur5;
		wait(90);
		PlaySFX(SFX_SWOOSH);
		ascent(i in 0..64) {
				ObjMove_SetY(volObj,ObjMove_GetY(volObj)-0.5); 
				ObjMove_SetY(volObj2,ObjMove_GetY(volObj2)+1); 	
				ObjMove_SetY(volObj3,ObjMove_GetY(volObj3)+2); 				
				ObjMove_SetY(volObj4,ObjMove_GetY(volObj4)+3);
				ObjMove_SetY(volObj5,ObjMove_GetY(volObj5)+4);
				ObjMove_SetY(volObj6,ObjMove_GetY(volObj6)+5);
				wait(1);
		}	
		loop(2) {
			swooshSFX;
			vuur; wait(15);
			vuur3; wait(15);
			vuur5; wait(15);
			swooshSFX2;
			wait(90);
		}
		wait(120);
		PlaySFX(SFX_SWOOSH);
		ascent(i in 0..64) {
				ObjMove_SetY(volObj,ObjMove_GetY(volObj)+0.5); 
				ObjMove_SetY(volObj2,ObjMove_GetY(volObj2)-1); 	
				ObjMove_SetY(volObj3,ObjMove_GetY(volObj3)-2); 				
				ObjMove_SetY(volObj4,ObjMove_GetY(volObj4)-3);
				ObjMove_SetY(volObj5,ObjMove_GetY(volObj5)-4);
				ObjMove_SetY(volObj6,ObjMove_GetY(volObj6)-5);
				wait(1);
		}
		swooshSFX;
		vuur2; vuur4; vuur6;
		wait(120);
		vuurbarrage;
		PlaySFX(SFX_SWOOSH);
		ascent(i in 0..160) {
				ObjMove_SetY(volObj,ObjMove_GetY(volObj)+1); 
				ObjMove_SetY(volObj2,ObjMove_GetY(volObj2)+1); 	
				ObjMove_SetY(volObj3,ObjMove_GetY(volObj3)+2); 				
				ObjMove_SetY(volObj4,ObjMove_GetY(volObj4)+2);
				ObjMove_SetY(volObj5,ObjMove_GetY(volObj5)+1);
				ObjMove_SetY(volObj6,ObjMove_GetY(volObj6)+1);
				wait(1);
		}
		PlaySFX(SFX_SWOOSH);
		ascent(i in 0..80) {
				ObjMove_SetY(volObj,ObjMove_GetY(volObj)-2); 
				ObjMove_SetY(volObj2,ObjMove_GetY(volObj2)-2); 	
				ObjMove_SetY(volObj3,ObjMove_GetY(volObj3)-4); 				
				ObjMove_SetY(volObj4,ObjMove_GetY(volObj4)-4);
				ObjMove_SetY(volObj5,ObjMove_GetY(volObj5)-2);
				ObjMove_SetY(volObj6,ObjMove_GetY(volObj6)-2);
				wait(1);
		}
		vuurbarrage2;	
		wait(320);
	}
}

// Fire barrage 1
task vuurbarrage { 
	wait(30);
	loop(3) { 
		swooshSFX;
		vuur; vuur4; vuur5;
		wait(120);
	}
}

// Fire barrage 2
task vuurbarrage2 { 
	wait(30);
	swooshSFX2;
	vuur; vuur6; wait(120);
	vuur2; vuur5; wait(120);
	vuur3; vuur4; wait(120);
}

// Amulet shots which will fire sideway amulets.
task vuur {  
	PlaySFX(SFX_OPTION);
	ObjRender_SetAlpha(arrowObj,255);	
	wait(75);
	ascent(i in 0..(GetClipMaxY-ObjMove_GetY(volObj))/21) {		
		volumeAmulet(ObjMove_GetX(volObj)+32,i,0,0,103,10);
		wait(1);
	}	
	ObjRender_SetAlpha(arrowObj,0);
}
task vuur6 {  
	PlaySFX(SFX_OPTION);
	ObjRender_SetAlpha(arrowObj6,255);	
	wait(75);		
	ascent(i in 0..(GetClipMaxY-ObjMove_GetY(volObj6))/21) {		
		volumeAmulet(ObjMove_GetX(volObj6)+32,i,0,0,103,10);
		wait(1);
	}	
	ObjRender_SetAlpha(arrowObj6,0);
}
task vuur2 { 
	PlaySFX(SFX_OPTION); 	
	ObjRender_SetAlpha(arrowObj2,255);	
	wait(75);	
	ascent(i in 0..(GetClipMaxY-ObjMove_GetY(volObj2))/21) {		
		volumeAmulet(ObjMove_GetX(volObj2)+32,i,0,0,103,10);
		wait(1);
	}	
	ObjRender_SetAlpha(arrowObj2,0);	
}
task vuur5 {  
	PlaySFX(SFX_OPTION);
	ObjRender_SetAlpha(arrowObj5,255);	
	wait(75);		
	ascent(i in 0..(GetClipMaxY-ObjMove_GetY(volObj5))/21) {		
		volumeAmulet(ObjMove_GetX(volObj5)+32,i,0,0,103,10);
		wait(1);
	}	
	ObjRender_SetAlpha(arrowObj5,0);
}
task vuur3 {  	
	PlaySFX(SFX_OPTION);
	ObjRender_SetAlpha(arrowObj3,255);	
	wait(75);	
	ascent(i in 0..(GetClipMaxY-ObjMove_GetY(volObj3))/21) {		
		volumeAmulet(ObjMove_GetX(volObj3)+32,i,0,0,103,10);
		wait(1);
	}	
	ObjRender_SetAlpha(arrowObj3,0);	
}
task vuur4 {
	PlaySFX(SFX_OPTION); 
	ObjRender_SetAlpha(arrowObj4,255);	
	wait(75); 		
	ascent(i in 0..(GetClipMaxY-ObjMove_GetY(volObj4))/21) {		
		volumeAmulet(ObjMove_GetX(volObj4)+32,i,0,0,103,10);
		wait(1);
	}	
	ObjRender_SetAlpha(arrowObj4,0);	
}

// =============================================================================
// volume button task
task volumeControl(obj,x,y) {
	ObjShot_Regist(obj);
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetSpeed(obj,0);
	ObjMove_SetAngle(obj,0);
	ObjStLaser_SetAngle(obj,0);
	ObjShot_SetGraphic(obj,267);
	ObjShot_SetDelay(obj,0);
	ObjLaser_SetLength(obj,64);
	ObjLaser_SetRenderWidth(obj,10);
	ObjLaser_SetIntersectionWidth(obj,10);
	ObjStLaser_SetSource(obj,false);
	ObjShot_SetSpellResist(obj,true);
	ObjLaser_SetInvalidLength(obj,0.0,0.0);
}

// Volume amulets 
task volumeAmulet(x,row,v,dir,kleur,delay) {
	PlaySFX(SFX_TAN00);
	let obj = ObjShot_Create(OBJ_SHOT);
	let beginY = GetClipMaxY - 16;
	let c = 0;
	
	ObjShot_Regist(obj);
	ObjMove_SetPosition(obj,x,beginY-row*20);
	ObjMove_SetSpeed(obj,v);
	ObjMove_SetAngle(obj,dir);
	ObjShot_SetGraphic(obj,kleur);
	ObjShot_SetDelay(obj,delay);
	ObjShot_SetSpellResist(obj,true);
	
	sidewayAmulet(obj);
	
	while(!Obj_IsDeleted(obj)) {
		if(IsIntersected_Obj_Obj(volObj,obj)) { Obj_Delete(obj); } 
		if(IsIntersected_Obj_Obj(volObj2,obj)) { Obj_Delete(obj); } 
		if(IsIntersected_Obj_Obj(volObj3,obj)) { Obj_Delete(obj); } 
		if(IsIntersected_Obj_Obj(volObj4,obj)) { Obj_Delete(obj); } 
		if(IsIntersected_Obj_Obj(volObj5,obj)) { Obj_Delete(obj); } 
		if(IsIntersected_Obj_Obj(volObj6,obj)) { Obj_Delete(obj); } 
		if(c == 90) { Obj_Delete(obj); } 
		c++;
		yield;
	}
}

// Summons the sideway amulets in a mini task
task sidewayAmulet(obj) {
	wait(60);
	volumeAmulet2(ObjMove_GetX(obj),ObjMove_GetY(obj),0.5,0,96,30);
	volumeAmulet2(ObjMove_GetX(obj),ObjMove_GetY(obj)+8,0.5,180,99,30);
}

// Sideway volume amulets
task volumeAmulet2(x,y,v,dir,kleur,delay) {
	let obj = ObjShot_Create(OBJ_SHOT);
	
	ObjShot_Regist(obj);
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetSpeed(obj,v);
	ObjMove_SetAngle(obj,dir);
	ObjShot_SetGraphic(obj,kleur);
	ObjShot_SetDelay(obj,delay);
	ObjShot_SetSpellResist(obj,false);
	
	while(!Obj_IsDeleted(obj)) {

		yield;
	}
}

task volumeBarAnim(obj2) {
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let snijl = 150 / 2;
	let snijd = 16;
	let glide = 0;
	let c = 0;
	
	ObjPrim_SetTexture(obj,snijEff);	
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj,0.3);	
	ObjRender_SetPosition(obj,ObjMove_GetX(obj2)+32,GetCenterY,0);
	ObjRender_SetAngleZ(obj,0);
	ObjPrim_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj,4);                                                                                                                                                                  
	ObjRender_SetAlpha(obj,155);

	globalObjectContainer = globalObjectContainer ~ [obj];

	while(! Obj_IsDeleted(obj)) {
		ObjPrim_SetVertexPosition(obj, 0, -snijd, ObjMove_GetY(obj2)-GetClipMaxY/2,0);
		ObjPrim_SetVertexUVT(obj, 0, 38, 0+glide);	
		ObjPrim_SetVertexPosition(obj, 1, snijd, ObjMove_GetY(obj2)-GetClipMaxY/2,0);
		ObjPrim_SetVertexUVT(obj, 1, 54, 0+glide);
		ObjPrim_SetVertexPosition(obj, 2, -snijd,GetClipMaxY,0);
		ObjPrim_SetVertexUVT(obj, 2, 38, 128+glide);	
		ObjPrim_SetVertexPosition(obj, 3, snijd,GetClipMaxY,0);
		ObjPrim_SetVertexUVT(obj, 3, 54, 128+glide);	

		glide+=2;
		yield;
	}
	Obj_Delete(obj);
}

// Dummy for concen effect
task dummyEff(obj,x,y) {
	let dummyframe = 0;
	
	ObjPrim_SetTexture(obj,arrowEff);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	Obj_SetRenderPriorityI(obj,30);
	ObjRender_SetPosition(obj,x,y,0);
	ObjMove_SetPosition(obj,x,y);
	ObjRender_SetAlpha(obj,0);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjSprite2D_SetSourceRect(obj,0,0,40,64);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAngleZ(obj,180);

	while(! Obj_IsDeleted(obj)) {
		if(dummyframe < 10) { ObjSprite2D_SetSourceRect(obj,0,0,40,64); }
		if(dummyframe >= 10 && dummyframe < 20) { ObjSprite2D_SetSourceRect(obj,40,0,80,64); }
		if(dummyframe >= 20 && dummyframe < 30) { ObjSprite2D_SetSourceRect(obj,80,0,120,64); } 
		if(dummyframe >= 30 && dummyframe < 40) { ObjSprite2D_SetSourceRect(obj,0,64,40,128); }
		if(dummyframe >= 40 && dummyframe < 50) { ObjSprite2D_SetSourceRect(obj,40,64,80,128); }
		if(dummyframe >= 50) { ObjSprite2D_SetSourceRect(obj,80,64,120,128); }
		dummyframe++;
		if(dummyframe == 60) { dummyframe = 0; } 
		yield;
	}
}

task scriptEnding {
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}

    ascent(i in 0..length(globalObjectContainer)) {
        Obj_Delete(globalObjectContainer[i]);
    }
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	Obj_Delete(bossObj);

	CloseScript(GetOwnScriptID());
}
 