#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Shokora 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, 13, 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*45);
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let shokocut = GetCurrentScriptDirectory~"sprites/shokorasprite.png";
	ObjCutin_SetSpellcardS4("Wind Tome &quot;Fimbulvetr&quot;", shokocut, "NAZRIN", "219", "255", "255");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 1);
	
	wait(120);
	attack1;
	SetShotAutoDeleteClip(40, 40, 40, 40);
}

task attack1{

	let xcoord = 0;
	let ycoord = 0;
	let dist = 80;
	let offset = dist/2;
	let switch = -1;
	let radius = 120;
	let time = 0;
	wait(30);
	PlaySoundEffect("charge2", 100, GetEnemyX);
	TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 255, 255, BLEND_ADD_ARGB, 25);	
	ObjAnim_SetAttackA1(objEnemy, true);
	wait(30);
	loop{
	
		if(time==150 || time == 390){
			PlaySoundEffect("charge2", 100, GetEnemyX);
			TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 255, 255, BLEND_ADD_ARGB, 25);	
		}
		
		if(time%11==0 || time%11==1 || time%11==2){
			loop(6){
				let obj = CreateShotA2(GetEnemyX, GetEnemyY, 3.25, angle, 0, 3.25, PELLET[BLUE], 0);
				ObjMove_AddPatternA2(obj, 0, 3.25, NO_CHANGE, 0, 2.85, 6); 
				ObjMove_AddPatternA2(obj, 105, NO_CHANGE, NO_CHANGE, -0.10, 0, 3.25); 
				angle+=360/6;
			}
		}
		
		if((time%11==8 || time%11==9 || time%11==10) && time>210){
			loop(6){
				let obj = CreateShotA2(GetEnemyX, GetEnemyY, 3.25, -angle, 0, 3.25, PELLET[AZURE], 0);
				ObjMove_AddPatternA2(obj, 0, 3.25, NO_CHANGE, 0, -2.85, 6);
				ObjMove_AddPatternA2(obj, 105, NO_CHANGE, NO_CHANGE, -0.10, 0, 3.25); 
				angle+=360/6;
			}	
		}
		
		if(time%120==0 && time>=450){
			PlaySoundEffect("shot3", 90, GetEnemyX);
			ascent(i in -1..2){
				CreateSnowflake(GetEnemyX, GetEnemyY, 5, GetAngleToPlayer(objEnemy)+(i*35), -0.1, 2, CIRCLE_S[GRAY], 10, 2.5*switch);
			}
			switch = switch*-1;
		}
		angle+=1.614285714285714;
		time++;
		
		yield;
	}
}

task CreateSnowflake(x, y, v, dir, accel, minmax, graphic, delay, snowflakerot){
		let ang = dir;
		let objMaster = CreateDNHShotA2(x, y, v, dir, accel, minmax, CIRCLE_S[GRAY], delay);
		Obj_SetRenderPriorityI(objMaster, Obj_GetRenderPriorityI(objMaster)-1);
		ObjShot_SetSpellResist(objMaster, true);
	loop(6){
		let obj = CreateDNHShotA2(x+14*cos(ang), y+14*sin(ang), v, dir, accel, minmax, graphic, delay);
		Obj_SetRenderPriorityI(obj, Obj_GetRenderPriorityI(obj)-1);
		ObjShot_SetSpellResist(obj, true);
		Rotate(obj, objMaster, ang, snowflakerot, 14);
		ang+=360/6;
		}
	loop(6){
		let obj = CreateDNHShotA2(x+28*cos(ang), y+28*sin(ang), v, dir, accel, minmax, graphic, delay);
		Obj_SetRenderPriorityI(obj, Obj_GetRenderPriorityI(obj)-1);
		ObjShot_SetSpellResist(obj, true);
		Rotate(obj, objMaster, ang, snowflakerot, 28);
		ang+=360/6;
		}
		
	task Rotate(obj, objToFollow, angle, speedsnow, radius){
		let ang = angle;
		while(!Obj_IsDeleted(objToFollow)){
			ObjMove_SetPosition(obj, ObjMove_GetX(objToFollow)+radius*cos(ang), ObjMove_GetY(objToFollow)+radius*sin(ang));
			ang+=speedsnow;
			yield;
		}
		Obj_Delete(obj);
	}
		
}

