#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Nonspell 3"]
#Text["Wooooooop"]

let objBoss;
#include "./Functions/Index.txt"


@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(2500);
        }
	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/charge.wav", 100);
	attack = true;
	 ConcentrationA1(objBoss,"FLOWER",60,3,120,[255,255,0]);
	wait(60);
	Attack;
	Ring;
	ObjEnemy_SetDamageRate(objBoss,60,60);
}

task Ring{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE) > 0){
let angle = rand(0,360);
	loop(32){
	CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 2, angle, 0, 2, BALL_S_BLUE, 5);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 70);
	angle += 360/32;
	}
	wait(70);
}
}

task Attack{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE) > 0){
	let angle = GetAngleToPlayer(objBoss)+70;
	let speed = 1;	
	let time = 30;
	loop(4){
	loop(15){
	let obj = CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), speed, angle, -0.01, 0, RICE_YELLOW, 5);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 70);
	Redirect(obj,-1,time);
	angle += 6;
	}
	time += 20;
	angle = GetAngleToPlayer(objBoss)+70;
	speed += 0.1;
	}
	time = 30;
	angle = GetAngleToPlayer(objBoss)-70;
	speed = 1;
	wait(120);
	loop(4){
	loop(15){
	let obj = CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), speed, angle, -0.01, 0, RICE_YELLOW, 5);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 70);
	Redirect(obj,1,time);
	angle -= 6;
	}
	angle = GetAngleToPlayer(objBoss)-70;
	speed += 0.1;
	time += 20;
	}
	wait(60);
	ObjMove_SetDestAtWeight(objBoss,GetPlayerX+rand(-30,30),120+rand(-20,20),10,5);
	wait(60);
}
}

task Redirect(obj,mult,time){
    while(ObjMove_GetSpeed(obj) > 0){yield;}
    wait(time);
	ObjMove_AddPatternA4(obj, 0, 0, ObjMove_GetAngle(obj)+(120*mult), 0.03, 0, 3, 0, NO_CHANGE);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spirit.wav", 50);
}


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;
    }
}