//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Sine wave"]
#include "./../ZUNShot/ZUNShot_Const.txt"
#include "./sharedfuncJames.dnh"


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


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


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	
	animNon(objBoss);
	TFinalizeNon(objBoss);
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, height/5, 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{
    Indicator(objBoss);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
    wait(60);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/Charge.WAV", 80));
	wait(60);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK5.WAV", 80));
	let obj1= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjShot_SetSpellResist(obj1, true);
	ObjMove_SetDestAtFrame(obj1, width/4, 0, 30);
	let obj2= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjShot_SetSpellResist(obj2, true);
	ObjMove_SetDestAtFrame(obj2, 3*width/4, 0, 30);
	let obj3= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjShot_SetSpellResist(obj3, true);
	ObjMove_SetDestAtFrame(obj3, 40, height, 60);
	let obj4= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjShot_SetSpellResist(obj4, true);
	ObjMove_SetDestAtFrame(obj4, width-40, height, 60);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/CAUTION.WAV", 90));
	wait(180);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
	let col1 = truncate(rand(43,51));
	let col2 = truncate(rand(51, 57));
	shot(obj1, col1 ,col2); shot(obj2, col1, col2); shot(obj3, col1, col2); shot(obj4, col1, col2);
	wait(120);
	moveEdge(obj1, 180, 0.5, width, 0, 60, objBoss, 0);
	moveEdge(obj2, 0, 0.5, 0, width, 60, objBoss, 0);
	moveEdge(obj3, 0, 1, 0, width, 60, objBoss, 0);
	moveEdge(obj4, 180, 1, width, 0, 60, objBoss, 0);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 2*life/3){yield;}
	shoot;
}



task shoot{
    let s = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK5.WAV", 70);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	    ObjSound_Play(s);
	    launch(objBoss, 20, GetAngleToPlayer(objBoss), 25, 2, -0.05, -0.01, 1, 138, 0, false, 0,0,0,0,0,false, objBoss);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > life/3){wait(150);}
		else{wait(80);}
	}
}

task shot(obj, col1, col2){
    let period; let t; let s = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK4.WAV", 30);
    if(ObjMove_GetY(obj) < height/2){	
	    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		    let dir = 1;
			if(rand(0,1)>=1/2){dir = -1;}
			period= rand(50,70);
			shotSound(s, period, 45, 3, objBoss,0);
			t = 0;
			while(t<period){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
				CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 3, 90 + 6*sin(dir*8*t), col1, 0);
				t++;
				yield;
			}
			wait(20);
		}
	}
	else{
	    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		    let dir = 1;
			if(rand(0,1)>=1/2){dir = -1;}
			period= rand(60,90);
			shotSound(s, period, 45, 3, objBoss,0);
			t = 0;
			while(t<period){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
				CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 2, -90+ 6*sin(dir*8*t), col2, 0);
				t++;
				yield;
			}
			wait(20);
		}
	}
}