#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 2"]
#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);
	FlameAura(objBoss,[20,200,135],2);
	
	mainTask;	
} 

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(1000);
        }
	case(EV_REQUEST_TIMER){
		SetScriptResult(49);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
} 

let BossX = 0;
let BossY = 0;

@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	
	ObjEnemy_SetIntersectionCircleToShot(objBoss,BossX,BossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,BossX,BossY,24);
	
	yield;
} 

@Finalize {

} 

task mainTask { 
	RenderBoss;
	end;
	action;
} 

task action{
	ObjEnemy_SetDamageRate(objBoss,0,0);
		wait(60);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spell.wav", 150);
	ObjCutin_SetSpellcardS3("Insect \"March of the Worker Red Ants\"", 0, 255, 0);
	ObjCutin_LaunchS3(BYAKUREN, CutIn, "Normal");
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
		wait(120);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/charge.wav", 100);
	attack = true;
	 ConcentrationA1(objBoss,"FLOWER",60,3,120,[255,255,0]);
	wait(60);
	Attack;
	Move;
	Ring;
	wait(120);
	ObjEnemy_SetDamageRate(objBoss,20,20);
}

task Ring{
	let ang = rand(0,360);
	while(!Obj_IsDeleted(objBoss)){
	if(ObjMove_GetY(GetPlayerObjectID)<ObjMove_GetY(objBoss)){
		loop(26){
		CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 1, ang, RICE_GREEN, 1);
		ang += 360/26;
		}
	}
	wait(60);
}
}

task Move{
	loop{
		wait(280);
		ObjMove_SetDestAtWeight(objBoss,GetPlayerX+rand(-30,30),120+rand(-20,20),10,5);
	}
}

task Attack{
	loop{
			let angle = -0.5;
			let angle2 = 0;
		loop(17){
		let obj = CreateShotB2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), angle,-angle2,0,0.05,0,10, RICE_M_RED, 1);
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 100);
		Crawl(obj, 1);
		angle += 0.2;
		angle2 += 0.1;
		wait(5);
		}
		attack = false;
			wait(300);
			angle = 0.5;
			angle2 = 0;
		attack = true;
		loop(17){
		let obj = CreateShotB2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), angle,-angle2,0,0.05,0,10, RICE_M_RED, 1);
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 100);
		Crawl(obj, -1);
		angle -= 0.2;
		angle2 += 0.1;
		wait(5);
		}
		attack = false;
		wait(300);
		attack = true;
	}
}

task Work(obj,ang){
	while(!Obj_IsDeleted(obj)){
		CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 1.05, ObjMove_GetAngle(obj)+(90*ang), PELLET_1_RED, 1);
		CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 1, ObjMove_GetAngle(obj)+(90*ang), PELLET_1_RED, 1);
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 100);
		wait(80);
	}
}

task Crawl(obj,ang){
let yes = 0;		
    while(!Obj_IsDeleted(obj))
    {
	let spd = ObjMove_GetSpeed(obj)/2;
	if(trunc(ObjRender_GetX(obj))>384)
    {      
		if(yes==0){Work(obj, ang);yes=1;}
		ObjMove_SetX(obj,382);
        ObjMove_SetAngle(obj,90*ang);
		ObjMove_SetAcceleration(obj, 0);
		ObjMove_SetSpeed(obj,2);
    }
	if(trunc(ObjMove_GetX(obj))<0)
    {      
		if(yes==0){Work(obj, ang);yes=1;}
		ObjMove_SetX(obj,2);
        ObjMove_SetAngle(obj,270*ang);
		ObjMove_SetAcceleration(obj, 0);
		ObjMove_SetSpeed(obj,2);
    }
    if(trunc(ObjMove_GetY(obj))>448)
	{
		if(yes==0){Work(obj, ang);yes=1;}
		ObjMove_SetY(obj,446);
        ObjMove_SetAngle(obj,90+(90*ang));
		ObjMove_SetAcceleration(obj, 0);
		ObjMove_SetSpeed(obj,2);
    }
wait(1);
}
}

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 attframe = 0;
let imgBoss = GetCurrentScriptDirectory~"Functions/img/Wriggle.png";

task RenderBoss {
    let dir = 0;
    let speed = 0;
    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);}
        }

        frame+=0.5;
        frame2+=1;
        attframe++;
        if(frame > 24) {frame = 0;}
        yield;
    }
}