#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Tricky Spell 1"]
#Background["./NamelessPath.dnh"]
#Player["./../lib/Okina_player_by_Python/Okina Matara/Okina.dnh"]

#include "../lib/UNL_Shot/lib_ShotConstants.dnh"
#include "./../lib/includes/BossIncludes.dnh"
#include "./../lib/includes/EffectFunctions.dnh"

@Event{
	alternative(GetEventType())
		case(EV_REQUEST_LIFE){ SetScriptResult(1000); }
		case(EV_REQUEST_TIMER){ SetScriptResult(999); }
		case(EV_REQUEST_SPELL_SCORE){ SetScriptResult(10000000); }
		case(EV_GAIN_SPELL){ AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE)); }
}

@Initialize{

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(objEnemy, 360, 12.5, 25);
	PlayEnemyHitSound(objEnemy, 1);
	
	let Eirin = GetCurrentScriptDirectory~"sprites/Eirin.png";
	ObjPrim_SetTexture(objEnemy, Eirin); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 0, 128*x, 128, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 128, 128*x, 256, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_LEFT, 256, 0, 256+128, 128);
	ascent(x in 2..4){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_RIGHT, 256, 128*2, 256+128, 128*3);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACK, 128*5, 128*x, 128*6, 128*x+128);}
	descent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 12);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 10);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 2);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	
	SetAutoDeleteObject(true);
	delayedstart;
	End;
}

task End {
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0) { yield; }
	CreateItems02(16, ITEM_POINT_S, GetEnemyX, GetEnemyY, 0, -50, 35, 35, 10000);
	SetShotAutoDeleteClip(32, 40, 32, 32);
	NotifyEventAll(EV_SPELLBGEND, 3);
	//DeleteShotWave(1,GetX,GetY,20);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	PlaySoundEffect("end",60,GetCenterX);
	Obj_Delete(objEnemy);
	CloseScript(GetOwnScriptID);
}

task delayedstart{

	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
	wait(60);
	
	ObjEnemyBossScene_StartSpell(objScene);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*60);
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let shokocut = GetCurrentScriptDirectory~"sprites/trickysprite.png";
	ObjCutin_SetSpellcardS4("Conjury &quot;Sequential Stonesplosion&quot;", shokocut, "NAZRIN", "139", "69", "19");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 3);
	
	wait(120);
	attack1;
	SetShotAutoDeleteClip(64, 32, 64, 64);
}

task attack1{
	let switch = -1;
	let xcoord = 0;
	let ycoord = 0;
	let dist = 80;
	let offset = dist/2;
	let radius = 120;
	let time = 0;
	wait(10);
	PlaySoundEffect("charge2", 100, GetEnemyX);
	TCharge01(GetEnemyX, GetEnemyY, 3, 45, 139, 69, 19, BLEND_ADD_ARGB, 25);	
	ObjAnim_SetAttackA1(objEnemy, true);
	wait(45);
	loop{
		ObjAnim_SetAttackA1(objEnemy, true);
		switch*=-1;
		Stonesplode(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy));
		wait(90);
		ObjAnim_SetAttackA1(objEnemy, false);
		wait(90);
		ObjAnim_SetAttackA1(objEnemy, true);
		if(switch==-1){
			Stonesplode(ObjMove_GetX(objEnemy)+(GetCenterX/2)+10, GetCenterY);
			wait(75);
			Stonesplode(GetCenterX, GetCenterY);
			wait(75);
			Stonesplode(ObjMove_GetX(objEnemy)-(GetCenterX/2)-10, GetCenterY);
		}
	else{
		Stonesplode(ObjMove_GetX(objEnemy)-(GetCenterX/2)-10, GetCenterY);
		wait(75);
		Stonesplode(GetCenterX, GetCenterY);
		wait(75);
		Stonesplode(ObjMove_GetX(objEnemy)+(GetCenterX/2)+10, GetCenterY);
	}
	wait(60);
	ObjAnim_SetAttackA1(objEnemy, false);
	wait(90);
	ObjAnim_SetAttackA1(objEnemy, true);
	if(switch==-1){
		Stonesplode(GetStgFrameWidth-30, GetStgFrameHeight-60);
		wait(75);
		Stonesplode(0+30, GetStgFrameHeight-60);
	}
	if(switch==1){
		Stonesplode(0+30, GetStgFrameHeight-60);
		wait(75);
		Stonesplode(GetStgFrameWidth-30, GetStgFrameHeight-60);
	}
	ObjAnim_SetAttackA1(objEnemy, false);
	wait(180);
	}
}

task Stonesplode(x, y){
	let angle3 = 0;
	let explode = 0;
	loop(45){
		TExpan(x+rand(-30, 30),y+rand(-30, 30),2,15,160,255,255,255);
		wait(2);
	}
	PlaySoundEffect("se_enep02", 75, x);
	loop(26){
		explode++;
		let obj = CreateDNHShotA2(x, y, 3.5, angle3, -0.05, 2, CIRCLE_L[ORANGE], 10);
		Obj_SetRenderPriorityI(obj, Obj_GetRenderPriorityI(obj)-1);
		if(explode%2==0){ ManipulateRock(obj); }
		angle3+=360/26;
	}
}

task ManipulateRock(obj){
	let vangle = ObjMove_GetAngle(obj);
	wait(45);
	if(Obj_IsDeleted(obj)){ return; }
	PlaySoundEffect("se_enep02", 75, GetCenterX);
	let obj2 = CreateDNHShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 3.5, vangle-195, -0.05, 2, CIRCLE_S[ORANGE], 10);
	ManipulateRock2(obj2);
	CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 3.5, vangle-165, -0.05, 2, CIRCLE_S[ORANGE], 10);
	Obj_Delete(obj);
}

task ManipulateRock2(obj){
	let vangle = ObjMove_GetAngle(obj);
	wait(30);
	if(Obj_IsDeleted(obj)){ return; }
	PlaySoundEffect("se_enep02", 75, GetCenterX);
	loop(10){
		CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 2.5, vangle+15, -0.05, 2-(rand(0.2, 0.4)), DIAMOND[ORANGE], 10);
		vangle+=360/10;
	}
	Obj_Delete(obj);
}

/*
function TExpan(px,py,maxv,t,r,g,b,alpha){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GetCurrentScriptDirectory~"../../img/effect/charge.png";
	let scale = 0;
	let v = 0;
	let time = 0;
	let alpha2 = 255;
	let alpha3 = 0;
	let malpha = 255/t;
	ObjPrim_SetTexture(obj,img);
	ObjRender_SetAlpha(obj, alpha);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,px,py,0);
	ObjRender_SetColor(obj,r,g,b);
	Obj_SetRenderPriorityI(obj,55);
	Expan;
		task Expan{
			descent(i in 0..t){
			if(time>=50){ alpha2 = malpha*i; alpha3 = alpha2; loop(10){ alpha2-=alpha3/10; ObjRender_SetAlpha(obj, alpha2); wait(1); } break;}
			ObjRender_SetAlpha(obj,malpha*i);
			v = (i*maxv/2)/(t*t);
			scale += v;
			ObjRender_SetScaleXYZ(obj,scale,scale,0);
			if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)<=0){ Obj_Delete(obj); }
			yield;
			time++;
			}
			Obj_Delete(obj);
		}
	//Obj_Delete(obj);
	}
*/