#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Kosuke Spell 2"]
#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, 10, 25);
	PlayEnemyHitSound(objEnemy, 1);
	
	let Youmu = GetCurrentScriptDirectory~"sprites/Youmu.png";
	ObjPrim_SetTexture(objEnemy, Youmu); 
	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, 0, 0, 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, 0, 0, 128, 128);
	ascent(x in 0..4){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACK, 128*4, 128*x, 128*5, 128*x+128);}
	ascent(x in 2..4){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	loop(8){ ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*7, 128*6, 128*7+128); }
	descent(x in 2..4){ ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	descent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*4, 128*x, 128*5, 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, 3);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	
	SetAutoDeleteObject(true);
	delayedstart;
	End;
}

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

task delayedstart{
	
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
	wait(60);
	
	ObjEnemyBossScene_StartSpell(objScene);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*49.75);
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let lbcut = GetCurrentScriptDirectory~"sprites/kosukesprite.png";
	ObjCutin_SetSpellcardS4("Counter-Magic &quot;Mana Drain&quot;", lbcut, "NAZRIN", "128", "0", "128");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 0);
	
	wait(120);
	attack1;
	SetShotAutoDeleteClip(80, 64, 80, 80);
}

task attack1{	

	switch = -1;
	let color = 0;
	let set = 0;
	let radius = 52;
	let time = 0;
	let type = -1;
	wait(30);
	
	loop{
	
		if(color==0){ TCharge01(GetEnemyX, GetEnemyY, 3, 45, 0, 0, 255, BLEND_ADD_ARGB, 25); } 
		else{ TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 255, BLEND_ADD_ARGB, 25); }
		ObjAnim_SetAttackA1(objEnemy, true);
		PlaySoundEffect("charge2", 100, GetEnemyX);
		wait(30);
		
		time = 15;
		ManaDrain(type);
		wait(330);
		
		TCharge01(GetEnemyX, GetEnemyY, 3, 120, 0, 0, 255, BLEND_ADD_ARGB, 25);
		PlaySoundEffect("charge", 100, GetEnemyX);
		wait(120);
		
		PlaySoundEffect("se_enep02", 100, GetEnemyX);
		ObjAnim_SetAttackA1(objEnemy, false);
		FlameVortex(switch, [FIRE[BLUE], FIRE[PURPLE]][color%2]);
		switch*=-1;
		wait(45);
		color++;
		PlaySoundEffect("se_enep02", 100, GetEnemyX);
		FlameVortex(switch, [FIRE[BLUE], FIRE[PURPLE]][color%2]);
		wait(105);
		ExMove(objEnemy, GetPlayerX, rand(40, 60), 120+rand(-5, 5), 20, 3, 15);
		wait(45);
		
		type*=-1;
		if(type==-1){
			switch*=-1;
		}
	}
	
	task ManaDrain(type){
		let count = 0;
		let angleacc = 20;
		loop(62){
			PlaySoundEffect("shot2", 100, GetPlayerX);
			loop(2){
				Succshot(GetPlayerX+radius*cos(angle), GetPlayerY+radius*sin(angle), 2.5, angle+rand(-2, 2), -2.5/45, 0, [[CIRCLE_S[PURPLE], CIRCLE_L[BLUE]], [CIRCLE_S[BLUE], CIRCLE_L[PURPLE]]][set%2][color%2], 10);
				angle+=360/2;
				}
			color++;
			if(type==-1){
				angle+=360/3/3*switch;
			}
			if(type==1){
				angle+=angleacc*switch;
				angleacc+=3;
			}
			wait(time);
			if(count<50){
				if(time>5){ time--; }
			}
			if(count>=50){
				if(time<15){ time++; }
			}
			count++;
		}
		set++;
	}
}

task FlameVortex(dir, color){
		let shottotal = 60;
		let way = 3;
		let speed = 5;
		let maxspeed = 1.5;
		let radius = 1.5 * 80;
		let radiusacc = ( 11 - radius ) / ( shottotal - 1 );
		let whirlangle = rand( 0, 360 );
		let whirlangleacc = 18.45 * dir;

		ascent(i in 0..shottotal){
			if(!Obj_IsDeleted(objEnemy)){
			let _nX = GetEnemyX;
			let _nY = GetEnemyY;
			let whirlangle2 = whirlangle;
			loop(way){
				let nX = GetEnemyX+radius*cos(whirlangle2);
				let nY = GetEnemyY+radius*sin(whirlangle2);
				let shotangle = whirlangle2 - 120 * dir;
				let obj = CreateMDNHShotA2(nX, nY, 5, shotangle, -3.5/30, 3.9-(i/17.5), color, 30);
				ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
				whirlangle2+= 360/way;
			}
			radius += radiusacc;
			whirlangle+=whirlangleacc+(i/55);
			wait(2);
			}
		}

	}

task Succshot(x, y, v, angle, accel, minmax, graphic, delay){
	let obj = CreateMDNHShotA2(x,y,v,angle,accel,minmax,graphic,delay);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Manage(obj);

	task Manage(obj){
		let time = 0;
		wait(60);
		ObjMove_SetAngle(obj, GetAngleToPoint(ObjMove_GetX(obj), ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+rand(-2, 2));
		ObjMove_SetSpeed(obj, 0);
		ObjMove_SetAcceleration(obj, v/45);
		ObjMove_SetMaxSpeed(obj, v);
		while(!Obj_IsDeleted(obj)){
			if(GetDistance(GetEnemyX, GetEnemyY, ObjMove_GetX(obj), ObjMove_GetY(obj))<=45){ ObjShot_FadeDelete(obj); } 
			yield;
		}
	}
}