//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Circles"]
#include "./../ZUNShot/ZUNShot_Const.txt"
#include "./sharedfuncMario.dnh"


let objBoss;
let width = 384;
let height = 448;
let life = 4000;
let scene= GetEnemyBossSceneObjectID();


@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(life);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(40);
    }
    case(EV_REQUEST_SPELL_SCORE){
        SetScriptResult(1000000);
    }
}


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	Indicator(objBoss);
	
    animNon(objBoss);
	
	TFinalizeNon(objBoss);
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, 90, 60);
	Main;
}


@MainLoop{
    ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); 
    ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 28);
    yield;
}

task Main{
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 100, 60);
    let shot = LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK5.WAV", 60);
	ObjSound_SetRestartEnable(shot, true);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "../SFX/Charge.WAV", 90));
    wait(60);
	moveCircle;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	    let ang = rand(0,360);
		ObjSound_Play(shot);
	    ascent(i in 0..7){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0) {return;}
		    let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)-20, 5, ang + i* 360/7, 324, 0);
			transform(obj);
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 5*life/6){wait(100);}
		else if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > life/3){wait(70);}
		else{wait(50);}
	}
}
    
task transform(obj){
    let twinkle = LoadEffect(GetCurrentScriptDirectory~ "../SFX/TWINKLE.WAV", 70);
	ObjSound_SetRestartEnable(twinkle, true);
	let missile = LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK1.WAV", 70);
	ObjSound_SetRestartEnable(missile, true);
	let laser = LoadEffect(GetCurrentScriptDirectory~ "../SFX/LASER.WAV", 70);
	ObjSound_SetRestartEnable(laser, true);
    while(ObjMove_GetX(obj)>0 && ObjMove_GetX(obj) <width && ObjMove_GetY(obj)>0 && ObjMove_GetY(obj) < height){yield;}
	let x = ObjMove_GetX(obj);
	let y = ObjMove_GetY(obj);
	wait(10);
	if(x<=0){ascent(i in 0..3){ObjSound_Play(missile); CreateShotA1(0, y, 2+i, 45-45*i, 198, 0);}}
	if(x>=width){ascent(i in 0..3){ObjSound_Play(missile); CreateShotA1(width, y, 2+i, 135+45*i, 198, 0);}}
	if(y>= height){let objl=CreateStraightLaserA1(x, height +20, -90, 500, 25, 60, 22, 30); wait(30); ObjSound_Play(laser); wait(50); Obj_Delete(objl);}
	if(y<=0){ascent(i in 0..4){ObjSound_Play(twinkle); CreateShotA2(x, 0, 1, rand(70, 110), rand(0, 0.02), 5, 226, 0);}}
}

task moveCircle{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	    ObjMove_SetDestAtFrame(objBoss, rand(2*width/5, 4*width/5), rand(height/5, height/2 +10), 60);
		wait(120);
	}
}