#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Nonspell 2"]
#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);
	ObjEnemy_SetDamageRate(objBoss,60,60);
	Attack;
}

task Attack{
	let angle = 0;	
	loop(5){
	let fam1 = fam(BossX,BossY,1,4,false);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/option.wav",100);
	Go(fam1,angle);
	Shooty2(fam1);
	wait(5);
	angle -= 45;
	}
	wait(50);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE) > 0){
	angle = 45;	
	loop(5){
	let fam1 = fam(BossX,BossY,1,2,false);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/option.wav",100);
	Go(fam1,angle);
	Shooty(fam1);
	wait(5);
	angle += 22.5;
	}
	angle = 180-45;
	attack = false;
	wait(200);
	attack = true;
	loop(5){
	let fam2 = fam(BossX,BossY,1,2,false);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/option.wav",100);
	Go(fam2,angle);
	Shooty(fam2);
	wait(5);
	angle -= 22.5;
	}
	attack = false;
	wait(200);
	attack = true;
	}
}

task Shooty2(obj){
    while(ObjMove_GetSpeed(obj) > 0){yield;}
	wait(rand(30,60));
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE) > 0){
	let angleT = rand(0, 360);
    loop(18){
        CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 2, angleT, RICE_GREEN, 0);
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav",100);
        angleT += 360/18;
    }
	wait(60);
	}
}

task Shooty(obj){
    while(ObjMove_GetSpeed(obj) > 0){yield;}
    loop(12){
        let obj2 = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(1,3), rand(0,360), BALL_S_YELLOW, 0);
		TDelete(obj2);
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav",100);
		wait(2);
    }
    Obj_Delete(obj);
}

task Go(fam, ang){
	let angle = ang;
	ObjMove_SetAngle(fam,angle);
	ObjMove_SetSpeed(fam,3.5);
	ObjMove_SetAcceleration(fam,-0.05);
	ObjMove_SetMaxSpeed(fam,0);
}

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

task TDelete(obj){
if(ObjMove_GetX(obj) == 0 || ObjMove_GetY(obj) == 0) { Obj_Delete(obj); } 
}

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