#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Kosuke Nonspell 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(3000); }
		case(EV_REQUEST_TIMER){ SetScriptResult(38); }
}

@Initialize{

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(objEnemy, 180, 60, 75);
	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 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	loop(4){ ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*5, 128*6, 128*5+128); }
	descent(x in 0..2){ 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); 
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, GetClipMinY+120, 60);
	
	SetAutoDeleteObject(true);
	attack1;
	End;
}

task End {
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0) { yield; }
	//DeleteShotWave(1,GetX,GetY,20);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	PlaySoundEffect("end",60,GetCenterX);
	Obj_Delete(objEnemy);
	CloseScript(GetOwnScriptID);
}

task attack1{

	let startangle = 0;
	wait(90);
	
	loop{
	
		PlaySoundEffect("charge2", 100, GetEnemyX);
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 165, 0, BLEND_ADD_ARGB, 25);
		ObjAnim_SetAttackA2(objEnemy, 45);
		
		wait(45);
		
		PlaySoundEffect("slash", 100, GetEnemyX);
		CreateLaser1(-40, 30, 0, CIRCLE_S[RED], 384+80, 15, 30, startangle);
		
		wait(180);
		
		ExMove(objEnemy, GetPlayerX, rand(20, 30), 120+rand(-5, 5), 20, 3, 15);
		
		wait(60);
		
		startangle+=45+rand(-15, 15);
	}	
}

task CreateLaser1(x, y, angle, graphic, length, width, delay, startangle){
	let obj = ObjShot_Create(OBJ_STRAIGHT_LASER);
	let angle2 = startangle;
	let one = 1;
	let x1 = x;
	let y1 = y;
	let numbullets = 50;
	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_Regist(obj);
	loop(delay/2){
		if(len<length){ len+=length/5; }
		ObjLaser_SetLength(obj, len);
		wait(1);
	}
	wait(delay/2);
	if(!Obj_IsDeleted(obj)){
	loop(3){
		PlaySoundEffect("shot3", 100, GetCenterX);
			ascent(i in 0..numbullets){
				if(!Obj_IsDeleted(obj)){
					x1 = ObjMove_GetX(obj)+(i*length/numbullets)*cos(angle);
					y1 = ObjMove_GetY(obj)+(i*length/numbullets)*sin(angle);
					if(x1>-4 && x1<389){
						loop(3){
							CreateShotA2(x1, y1, 4, angle2+(i*22.5), -0.055, 2.25, [KUNAI[RED], KUNAI[ORANGE]][graphic%2], 10);
							CreateDNHShotA2(x1, y1, 4, angle2+(i*22.5), -0.055, 1.25, [KUNAI[RED], KUNAI[ORANGE]][(graphic+1)%2], 10);
							angle2+=360/3;
						}
					}
					else{
						loop(3){
							CreateShotA2(x1, y1, 4, angle2+(i*22.5), -0.055, 2.25, [KUNAI[RED], KUNAI[ORANGE]][graphic%2], 10);
							CreateShotA2(x1, y1, 4, angle2+(i*22.5), -0.055, 1.25, [KUNAI[RED], KUNAI[ORANGE]][(graphic+1)%2], 10);
							angle2+=360/3;
						}
					}
				}
			}
		angle2+=360/3/3;
		wait(45);
	}
	}
	ascent(i in 0..15){
		ObjRender_SetAlpha(obj, 255-((i)*255/15));
		wait(1);
	}
	Obj_Delete(obj);
}