#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Afro spell 02 hard"]
#Text["Afro spell 02 hard"]

let glasTex = "script/thdcs/system/enm02/glazen.png";
let shakerTex = "script/thdcs/system/enm02/shaker.png";
let shakerCon = 0;

// enum
let IDLE  = 0;
let SHAKEIT = 1;
let TOSS = 2;
let HURL = 3;

#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(2);
	setAura(bossObj,64,64,255);
	SetCommonData("bossCircleType",2);
	SetCommonData("bossAttackType",2);
	SetCommonData("bossBgType",2);
	if(GetAreaCommonData("system","gameLang",0) == 0) { SetCommonData("cardName","o[e_[Z uōv"); } 
	if(GetAreaCommonData("system","gameLang",0) == 1) { SetCommonData("cardName","Bartender skill uPunch Mixv"); } 			
	setInvulnerableTime(bossObj,300);
	
	concenEffect(bossObj,15,4,64,64,255);
	concenEffect(bossObj,15,4,255,64,64);
	concenEffect(bossObj,15,4,64,255,64);
	concenEffect(bossObj,15,4,255,255,64);

	concenEffect(bossObj,30,3,155,32,255);
	concenEffect(bossObj,30,3,255,255,255);
	concenEffect(bossObj,30,3,255,255,64);	

	ObjMove_SetDestAtSpeed(bossObj,192,GetCenterY-128,3);
	SetShotAutoDeleteClip(16,16,16,16);
	SetAutoDeleteObject(true);
	mainTask;
} 

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

@MainLoop { yield; } 
@Finalize { } 


task mainTask { 
	yield;
	scriptEnding;
	wait(120);
	control;
	control2;
	mixDrink;
} 

// Shaker spawner
task mixDrink {
	while(!Obj_IsDeleted(bossObj)) { 
		wait(75);
		shakerFam(GetEnemyX-30,GetEnemyY-5);
		wait(240);
		ObjMove_SetDestAtSpeed(bossObj,GetPlayerX,GetCenterY-128,5);
	}
}

// outer martini spawning
task control { 
	while(!Obj_IsDeleted(bossObj)) { 
		glassFam(GetClipMinX-16,50,0);
		glassFam(GetClipMaxX+16,400,1);
		wait(40);
	}
}

// inner martini spawning
task control2 { 
	wait(30);
	while(!Obj_IsDeleted(bossObj)) { 
		PlaySFX(SFX_KIRA00);
		glassFam(GetClipMaxX+16,150,1);
		wait(100);
	}
}

// shaker
task shakerFam(x,y) {
	let obj = ObjEnemy_Create(OBJ_ENEMY); 
	let dir = 180;
	let dir2 = 0;
	let dir3 = 0;
	let r = 50;
	
	ObjEnemy_Regist(obj);
	ObjPrim_SetTexture(obj,shakerTex); 
	
	ObjRender_SetScaleXYZ(obj,0.7,0.7,0);	
	ObjSprite2D_SetSourceRect(obj,0,0,64,64); 
	ObjSprite2D_SetDestCenter(obj);
	ObjEnemy_SetLife(obj,1);
	ObjMove_SetPosition(obj,x,y);	
	Obj_SetValue(obj,"isFamiliar",true); 
	
	bossAttack = 3;
		
	// show off
	shakeEffect(obj);
	loop(8) { 
		PlaySFX(SFX_WATER);
		ObjMove_SetY(obj,ObjMove_GetY(obj)+5); 
		wait(3);
		ObjMove_SetY(obj,ObjMove_GetY(obj)-10); 
		wait(3);
		ObjMove_SetY(obj,ObjMove_GetY(obj)+5); 
	}
	bossAttack = 0;
	// toss
	loop(15) {
		ObjRender_SetAngleZ(obj,dir2);
		ObjMove_SetX(obj,GetEnemyX+30*cos(dir));
		ObjMove_SetY(obj,GetEnemyY+80*sin(dir));
		dir+=180/15; 
		dir2+=180/7;
		wait(2);
	}
 	bossAttack = 4;	
	// show off
	shakeEffect(obj);
	loop(8) { 
		PlaySFX(SFX_WATER);	
		ObjMove_SetY(obj,ObjMove_GetY(obj)+5); 
		wait(3);
		ObjMove_SetY(obj,ObjMove_GetY(obj)-10); 
		wait(3);
		ObjMove_SetY(obj,ObjMove_GetY(obj)+5); 
	}
	bossAttack = 0;
	// toss
	loop(12) {
		ObjRender_SetAngleZ(obj,dir2);
		ObjMove_SetX(obj,GetEnemyX+30*cos(dir));
		ObjMove_SetY(obj,GetEnemyY+80*sin(dir));
		dir-=180/15; 
		dir2+=180/6;
		wait(2);
	}
	PlaySFX(SFX_CH02);	
	concenEffect(obj,5,4,128,64,255);
	concenEffect(obj,5,4,64,255,64);		
	concenEffect(obj,8,3,255,255,255);
	concenEffect(obj,8,3,64,255,64);	
	concenEffect(obj,8,3,255,64,64);
	wait(75);
	bossAttack = 0;
	// hurl
	PlaySFX(SFX_FEATHER);		
	bossAttack = 2;
	ObjMove_SetAngle(obj,atan2(GetPlayerY-ObjMove_GetY(obj),GetPlayerX-ObjMove_GetX(obj)));
	ObjMove_SetSpeed(obj,3);
	splashAround(obj);
	
	wait(120);
	// splash
	PlaySFX(SFX_KILL);	
	ascent(i in 0..6) {
		CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2,GetAngleToPlayer(obj)+dir3,234,0);
		dir3+=360/6;
	}
	Obj_DeleteValue(obj,"isFamiliar");		
	Obj_Delete(obj);

} 

