#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 5"]
#Text["Wooooooop"]

let objBoss;

#include "./Functions/Index.txt"

let CutIn = GetCurrentScriptDirectory~"./Functions/img/Nightbug.png";

@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);

	ObjMove_SetDestAtSpeed(objBoss,384/2,150,5);
	
	mainTask;	
}

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(6000);
        }
	case(EV_REQUEST_TIMER){
		SetScriptResult(70);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
	case(EV_REQUEST_IS_DURABLE_SPELL){
		SetScriptResult(true);
	}
} 

let BossX = 0;
let BossY = 0;

@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	
	yield;
} 

@Finalize {

} 

task mainTask { 
	RenderBoss;
	end;
	action;
} 

task action{
	ObjEnemy_SetDamageRate(objBoss,0,0);
		wait(30);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spell.wav", 150);
	ObjCutin_SetSpellcardS3("\"Wriggle's Lantern\"", 0, 255, 0);
	ObjCutin_LaunchS3(BYAKUREN, CutIn, "Normal");
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
		wait(120);
	survival = true;
		wait(60);
	Attack;
}

task Attack{
	loop(10){
	let obj = CreateShotA2(384/2+rand(-150,150),300+rand(-200,150), 0.1, rand(0,360), 0, 2, ORB_M_YELLOW, 0);
	Alpha(obj,false,false,false);
	wait(60);
}
wait(200);
	let Wriggle = CreateShotA2(384/2,150, 0, 90, 0, 2, ORB_M_LEMON, 0);
	ObjShot_SetSpellResist(Wriggle, true);
	BossMove(Wriggle);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spirit.wav", 100);
	ExplosionA1(Wriggle,"Flower",[255,200,0],[255,200,0]);
wait(120);
	loop(10){
	let obj = CreateShotA2(384/2+rand(-150,150),300+rand(-200,150), 0.1, rand(0,360), 0, 2, ORB_M_YELLOW, 0);
	Alpha(obj,true,false,false);
	wait(60);
}
wait(120);
	loop(10){
	let obj = CreateShotA2(384/2+rand(-150,150),300+rand(-200,150), 0.1, rand(0,360), 0, 2, ORB_M_YELLOW, 0);
	Alpha(obj,true,true,false);
	wait(60);
}
wait(200);
	ExplosionA1(Wriggle,"Flower",[255,200,0],[255,200,0]);
	ObjShot_SetGraphic(Wriggle,ORB_L_LEMON);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spirit.wav", 100);
wait(120);
	loop(10){
	let obj = CreateShotA2(GetPlayerX+rand(-100,100),GetPlayerY+rand(-100,100), 0.1, rand(0,360), 0, 2, ORB_M_YELLOW, 0);
	Alpha(obj,true,true,true);
	wait(60);
}
wait(120);
	GOGOGOGO(Wriggle);
	STAHP;
	loop(5){
	let obj = CreateShotA2(GetPlayerX+rand(-100,100),GetPlayerY+rand(-100,100), 0.1, rand(0,360), 0, 2, ORB_M_YELLOW, 0);
	Alpha(obj,true,true,true);
	wait(60);
}
	loop{
	let obj = CreateShotA2(GetPlayerX+rand(-50,50),GetPlayerY+rand(-50,50), 0.1, rand(0,360), 0, 2, ORB_M_YELLOW, 0);
	Alpha(obj,true,true,true);
	wait(20);
}
}

task STAHP{
	wait(60);
	loop(5){
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/timeout.wav", 255);
	wait(60);
	}
	loop(6){
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/timeout2.wav", 255);
	wait(60);
	}
}

task BossMove(wut){
	loop{
	let x = ObjMove_GetX(wut);
	let y = ObjMove_GetY(wut);
	ObjMove_SetPosition(objBoss,x,y);
	wait(1);
	}
}

task GOGOGOGO(lel){
	let target = fam(GetPlayerX,GetPlayerY,1,1,false);
	let angle = 0;
	loop{
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/option.wav", 100);
		loop(60){
			ObjMove_SetPosition(target,GetPlayerX,GetPlayerY);
			wait(1);
		}
		ObjMove_SetDestAtWeight(lel,GetPlayerX,GetPlayerY,10,7);
		wait(50);
		loop(16){
		let kek = CreateShotA2(ObjMove_GetX(lel),ObjMove_GetY(lel), 1, angle, 0, 2, BALL_S_LEMON, 5);
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 50);
		angle += 360/16;
		}
		angle += 360/16;
		loop(16){
		let kek = CreateShotA2(ObjMove_GetX(lel),ObjMove_GetY(lel), 0.8, angle, 0, 2, BALL_S_LEMON, 5);
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 50);
		angle += 360/16;
		}
	}
}

