#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Shokora Spell 3"]
#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{
	LoadTexture(GetCurrentScriptDirectory~"sprites/white.png");
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(objEnemy, 360, 15, 25);
	PlayEnemyHitSound(objEnemy, 1);
	
	let Patchouli = GetCurrentScriptDirectory~"sprites/Patchouli.png";
	ObjPrim_SetTexture(objEnemy, Patchouli); 
	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(14, ITEM_POINT_S, GetEnemyX, GetEnemyY, 0, -50, 35, 35, 10000);
	SetShotAutoDeleteClip(32, 40, 32, 32);
	NotifyEventAll(EV_SPELLBGEND, 1);
	//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*41.75);
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let lbcut = GetCurrentScriptDirectory~"sprites/shokorasprite.png";
	ObjCutin_SetSpellcardS4("Self-Enhancement &quot;Solo Speed Thunder&quot;", lbcut, "NAZRIN", "255", "255", "0");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 1);
	
	wait(120);
	attack1;
	SetShotAutoDeleteClip(150, 150, 150, 20);
}

task attack1{	
	
	let radmult = 5.85;
	switch = -1;
	let color = 0;
	let set = 0;
	let radius = 49;
	let time = 0;
	let type = -1;
	let xcoord = 0;
	let ycoord = 0;
	angle = 0;
	angle2 = 90;

	loop{
		SetCommonData("turnoffaura", false);
		PlaySoundEffect("charge", 100, GetEnemyX);
		TCharge01(GetEnemyX, GetEnemyY, 3, 90, 255, 255, 0, BLEND_ADD_ARGB, 25);
		ObjAnim_SetAttackA1(objEnemy, true);
		wait(90);
		PlaySoundEffect("thunder", 90, GetCenterX);
		screenflash;
		wait(10);
		Aura(objEnemy, GetCurrentScriptDirectory~"sprites/Patchouli.png", 255, 255, 0);
		ascent(i in 7..85){
			loop(4){
				xcoord = GetEnemyX+(i*radmult)*cos(angle);
				ycoord = GetEnemyY+(i*radmult)*sin(angle);
				angle2 = AngleBetweenPoints(xcoord, ycoord, GetEnemyX, GetEnemyY);
				if(GetDistance(xcoord, ycoord, GetPlayerX, GetPlayerY)>30){
					loop(3){
						let obj = CreateShotA2(GetEnemyX+(i*radmult)*cos(angle), GetEnemyY+(i*radmult)*sin(angle), 0, angle2, 0, 0, CIRCLE_S[YELLOW], 0);
						ObjShot_SetSpellResist(obj, true);
						ObjMove_AddPatternA2(obj, 90, NO_CHANGE, NO_CHANGE, 0.01, 0, 0.75); 
						ObjMove_AddPatternA2(obj, 420, NO_CHANGE, NO_CHANGE, 0.01, 0, 2);  						
						angle2+=360/3;
					}
				}
				angle+=360/4;
			}
			//yield;
			angle+=rand(7.5-0.25, 7.5+0.25)*switch;
			angle2+=4;
		}
		wait(90);
		ObjAnim_SetAttackA1(objEnemy, false);
		PlaySoundEffect("shot2", 90, GetCenterX);
		angle = rand(0, 360);
		switch*=-1;
		wait(30);
		PlaySoundEffect("charge2", 100, GetEnemyX);
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 255, 0, BLEND_ADD_ARGB, 25);
		wait(45);
		ascent(i in 0..241){
			if(i%5==0){
				PlaySoundEffect("shot", 100, ObjMove_GetX(objEnemy));
				ascent(j in -1..2){ CreateShotA2(GetEnemyX, GetEnemyY, 3+(i/120), GetAngleToPlayer(objEnemy)+(j*15), 0.06, 10, ARROWHEAD[ORANGE], 0); }
			}
			if(i%30==0){
				ExMove(objEnemy, GetCenterX, rand(50, 150), 120+rand(-10, 10), 25, 5, 10);
			}
			yield;
		}
		ObjMove_SetDestAtWeight(objEnemy, GetCenterX, rand(120-10, 120+10), 15, 3);
		wait(30);
		SetCommonData("turnoffaura", true);
		wait(30);
		yield;
	}
}

task screenflash{
	let time = 0;
	let objWhite = ObjPrim_Create(OBJ_SPRITE_2D);

	let alphavar = 0;
	let white = GetCurrentScriptDirectory~"sprites/white.png";
	ObjPrim_SetTexture(objWhite, white);
	ObjSprite2D_SetSourceRect(objWhite, 0, 0, 691, 607);
	ObjSprite2D_SetDestCenter(objWhite);
	ObjRender_SetColor(objWhite, 255, 255, 255);
	ObjRender_SetAlpha(objWhite, 127.5);
	ObjMove_SetX(objWhite, GetStgFrameWidth/2);
	ObjMove_SetY(objWhite, GetStgFrameHeight/2);
	ObjRender_SetScaleXYZ(objWhite, 2, 2, 1);
	Obj_SetRenderPriorityI(objWhite, 69);
	
	loop(75){
		ObjRender_SetAlpha(objWhite, 125+125*sin(alphavar));
		if(time<=9){
		alphavar+=9;
		}
		if(time>=45){
		alphavar+=180/30;
		}
		yield;
		time++;
		}
	Obj_Delete(objWhite);
}