#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Tricky Nonspell 4"]
#Background["./NamelessPath.dnh"]
#Player["./../lib/Okina_player_by_Python/Okina Matara/Okina.dnh"]
#BGM["./../sfx/Cold Whirlwind of the Forest.ogg"]

#include "../lib/UNL_Shot/lib_ShotConstants.dnh"
#include "./../lib/includes/BossIncludes.dnh"
#include "./../lib/includes/EffectFunctions.dnh"

let GetDifficulty = 2;
let GetStage = 4;
let leftx = 20;
let rightx = GetStgFrameWidth-20;

@Event{
	alternative(GetEventType())
		case(EV_REQUEST_LIFE){ SetScriptResult(3000); }
		case(EV_REQUEST_TIMER){ SetScriptResult(40); }
}

@Initialize{

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(objEnemy, 180, 50, 75);
	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); 
	
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 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 angle = -90;
	let one = 1;
	let color = 0;
	wait(135);
	loop{
		if(color%3==0){ 
			PlaySoundEffect("charge2", 100, GetEnemyX);
			TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 255, 255, BLEND_ADD_ARGB, 25);
			ObjAnim_SetAttackA1(objEnemy, true);
			wait(45); 
		}
		loop(3){
			loop(12){
				let obj = CreateDNHShotA1(GetEnemyX, GetEnemyY, 2.75, angle, [CIRCLE_L[ORANGE], CIRCLE_L[GREEN], CIRCLE_L[BLUE]][color%3], 15);
				RunExplodeCode(obj, true, true, true, false);
				PlaySoundEffect("shot3", 100, GetEnemyX);
				angle+=360/12;
			}
			wait(45);
			angle+=360/12/3*one;
		}
		ObjAnim_SetAttackA1(objEnemy, false);
		color++;
		one*=-1;
		wait(30);
		ExMove(objEnemy, GetCenterX, rand(20, 30), rand(110, 130), 40, 3, 15);
		wait(60);
		ObjAnim_SetAttackA1(objEnemy, true);
		wait(30);
		
	}	
}

