#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Ballerina non-spell 02 lunatic"]
#Text["Ballerina non-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 {
	renderBoss(1);
	setAura(bossObj,255,64,255);
	SetCommonData("bossAttackType",1);
	SetCommonData("bossCircleType",0);
	setInvulnerableTime(bossObj,120);	
	
	ObjMove_SetDestAtSpeed(bossObj,192,120,5);
	SetAutoDeleteObject(true);
	mainTask;
} 

@Event { 
	alternative(GetEventType()) 
	case(EV_REQUEST_LIFE) { 
		SetScriptResult(300);
	}
	case(EV_REQUEST_TIMER) { 
		SetScriptResult(48);
	}
} 

@MainLoop { yield; } 
@Finalize { } 

task mainTask { 
	yield;
	scriptEnding;	
	wait(60);
	beweeg;
	vuur;
} 

// circular movement around the screen
task beweeg {
	let dir = 270;
	while(!Obj_IsDeleted(bossObj)) { 
		ObjMove_SetDestAtWeight(bossObj,GetCenterX+160*cos(dir),GetCenterY+160*sin(dir),50,240);
		dir+=0.3;
		yield;
	}
}

// task for handling the pattern
task vuur {
	let dir = 0;
	let kleur = 0;	
	let vuurx = 0;
	let vuury = 0;

	while(!Obj_IsDeleted(bossObj)) { 
		ascent(i in 0..10) {
			PlaySFX(SFX_TAN00);
			ascent(i in 0..16) {
				let hurricane1 = CreateShotA(bossX+60*cos(dir),bossY+10*sin(dir),10);
				SetShotDataA(hurricane1,0,1,dir,0,0.05,3,72);
				
				let hurricane2 = CreateShotA(bossX+60*cos(dir+20),bossY+10*sin(dir+20),10);
				SetShotDataA(hurricane2,0,1,dir+20,0,0.05,3,73);

				let hurricane3 = CreateShotA(bossX+60*cos(dir-20),bossY+10*sin(dir-20),10);
				SetShotDataA(hurricane3,0,1,dir-20,0,0.05,3,74);
				dir+=360/16;
			}
			dir+=4;
			wait(5);
		}
		vuurx = bossX;
		vuury = bossY;
		concenEffect(bossObj,5,2,255,64,128);
		concenEffect(bossObj,5,2,64,2559,64);
		torcenter(vuurx,vuury,0,0,234,0);
		ascent(i in 0..24) {
			torbullet(vuurx,vuury,35,-2,dir,18,50);
			torbullet(vuurx,vuury,40,2,dir,20,50);
			dir+=360/24;
		}
		wait(100);
	}
}	

// tornado centre bubble bullet
task torcenter(x,y,v,dir,kleur,delay) {
	let obj = ObjShot_Create(OBJ_SHOT);
	let speedup = 1;

	ObjShot_Regist(obj);
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetSpeed(obj,v);
	ObjMove_SetAngle(obj,dir);
	ObjShot_SetGraphic(obj,kleur);
	ObjShot_SetDelay(obj,delay);

	loop(120) {
		ObjMove_SetSpeed(obj,0);
		yield;
	} 
	ObjMove_SetAngle(obj, atan2(GetPlayerY-ObjMove_GetY(obj),GetPlayerX-ObjMove_GetX(obj)));
	ObjMove_SetSpeed(obj,3);
}

// tornado outer layer bullets
task torbullet(x,y,r,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);

	PlaySFX(SFX_KIRA00);

	loop(120) {
		ObjMove_SetAngle(obj,dir);
		ObjMove_SetPosition(obj,x+r*cos(dir),y+r*sin(dir));
		dir-=v;
		yield;
	} 
	PlaySFX(SFX_WIND0);
	ObjMove_SetSpeed(obj,3);
}

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

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

	CloseScript(GetOwnScriptID());
}
 