#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Dj spell 05 lunatic"]
#Text["Dj spell 05 lunatic"]

// tex
let snijEff = "script/thdcs/system/effects/circledj.png";
let dummyTex = "";
let famTex = "script/thdcs/system/effects/fammer.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);
	
	SetCommonData("bossCircleType",3);
	SetCommonData("bossAttackType",2);
	SetCommonData("bossBgType",3);
	if(GetAreaCommonData("system","gameLang",0) == 0) { SetCommonData("cardName","rj[@uv"); } 
	if(GetAreaCommonData("system","gameLang",0) == 1) { SetCommonData("cardName","Vinyl uExplosive theatrev"); } 
	setInvulnerableTime(bossObj,360);
		
	ObjMove_SetDestAtSpeed(bossObj,192,GetCenterY,5);
	SetAutoDeleteObject(true);
	SetShotAutoDeleteClip(128,128,128,128);

	mainTask;
} 

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

@MainLoop { yield; } 
@Finalize { } 


task mainTask { 
	yield;
	scriptEnding;
	fireRecords;
} 

// Animation to warn the disc movement
task warningFX {
	let r = 240;
	let dir = 0;
	ascent(i in 0..72) { 
		ascent(j in 0..5) {
			let effShot = CreateShotA(GetEnemyX+r*cos(dir),GetEnemyY+r*sin(dir),15);
			SetShotDataA(effShot,0,0,dir,0,0,0,173);
			DeleteShotA(effShot,0);
			dir+=360/5;
		}
		dir+=360/72;
		r-=240/72;
		wait(1);
	}
}

task warningFX2 {
	let r = 0;
	let dir = 0;
	ascent(i in 0..72) { 
		ascent(j in 0..5) {
			let effShot = CreateShotA(GetEnemyX+r*cos(dir),GetEnemyY+r*sin(dir),15);
			SetShotDataA(effShot,0,0,dir,0,0,0,173);
			DeleteShotA(effShot,0);
			dir+=360/5;
		}
		dir+=360/72;
		r+=240/72;
		wait(1);
	}
}

// Fires the records spinning around. 
task fireRecords {
	let r = 0;
	let dir = 0;
	let richting = 0;
	wait(120);
	ascent(i in 0..7) {
		recordBullet(GetEnemyX+r*cos(dir),GetEnemyY+r*sin(dir),0,dir,251,15,richting);
		richting++;
		dir+=360/7;
		if(richting == 2) { richting = 0; }  
	}
}

// Slow moving record bullets. Has fake fam for blocking player bullets.
task recordBullet(x,y,v,dir,kleur,delay,spin) {
	let obj = ObjShot_Create(OBJ_SHOT);
	let obj2 = ObjEnemy_Create(OBJ_ENEMY); 
	let amuKleur = 101;
		
	// shot overlapping the fam
	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,true);
	Obj_SetRenderPriorityI(obj,30);
	
	// invis familiar
	ObjEnemy_Regist(obj2);
	ObjPrim_SetTexture(obj2,dummyTex); 
	ObjRender_SetScaleXYZ(obj2,1,1,0);
	ObjEnemy_SetLife(obj2,999999);
	ObjMove_SetPosition(obj2,x,y);	
	Obj_SetValue(obj2,"isFamiliar",true); 
	
	recordColl(obj,obj2);
	recordHandle(obj,obj2,dir);
	recordRandom(obj);

	ascent(i in 0..9) { 	
		recordAmulets(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),90,15,spin,amuKleur);	
		amuKleur++;		
		if(amuKleur == 103) { amuKleur = 101; } 
		wait(16);
	}
}