task Alpha(t,check,check2,check3){
	let alpha = 0;
	let ringnum = 12;
	let angle = rand(0,360);
	ObjRender_SetAlpha(t,0);
	ObjShot_SetIntersectionEnable(t,0);
	while(!Obj_IsDeleted(t)){
		ObjRender_SetAlpha(t,alpha);
		alpha+=2;
		wait(1);
		if(alpha>255){break;}
	}
	if(Obj_IsDeleted(t)){return;}
	ObjMove_SetSpeed(t,0);
	ObjShot_SetIntersectionEnable(t,1);
	ObjShot_SetGraphic(t,ORB_L_YELLOW);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spirit.wav", 50);
	DefaultCircle(t,5,[255,255,0],25,25,25);
	if(check2==true){
	loop(14){
	let lel = CreateShotA2(ObjMove_GetX(t),ObjMove_GetY(t), 2, angle, 0, 2, RICE_YELLOW, 5);
	if(check3==true){
	let lel2 = CreateShotA2(ObjMove_GetX(t),ObjMove_GetY(t), 2.2, angle, 0, 2, RICE_YELLOW, 5);
		}
	angle += 360/14;
	}
	}
	wait(60);
	ObjMove_AddPatternA4(t, 0, 0, GetAngleToPlayer(t), 0.05, 0, 4, 0, ORB_L_YELLOW);
	wait(70);
	if(check==true){ObjMove_AddPatternA4(t, 0, ObjMove_GetSpeed(t), ObjMove_GetAngle(t), -0.03, 0, 0, 0, ORB_L_YELLOW);}
	wait(120);
	if(check==true){ObjMove_AddPatternA4(t, 0, 0, GetAngleToPlayer(t), 0.05, 0, 4, 0, ORB_L_YELLOW);}
}

task end{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
}



let attack = false;
let frame = 0;
let frame2 = 0;
let survival = false;
let attframe = 0;
let imgBoss = GetCurrentScriptDirectory~"Functions/img/Wriggle.png";

task RenderBoss {
    let dir = 0;
    let speed = 0;
	let alpha = 255;
    let imgCreate = ObjPrim_Create(OBJ_SPRITE_2D);

    while(!Obj_IsDeleted(objBoss)) {
        ObjPrim_SetTexture(imgCreate,imgBoss);
        ObjSprite2D_SetDestCenter(imgCreate);
        ObjRender_SetScaleXYZ(imgCreate,1,1,0);
        Obj_SetRenderPriorityI(imgCreate,25);

        ObjRender_SetPosition(imgCreate,BossX,BossY,0);

        dir = ObjMove_GetAngle(objBoss);
        speed = ObjMove_GetSpeed(objBoss);

        if(speed == 0 && attack == false) {ObjRender_SetAngleXYZ(imgCreate,0,0,0);
        if(frame < 4) {ObjSprite2D_SetSourceRect(imgCreate,0,0,48,64);}
        if(frame >= 4 && frame < 8) {ObjSprite2D_SetSourceRect(imgCreate,48,0,96,64);}
        if(frame >= 8 && frame < 12) {ObjSprite2D_SetSourceRect(imgCreate,96,0,144,64);}
        if(frame >= 12 && frame < 16) {ObjSprite2D_SetSourceRect(imgCreate,144,0,192,64);}
        if(frame >= 16) {ObjSprite2D_SetSourceRect(imgCreate,192,0,240,64);}
        frame2 = 0;
        }
        else if(cos(dir) > 0) {ObjRender_SetAngleXYZ(imgCreate,0,0,0);
        if(frame2 < 4) {ObjSprite2D_SetSourceRect(imgCreate,0,64,48,128);}
        if(frame2 >= 4 && frame2 < 8) {ObjSprite2D_SetSourceRect(imgCreate,48,64,96,128);}
        if(frame2 >= 8 && frame2 < 12) {ObjSprite2D_SetSourceRect(imgCreate,96,64,144,128);}
        if(frame2 >= 16) {ObjSprite2D_SetSourceRect(imgCreate,144,64,192,128);}
        }
        else if(cos(dir) < 0) {ObjRender_SetAngleXYZ(imgCreate,0,180,0);
        if(frame2 < 4) {ObjSprite2D_SetSourceRect(imgCreate,0,64,48,128);}
        if(frame2 >= 4 && frame2 < 8) {ObjSprite2D_SetSourceRect(imgCreate,48,64,96,128);}
        if(frame2 >= 8 && frame2 < 12) {ObjSprite2D_SetSourceRect(imgCreate,96,64,144,128);}
        if(frame2 >= 16) {ObjSprite2D_SetSourceRect(imgCreate,144,64,192,128);}
        }
if(attack == true && speed == 0) {ObjRender_SetAngleXYZ(imgCreate,0,0,0);
        if(attframe < 4) {ObjSprite2D_SetSourceRect(imgCreate,0,128,48,192);}
        if(attframe >= 4/* && attframe < 8*/) {ObjSprite2D_SetSourceRect(imgCreate,48,128,96,192);}
        }
		if(survival == true){
		loop{
		ObjRender_SetAlpha(imgCreate,alpha);
		alpha-=5;
		yield;
		}
		}
		
        frame+=0.5;
        frame2+=1;
        attframe++;
        if(frame > 24) {frame = 0;}
        yield;
    }
}