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

@Initialize{

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(objEnemy, 90, 70, 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 2..4){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	loop(4){ 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); 
	
	ObjMove_SetPosition(objEnemy, GetCenterX, GetClipMinY+120);

	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{
	angle = 180;
	let radius = 85;
	let pointx = 0;
	let pointy = 0;
	let numtimes = 8;
	wait(30);
	loop{
	
		PlaySoundEffect("charge2", 100, GetEnemyX);
		if(color%2==0){ TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 0, BLEND_ADD_ARGB, 25); }
		else{ TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 165, 0, BLEND_ADD_ARGB, 25); }
		ObjAnim_SetAttackA2(objEnemy, 30);
		
		wait(30);
		
		loop(numtimes){
			pointx = GetEnemyX+radius*cos(angle);
			pointy = GetEnemyY+radius*sin(angle);
			PlaySoundEffect("slash", 100, GetEnemyX);
			CreateLaser1(pointx-(radius/2)*cos(angle-90), pointy-(radius/2)*sin(angle-90), angle-90, [CIRCLE_S[RED], CIRCLE_S[ORANGE]][color%2], radius, 10, 90, switch);
			angle-=(360/numtimes)*switch;
			wait(6);
		}
		
		wait(75);
		
		ExMove(objEnemy, GetPlayerX, rand(20, 30), 120+rand(-5, 5), 20, 3, 15);
		
		wait(45);
		
		angle = [0, 180][color%2] + rand(-10, 10);
		color++;
		switch*=-1;
	}	
}

task CreateLaser1(x, y, angle, graphic, length, width, delay, dir){
	let obj = ObjShot_Create(OBJ_STRAIGHT_LASER);
	let angle2 = GetAngleToPoint(x, y, GetEnemyX, GetEnemyY);
	let one = 1;
	let x1 = x;
	let y1 = y;
	let numbullets = 16;
	let alpha = 255;
	let len = 0;
	let waitincrement = 0;
	let shotgraphic = KUNAI[RED];
	if(dir==-1){ shotgraphic = KUNAI[ORANGE]; }
	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);
	}
	PlaySoundEffect("ping", 100, x);
	if(dir==1){
		ascent(i in 0..numbullets){
			if(!Obj_IsDeleted(obj)){
				loop(3){
					CreateDNHShotA2(ObjMove_GetX(obj)+(i*length/numbullets)*cos(angle), ObjMove_GetY(obj)+(i*length/numbullets)*sin(angle), 1.25, angle2+(i*22.5), 0.04, 2.6, shotgraphic, 10);
					angle2+=360/3;
				}
				wait(3);
				waitincrement++;
			}
			ObjRender_SetAlpha(obj, 255-((i+2)*255/numbullets));
		}
	}
	else{
	descent(i in 0..numbullets){
			if(!Obj_IsDeleted(obj)){
				loop(3){
					CreateDNHShotA2(ObjMove_GetX(obj)+(i*length/numbullets)*cos(angle), ObjMove_GetY(obj)+(i*length/numbullets)*sin(angle), 1.25, angle2+(i*22.5), 0.04, 2.6, shotgraphic, 10);
					angle2+=360/3;
				}
				wait(3);
				waitincrement++;
			}
			ObjRender_SetAlpha(obj, 0+((i+2)*255/numbullets));
		}
	ObjRender_SetAlpha(obj, 0);
	}
	wait(delay/2);
	Obj_Delete(obj);
}