// Random triggered explosive timer.
task recordRandom(obj) {
	let trigger = 0;
	let dir2 = 0;
	wait(240);	
	while(!Obj_IsDeleted(obj)) {
		trigger = rand_int(2,8);
		if(trigger == 5) { 
			PlaySFX(SFX_TICK00);
			ascent(i in 0..4) {
				ObjShot_SetGraphic(obj,248);
				wait(15);
				PlaySFX(SFX_TICK01);
				ObjShot_SetGraphic(obj,251);
				wait(15);
			}
			ascent(i in 0..8) {
				ObjShot_SetGraphic(obj,248);
				wait(3);
				PlaySFX(SFX_TICK01);
				ObjShot_SetGraphic(obj,251);
				wait(3);
			}	
			ObjShot_SetGraphic(obj,248);	
			PlaySFX(SFX_TAN01);
			PlaySFX(SFX_KILL);
			exploEffect(ObjMove_GetX(obj),ObjMove_GetY(obj),10,0.8);
			ascent(j in 0..10) { 
				CreateShotA1(ObjMove_GetX(obj)+1*cos(dir2),ObjMove_GetY(obj)+1*sin(dir2),1.0,dir2+rand_int(-8,8),109,0);
				CreateShotA1(ObjMove_GetX(obj)+1*cos(dir2),ObjMove_GetY(obj)+1*sin(dir2),1.3,dir2+rand_int(-8,8),109,0);
				CreateShotA1(ObjMove_GetX(obj)+1*cos(dir2),ObjMove_GetY(obj)+1*sin(dir2),1.6,dir2+rand_int(-8,8),109,0);
				CreateShotA1(ObjMove_GetX(obj)+1*cos(dir2),ObjMove_GetY(obj)+1*sin(dir2),1.9,dir2+rand_int(-8,8),109,0);
				dir2+=rand_int(34,40);
			}
			wait(60);
			ObjShot_SetGraphic(obj,251);		
			wait(60);
		}
		wait(60);
	}
}

// Record bullet behaviour handler
task recordHandle(obj,obj2,dir) { 
	let r = 0;
	let c = 0;
	let sw = 3;
	wait(120);
	warningFX2;
	PlaySFX(SFX_CH00); 
	concenEffect(bossObj,15,1,128,128,128);
	concenEffect(bossObj,5,5,64,64,255);
	concenEffect(bossObj,15,1,64,255,64);		
	PlaySFX(SFX_OPTION);	
	while(!Obj_IsDeleted(obj)) {
		if(sw == 3) { if(r < 200) { r++; } if(r == 200) { sw = 0; } }
		ObjMove_SetPosition(obj,GetEnemyX+r*cos(dir),GetEnemyY+r*sin(dir));
		ObjMove_SetPosition(obj2,GetEnemyX+r*cos(dir),GetEnemyY+r*sin(dir));
		ObjRender_SetPosition(obj2,GetEnemyX+r*cos(dir),GetEnemyY+r*sin(dir),0);
		dir+=0.5;
		if(c == 500) { warningFX; }
		if(c == 600) { 
			PlaySFX(SFX_CH00); 
			concenEffect(bossObj,15,1,128,128,128);
			concenEffect(bossObj,5,5,64,64,255);
			concenEffect(bossObj,15,1,64,255,64);			
		}
		if(c == 800) { warningFX2; }
		if(c == 900) { 
			PlaySFX(SFX_CH00); 
			concenEffect(bossObj,15,1,128,128,128);
			concenEffect(bossObj,5,5,64,64,255);
			concenEffect(bossObj,15,1,64,255,64);			
		}		
		if(c > 600 && sw == 0) { if(r > 80) { r-=0.5; } if(r == 80) { sw = 1; } }
		if(c > 900 && sw == 1) { if(r < 200) { r+=0.5; } if(r == 200) { sw = 0; c = 0; } }
		c++; 
		yield;
	}
	Obj_DeleteValue(obj2,"isFamiliar"); 
}

// Record bullet collision handler
task recordColl(obj,obj2) { 
	while(!Obj_IsDeleted(obj)) {
		ObjShot_SetIntersectionCircleA1(obj,25);
		ObjEnemy_SetIntersectionCircleToShot(obj2,ObjMove_GetX(obj2),ObjMove_GetY(obj2),25);
		if(ObjEnemy_GetInfo(obj2,INFO_SHOT_HIT_COUNT) > 0) { PlaySFX(SFX_NODAMAGE); }	
		yield;
	}
}

// Amulets spawned with record, revolving.
task recordAmulets(obj2,x,y,dir,delay,spin,kleur) {
	let obj = ObjShot_Create(OBJ_SHOT);
	let r = 0;
	
	ObjShot_Regist(obj);
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetSpeed(obj,0);
	ObjMove_SetAngle(obj,dir);
	ObjShot_SetGraphic(obj,kleur);
	ObjShot_SetDelay(obj,delay);
	ObjShot_SetSpellResist(obj,true);
	
	while(!Obj_IsDeleted(obj2)) {
		if(r < 64) { r++; } 
		//if(r >= 64) { r+=0.1; }
		ObjMove_SetPosition(obj,ObjMove_GetX(obj2)+r*cos(dir),ObjMove_GetY(obj2)+r*sin(dir));
		ObjMove_SetAngle(obj,dir+90);
		if(spin == 0) { dir-=2.5; }
		if(spin == 1) { dir+=2.5; }
		yield;
	}
}

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

	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	Obj_Delete(bossObj);

	CloseScript(GetOwnScriptID());
}
 