// Shaker effect handler 
task shakeEffect(obj) { 
	let dir = 0;
	
	loop(36) {
		wait(1);
		let shakyEff = CreateShotA(ObjMove_GetX(obj)+25*cos(dir),ObjMove_GetY(obj)+25*sin(dir),5);
		SetShotDataA(shakyEff,0,1,dir,0,0,0,177);
		DeleteShotA(shakyEff,10);

		let shakyEff2 = CreateShotA(ObjMove_GetX(obj)+15*cos(dir),ObjMove_GetY(obj)+15*sin(dir),5);
		SetShotDataA(shakyEff2,0,1,dir-20,0,0,0,172);
		DeleteShotA(shakyEff2,10);

		let shakyEff3 = CreateShotA(ObjMove_GetX(obj)-35*cos(dir),ObjMove_GetY(obj)-35*sin(dir),5);
		SetShotDataA(shakyEff3,0,1,dir-35,0,0,0,173);
		DeleteShotA(shakyEff3,10);

		dir+=48;
	}
}

// Spreading bullets while tossing
task splashAround(obj) {
	let dir = 0;
	loop(10) {
		shakeEffect(obj);
		ascent(i in 0..6) { 
			PlaySFX(SFX_TAN00);
			CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),rand(0.3,2.0),GetAngleToPlayer(obj)+dir,26,10);
			CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),rand(0.3,2.0),GetAngleToPlayer(obj)+dir,61,10);	
			dir+=8; 			
		}
		wait(5);
	}
}

// glass fam 
task glassFam(x,y,move) {
	let type = rand_int(1,5); 
	let dir = 0;
	let obj = ObjEnemy_Create(OBJ_ENEMY); 
	
	ObjEnemy_Regist(obj);
	ObjEnemy_SetLife(obj,5);
	ObjPrim_SetTexture(obj,glasTex); 
	ObjRender_SetScaleXYZ(obj,1,1,0);	
	if(type == 1){ ObjSprite2D_SetSourceRect(obj,0,0,48,48); }
	if(type == 2){ ObjSprite2D_SetSourceRect(obj,48,0,96,48); }
	if(type == 3){ ObjSprite2D_SetSourceRect(obj,0,48,48,96); }
	if(type == 4){ ObjSprite2D_SetSourceRect(obj,48,48,96,96); }
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetValue(obj,"isFamiliar",true); 
	ObjMove_SetPosition(obj,x,y);	
	
	if(move == 0) { ObjMove_SetDestAtSpeed(obj,GetClipMaxX+64,ObjMove_GetY(obj),1); } 
	if(move == 1) { ObjMove_SetDestAtSpeed(obj,GetClipMinX-64,ObjMove_GetY(obj),1); }
	
	while(!Obj_IsDeleted(obj)) {
		ObjEnemy_SetIntersectionCircleToShot(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),16);
		ObjEnemy_SetIntersectionCircleToPlayer(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),16);
		
		if(ObjEnemy_GetInfo(obj, INFO_LIFE) <= 0) { 
			PlaySFX(SFX_KILL);	
			ascent(i in 0..6) { 
				CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2,GetAngleToPlayer(obj)+dir,230,0);
				dir+=360/6;
			}
			Obj_Delete(obj); 
		}
		if(ObjMove_GetX(obj) > GetClipMaxX+32) { Obj_Delete(obj); } 
		if(ObjMove_GetX(obj) < GetClipMinX-32) { Obj_Delete(obj); } 
		
		yield;
	}
	Obj_DeleteValue(obj,"isFamiliar");	
	Obj_Delete(obj);	
}

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

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

	CloseScript(GetOwnScriptID());
}
 