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

@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.5, 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 {
	let itemx = 0;
	let itemy = 0;
	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*(53.75-17));
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let lbcut = GetCurrentScriptDirectory~"sprites/kosukesprite.png";
	ObjCutin_SetSpellcardS4("Blazing Barrier &quot;Firewall&quot;", GetCurrentScriptDirectory~"sprites/kosukesprite.png", "NAZRIN", "255", "0", "0");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 0);
	
	wait(120);
	attack1;
	SetShotAutoDeleteClip(150, 138, 150, 138);
}

task attack1{

	let x = 0;
	wait(30);
	
	loop{
	
		x = rand(-15, 15);
		AlertZone01(3, -200+x, 150+(1*74), -30, 60);
		wait(30);
		
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 0, BLEND_ADD_ARGB, 25);
		ObjAnim_SetAttackA1(objEnemy, true);
		PlaySoundEffect("charge2", 100, GetEnemyX);
		wait(30);
		
		ObjAnim_SetAttackA1(objEnemy, false);
		PlaySoundEffect("slash", 100, GetEnemyX);
		ascent(i in 0..3){
			CreateLaser1(-200-(i*18)+x, 150+(i*74), -30, LASER[RED], 800, 15, 30, -30+90, 0);
		}
		wait(35);
		
		x = rand(-15, 15);
		AlertZone01(3, 384+200+x, 150+(1*74), 210, 60);
		wait(30);
		
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 0, BLEND_ADD_ARGB, 25);
		ObjAnim_SetAttackA1(objEnemy, true);
		PlaySoundEffect("charge2", 100, GetEnemyX);
		wait(30);
		
		PlaySoundEffect("slash", 100, GetEnemyX);
		ObjAnim_SetAttackA1(objEnemy, false);
		ascent(i in 0..3){
			CreateLaser1(384+200+(i*18)+x, 150+(i*74), 210, LASER[RED], 800, 15, 30, 210-90, 0);
		}
		wait(35);
		
		x = rand(-15, 15);
		AlertZone01(3, 384+200+x, (448-150)-(1*74), -210, 60);
		wait(30);
		
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 0, BLEND_ADD_ARGB, 25);
		ObjAnim_SetAttackA1(objEnemy, true);
		PlaySoundEffect("charge2", 100, GetEnemyX);
		wait(30);
		
		PlaySoundEffect("slash", 100, GetEnemyX);
		ObjAnim_SetAttackA1(objEnemy, false);
		ascent(i in 0..3){
			CreateLaser1(384+200+(i*18)+x, (448-150)-(i*74), -210, LASER[RED], 800, 15, 30, -210+90, 0);
		}
		wait(35);
		
		x = rand(-15, 15);
		AlertZone01(3, -200+x, (448-150)-(1*74), 30, 60);
		wait(30);
		
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 0, BLEND_ADD_ARGB, 25);
		ObjAnim_SetAttackA1(objEnemy, true);
		PlaySoundEffect("charge2", 100, GetEnemyX);
		wait(30);
		
		PlaySoundEffect("slash", 100, GetEnemyX);
		ObjAnim_SetAttackA1(objEnemy, false);
		ascent(i in 0..3){
			CreateLaser1(-200-(i*18)+x, (448-150)-(i*74), 30, LASER[RED], 800, 15, 30, 30-90, 0);
		}
		wait(185);
	}	
}

task CreateLaser1(x, y, angle, graphic, length, width, delay, startangle, offset){
	let obj = ObjShot_Create(OBJ_STRAIGHT_LASER);
	let angle2 = startangle;
	let spawnbullets = 1;
	let switchcount = 6;
	let count = 0;
	let x1 = x;
	let y1 = y;
	let dist = 10;
	let currdist = 0;
	let alpha = 255;
	let len = 0;
	ObjMove_SetPosition(obj, x, y);
	
	ObjStLaser_SetAngle(obj, angle);
	ObjShot_SetGraphic(obj, graphic);
	ObjShot_SetDelay(obj, 0);
	ObjLaser_SetLength(obj, 0);
	ObjLaser_SetRenderWidth(obj, width);
	ObjLaser_SetIntersectionWidth(obj, width);
	ObjShot_SetSpellResist(obj, true);
	ObjStLaser_SetSource(obj, false);
	ObjShot_SetAutoDelete(obj, false);
	ObjShot_Regist(obj);
	loop(delay/2){
		if(len<length){ len+=length/5; }
		ObjLaser_SetLength(obj, len);
		wait(1);
	}
	wait(delay/2);
	PlaySoundEffect("fire", 100, GetCenterX);
	if(!Obj_IsDeleted(obj)){
		while(currdist<length){
			if(count%switchcount==0){ spawnbullets*=-1; }
			if(!Obj_IsDeleted(obj) && spawnbullets == 1){
				x1 = ObjMove_GetX(obj)+currdist*cos(angle);
				y1 = ObjMove_GetY(obj)+currdist*sin(angle);
				let obj2 = CreateMDNHShotA2(x1, y1, -0.01111111111*10, angle2, (2/180), 2, FIRE[RED], 10);
				ObjRender_SetBlendType(obj2, BLEND_ADD_ARGB);
			}
		count++;
		if(spawnbullets==-1){ count+=0.5; }
		currdist+=dist;
		}
		angle2+=360/3/3;
		wait(45);
	}
	ascent(i in 0..15){
		ObjRender_SetAlpha(obj, 255-((i)*255/15));
		wait(1);
	}
	Obj_Delete(obj);
}