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

#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","rj[˕Ԃ"); } 
	if(GetAreaCommonData("system","gameLang",0) == 1) { SetCommonData("cardName","Bouncing Vinyl"); } 	
	setInvulnerableTime(bossObj,180);
	
	ObjMove_SetDestAtSpeed(bossObj,192,120,5);
	SetShotAutoDeleteClip(16,16,16,16);
	SetAutoDeleteObject(true);
	
	concenEffect(bossObj,30,5,64,128,64);
	concenEffect(bossObj,30,5,128,128,128);
	concenEffect(bossObj,60,1,64,64,255);
	concenEffect(bossObj,60,1,64,255,64);
	
	mainTask;
} 

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

@MainLoop { yield; } 
@Finalize { } 


task mainTask { 
	yield;
	scriptEnding;	
	wait(120);
	beweeg;
} 

// movement task
task beweeg { 
	let dir = 0;
	while(!Obj_IsDeleted(bossObj)) {  
		PlaySFX(SFX_POWER0);
		bossAttack = 1;	
		concenEffect(bossObj,15,5,64,128,64);
		concenEffect(bossObj,15,5,128,128,128);

		concenEffect(bossObj,60,1,64,64,255);
		concenEffect(bossObj,60,1,64,255,64);			
		recordSpawnEff;
		recordBullet(GetEnemyX-30,GetEnemyY,0,0,249,30);
		wait(120);
		bossAttack = 0;
		wait(45);
		ObjMove_RandomPosWeight(bossObj,rand(30,60),rand(20,30),15,30,100,96,350,140);
		wait(200);
	} 
}

// amulet shot
task fire { 
	let dir = GetAngleToPlayer(bossObj) - 90;
	ascent(i in 0..6) { 
		PlaySFX(SFX_TAN00);
		ascent(j in 0..5) { 
			CreateShotA1(bossX,bossY,3,dir-15,96,5);
			CreateShotA1(bossX,bossY,3,dir+15,96,5);
			dir+=180/5;
		}
		dir = GetAngleToPlayer(bossObj) - 90;
		wait(15);
	}
}

// record spawn effect
task recordSpawnEff {
	let r = 120;
	let dirx = 0;
	ascent(i in 0..72) { 
		let effShot = CreateShotA((GetEnemyX-30)+r*cos(dirx),GetEnemyY+r*sin(dirx),25);
		SetShotDataA(effShot,0,0,dirx,0,0,0,173);
		DeleteShotA(effShot,0);

		let effShot2 = CreateShotA((GetEnemyX-30)+r*cos(dirx+120),GetEnemyY+r*sin(dirx+120),25);
		SetShotDataA(effShot2,0,0,dirx+120,0,0,0,174);
		DeleteShotA(effShot2,0);
		
		let effShot3 = CreateShotA((GetEnemyX-30)+r*cos(dirx+240),GetEnemyY+r*sin(dirx+240),25);
		SetShotDataA(effShot3,0,0,dirx+240,0,0,0,176);
		DeleteShotA(effShot3,0);	
		dirx+=360/72;
		r-=120/72;
		wait(1);
	}
}

// record bullet
task recordBullet(x,y,v,dir,kleur,delay) {
	let obj = ObjShot_Create(OBJ_SHOT);
	let dir2 = 0;
	let dir3 = 0;
	let bounce = 0;
	
	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);

	loop(20) {
		ascent(i in 0..3) { 
			breakEffectGlitter(ObjMove_GetX(obj)+10*cos(dir2),ObjMove_GetY(obj)+10*sin(dir2),1,dir2,rand(0.2,0.5),255,5,5,255);
			wait(1);
			breakEffectGlitter(ObjMove_GetX(obj)+10*cos(dir2),ObjMove_GetY(obj)+10*sin(dir2),1,dir2,rand(0.2,0.5),255,5,255,5); 
			dir2+=332/18; 
		}
		ObjMove_SetSpeed(obj,0);
		yield;
	} 
	PlaySFX(SFX_SLASH);
	recordMessen(obj);
	recordGlitter(obj);
	ObjMove_SetAngle(obj, atan2(GetPlayerY-ObjMove_GetY(obj),GetPlayerX-ObjMove_GetX(obj)));
	ObjMove_SetSpeed(obj,5);

	while(!Obj_IsDeleted(obj)) {
		x = ObjMove_GetX(obj);
		y = ObjMove_GetY(obj);

		if((y < GetClipMinY) || (y > GetClipMaxY)) {
			ObjMove_SetAngle(obj,atan2(GetPlayerY-ObjMove_GetY(obj),GetPlayerX-ObjMove_GetX(obj)));
			bounce++;
			PlaySFX(SFX_KIRA01);
			if(bounce == 2) { loop(10) { yield; } bounce++; ObjShot_SetGraphic(obj,248); loop(10) { yield; } ObjMove_SetSpeed(obj,0); }
			wait(30);
		} 
		if((x > GetClipMaxX) || (x < GetClipMinX)) {
			ObjMove_SetAngle(obj,atan2(GetPlayerY-ObjMove_GetY(obj),GetPlayerX-ObjMove_GetX(obj)));
			bounce++;
			PlaySFX(SFX_KIRA01);
			if(bounce == 2) { loop(10) { yield; } ObjMove_SetSpeed(obj,0); ObjShot_SetGraphic(obj,248); loop(10) { yield; } bounce++; }
			wait(30);
		}
		if(bounce == 3) { 
			fire;
			dir3 = GetAngleToPlayer(obj);
			PlaySFX(SFX_SPELLCARD);
			ascent(i in 0..43){
				CreateStraightLaserA1(ObjMove_GetX(obj)+32*cos(dir-90),ObjMove_GetY(obj)+32*sin(dir-90),dir,600,10,45,30,45);
				CreateStraightLaserA1(ObjMove_GetX(obj)+32*cos(-dir+90),ObjMove_GetY(obj)+32*sin(-dir+90),dir+180,600,10,45,30,45);
				dir+=360/41;
				wait(1);
			}
			ascent(i in 0..8) {
				CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2,atan2(GetPlayerY-ObjMove_GetY(obj),GetPlayerX-ObjMove_GetX(obj))+dir3,231,5);
				dir3+=360/8;
			}			
			Obj_Delete(obj);	
		}
		yield;
	}
}

// knives
task recordMessen(obj) { 
	let dir = 0;
	while(!Obj_IsDeleted(obj)) {
		ascent(i in 0..5) {
			CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2,atan2(GetPlayerY-ObjMove_GetY(obj),GetPlayerX-ObjMove_GetX(obj))+dir,216,5);
			dir+=360/5;
		}
		wait(15);
	}
}

// glitters
task recordGlitter(obj) { 
	let dir2 = 0;
	while(!Obj_IsDeleted(obj)) {
		ascent(i in 0..6) {
			breakEffectGlitter(ObjMove_GetX(obj)+10*cos(dir2),ObjMove_GetY(obj)+10*sin(dir2),0.4,dir2,rand(0.2,0.5),255,5,5,255);
			wait(1);
			breakEffectGlitter(ObjMove_GetX(obj)+10*cos(dir2),ObjMove_GetY(obj)+10*sin(dir2),0.4,dir2,rand(0.2,0.5),255,5,255,5);  
			dir2+=360/12; 
		}
		wait(5);
		yield;
	}
}

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

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

	CloseScript(GetOwnScriptID());
}
 