#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Tricky Spell 4"]
#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"

let GetDifficulty = 2;
let GetStage = 4;
let fam = GetCurrentScriptDirectory~"sprites/familiar.png";	

@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("Woven Elements &quot;Triple Triad&quot;", shokocut, "NAZRIN", "255", "255", "255");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 3);
	
	wait(120);
	attack1;
	SetShotAutoDeleteClip(32, 40, 32, 32);
}

task attack1{
	let color = 0;
	let rad1 = 40;
	let count = 2;
	let angle = rand(0, 360);
	wait(30);
	PlaySoundEffect("charge2", 100, GetEnemyX);
	TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 255, 255, BLEND_ADD_ARGB, 25);
	wait(60);
	PlaySoundEffect("shadow", 100, GetEnemyX);
	loop(3){
		LightSprite(angle, 1, color);
		angle+=360/3;
		color++;
		}
	wait(180);
	loop{
		wait(60);
		PlaySoundEffect("shot3", 100, GetEnemyX);
		ascent(i in -4..5){
			let obj = CreateDNHShotA2(GetEnemyX, GetEnemyY, 4, GetAngleToPlayer(objEnemy)+(i*12), -0.04, 1+1/3, [CIRCLE_L[ORANGE], CIRCLE_L[GREEN], CIRCLE_L[AZURE]][color%3], 10);
			Obj_SetRenderPriorityI(obj, Obj_GetRenderPriorityI(obj)-1);
			//ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
			}
			color++;
		if(count%2==0){
			ExMove(objEnemy, ObjMove_GetX(GetPlayerObjectID), rand(20, 30), rand(105, 135), 40, 3, 15);
		}
		count++;
	}
}

task LightSprite(ang, dir, col){
	let spriteframe = 420;
	let spriteangle = 90;
	let rad = 0;
	let ang2 = ang;
	let ang3 = ang+30*dir;
	let alpha = 0;
	let radacc = 2.275;
	let initradacc = radacc;
	let objSprite = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(objSprite);
	ObjMove_SetPosition(objSprite, GetEnemyX, GetEnemyY);
	ObjEnemy_SetLife(objSprite, 150);
	ObjEnemy_SetDamageRate(objSprite, 100, 100);
	ObjPrim_SetTexture(objSprite, fam); 
	ObjSprite2D_SetSourceRect(objSprite, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(objSprite);  
	ObjRender_SetScaleXYZ(objSprite, 0.75, 0.75, 0);
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
		if(alpha<255){ alpha+=255/30; ObjRender_SetAlpha(objSprite, alpha);}
		if(rad<60){ rad+=60/45; }
		ObjRender_SetAngleZ(objSprite, spriteframe);
		if(spriteframe%7==0&& spriteframe>450){
			PlaySoundEffect("shot2", 85, GetEnemyX);
			loop(3){
				ascent(i in 0..3){
					if(ObjMove_GetY(objSprite)<GetEnemyY){
						let obj = CreateDNHShotA2(ObjMove_GetX(objSprite)+(i*20)*cos(ang3), ObjMove_GetY(objSprite)+(i*20)*sin(ang3), 3, ang3, -0.125, 0.35, [PELLET[ORANGE], PELLET[GREEN], PELLET[AZURE]][col%3], 10); 
						ManipulateShots(obj, i);
					}
					else{
						let obj = CreateDNHShotA2(ObjMove_GetX(objSprite)+(i*20)*cos(ang3), ObjMove_GetY(objSprite)+(i*20)*sin(ang3), 2.5, ang3, -0.125, 0.35, [PELLET[ORANGE], PELLET[GREEN], PELLET[AZURE]][col%3], 10); 
						ManipulateShots(obj, i);
					}
				}
				ang3+=360/3;
			}
			if(dir>0){ 	ang3+=360/3/5*(max(dir, 0.3025)); }
			if(dir<=0){ ang3+=360/3/5*(min(dir, -0.3025)); }
		}
		ang2+=radacc*dir;
		ObjMove_SetPosition(objSprite, GetEnemyX+rad*1.65*cos(ang2), GetEnemyY+rad*sin(ang2));
		dir = 1.25+0.75*sin(spriteframe*0.45);
		spriteframe++;	
		yield;
		}
	if(alpha>0){
				alpha-=255/30;
				ObjRender_SetAlpha(objSprite, alpha);
	}
	if(alpha<=0){
		Obj_Delete(objSprite);
	}
	Obj_Delete(objSprite);
}

task ManipulateShots(obj, velmod){
	let counter = 0;
	while(!Obj_IsDeleted(obj)){
		counter++;
		if(counter==45){
			ObjMove_SetAcceleration(obj, 0.023-(velmod*0.00225));
			ObjMove_SetMaxSpeed(obj, 5);
			}
		yield;
	}
}