task RunExplodeCode(obj, left, top, right, bottom){
	let angle = rand(-25, 25);
	let xspeed = 0;
        while(!Obj_IsDeleted(obj)){
            if(trunc(ObjMove_GetX(obj))>GetStgFrameWidth && right == true && ObjMove_GetY(obj) < 300){
               PlaySoundEffect("shot", 75, ObjMove_GetX(obj));
					if(ObjShot_GetImageID(obj) == CIRCLE_L[ORANGE]){
						loop(7){
							CreateDNHShotA2(GetStgFrameWidth, ObjMove_GetY(obj), 3, AngleBetweenPoints(GetStgFrameWidth, ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+angle, -0.05, 1.65, CIRCLE_S[ORANGE], 10);
							angle+=360/7;
							}
							angle+=360/14;
						loop(7){
							CreateDNHShotA2(GetStgFrameWidth, ObjMove_GetY(obj), 3, AngleBetweenPoints(GetStgFrameWidth, ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+angle, -0.05, 1.15, CIRCLE_S[ORANGE], 10);
							angle+=360/7;
							}
						}
					else if(ObjShot_GetImageID(obj) == CIRCLE_L[BLUE]){
						loop(8){
							let obj2 = CreateDNHShotA2(GetStgFrameWidth, ObjMove_GetY(obj), 1, AngleBetweenPoints(GetStgFrameWidth, ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+angle, -0.05, 1.35, RICE[BLUE], 10);
							xspeed = ObjMove_GetSpeed(obj2)*cos(ObjMove_GetAngle(obj2));
							ObjMove_AddPatternB2(obj2, 10, NO_CHANGE, NO_CHANGE, -xspeed/180, 0.010, 0, 2.55); 
							angle+=360/8;
							}
						}
					else if(ObjShot_GetImageID(obj) == CIRCLE_L[GREEN]){
						loop(6){
							ascent(i in 0..8){
							let obj2 = CreateDNHShotA2(GetStgFrameWidth, ObjMove_GetY(obj), 1, AngleBetweenPoints(GetStgFrameWidth, ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+angle, -0.05, 2.25, ARROWHEAD[GREEN], 2+(i*5));
							WavyBullet(obj2, angle, 2+(i*4));
							}
							angle+=360/6;
							}
						}
                return;
                }
            if(trunc(ObjMove_GetX(obj))<0 && left == true && ObjMove_GetY(obj) < 300){      
                PlaySoundEffect("shot", 75, ObjMove_GetX(obj)+80);
				if(ObjShot_GetImageID(obj) == CIRCLE_L[ORANGE]){
						loop(7){
							CreateDNHShotA2(0, ObjMove_GetY(obj), 3, AngleBetweenPoints(GetStgFrameWidth, ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+angle, -0.05, 1.65, CIRCLE_S[ORANGE], 10);
							angle+=360/7;
							}
						angle+=360/14;
						loop(7){
							CreateDNHShotA2(0, ObjMove_GetY(obj), 3, AngleBetweenPoints(GetStgFrameWidth, ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+angle, -0.05, 1.15, CIRCLE_S[ORANGE], 10);
							angle+=360/7;
							}
						}
				else if(ObjShot_GetImageID(obj) == CIRCLE_L[BLUE]){
						loop(8){
							let obj2 = CreateDNHShotA2(0, ObjMove_GetY(obj), 1, AngleBetweenPoints(GetStgFrameWidth, ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+angle, -0.05, 1.35, RICE[BLUE], 10);
							xspeed = ObjMove_GetSpeed(obj2)*cos(ObjMove_GetAngle(obj2));
							ObjMove_AddPatternB2(obj2, 10, NO_CHANGE, NO_CHANGE, -xspeed/180, 0.010, 0, 2.55); 
							angle+=360/8;
							}
						}
				else if(ObjShot_GetImageID(obj) == CIRCLE_L[GREEN]){
						loop(6){
							ascent(i in 0..8){
							let obj2 = CreateDNHShotA2(0, ObjMove_GetY(obj), 1, AngleBetweenPoints(GetStgFrameWidth, ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+angle, -0.05, 2.25, ARROWHEAD[GREEN], 2+(i*5));
							WavyBullet(obj2, angle, 2+(i*4));
							}
							angle+=360/6;
							}
						}
                return;
                }       
			if(trunc(ObjMove_GetY(obj))>448 && bottom==true){}
			if(trunc(ObjMove_GetY(obj))<0 && top == true){      
                    PlaySoundEffect("shot", 75, ObjMove_GetX(obj));
				if(ObjShot_GetImageID(obj) == CIRCLE_L[ORANGE]){
						loop(7){
							CreateDNHShotA2(ObjMove_GetX(obj), 0, 3, AngleBetweenPoints(GetStgFrameWidth, ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+angle, -0.05, 1.65, CIRCLE_S[ORANGE], 10);
							angle+=360/7;
							}
						angle+=360/17;
						loop(7){
							CreateDNHShotA2(ObjMove_GetX(obj), 0, 3, AngleBetweenPoints(GetStgFrameWidth, ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+angle, -0.05, 1.15, CIRCLE_S[ORANGE], 10);
							angle+=360/7;
							}
						}
				else if(ObjShot_GetImageID(obj) == CIRCLE_L[BLUE]){
						loop(8){
							let obj2 = CreateDNHShotA2(ObjMove_GetX(obj), 0, 1, AngleBetweenPoints(GetStgFrameWidth, ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+angle, -0.05, 1.35, RICE[BLUE], 10);
							xspeed = ObjMove_GetSpeed(obj2)*cos(ObjMove_GetAngle(obj2));
							ObjMove_AddPatternB2(obj2, 10, NO_CHANGE, NO_CHANGE, -xspeed/180, 0.010, 0, 2.55); 
							angle+=360/8;
							}
						}
				else if(ObjShot_GetImageID(obj) == CIRCLE_L[GREEN]){
						loop(6){
							ascent(i in 0..8){
							let obj2 = CreateDNHShotA2(ObjMove_GetX(obj), 0, 1, AngleBetweenPoints(GetStgFrameWidth, ObjMove_GetY(obj), GetEnemyX, GetEnemyY)+angle, -0.05, 2.25, ARROWHEAD[GREEN], 2+(i*5));
							WavyBullet(obj2, angle, 2+(i*4));
							}
							angle+=360/6;
							}
						}
                return;
                }
            yield;
        }
}

task WavyBullet(obj2, angle, delay){
	let variance = 0;
	let originalangle = angle;
	loop(delay){ yield; }
	while(!Obj_IsDeleted(obj2)){
	ObjMove_SetAngle(obj2, originalangle+10*cos(variance));
	variance+=8;
	wait(1);
	}
}