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


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


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


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	Indicator(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
	
	
    animSpell(objBoss);
	
	
	TFinalizeSpell(scene, objBoss);
    ObjMove_SetDestAtFrame(objBoss, rand(width/5, 4*width/5), rand(3*height/20, 80), 60);
	Maint;
}


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


task Maint{
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "../SFX/SPELLCARD.WAV", 100));
	Spell("Fantastic[r]Parametrisation[r]Festival", scene, objBoss);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	moveParam;
    params;
	laser;
}


task params{
    let shot = LoadEffect( GetCurrentScriptDirectory~ "../SFX/ATTACK5.WAV", 80);
    let alignAxes=true;
	let colour = [92, 94, 100, 104];
	if(rand(-1,1)<0){alignAxes= false;}
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		let ang = GetAngleToPlayer(objBoss);
		ascent(i in 0..4){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0) {return;}
		    ObjSound_Play(shot);
			if(alignAxes){rose(100+100*i, 100+ 30*i, ang, 2.5-i/3, 2+2*i, colour[i], true);}
			else if(! alignAxes){rose(160+100*i, 100+ 30*i, ang, 2.5-i/3, 5+2*i, colour[i], false);}
			wait(30);
		}
		alignAxes= ! alignAxes;
		wait(120);
	}
}


task rose(numBul, rad, ang, speed, numPetals, graphic, alignWithAxes){
    let r; let x; let y; let theta = 0;
    loop(numBul){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0) {return;}
	    r = rad*sin(numPetals*theta);
		if(alignWithAxes){ r = rad* cos(numPetals * theta);}
		x = cos(ang) * r*cos(theta) -sin(ang)* r*sin(theta);
		y = sin(ang) * r*cos(theta) +cos(ang)* r*sin(theta);
	    CreateShotA1(ObjMove_GetX(objBoss) + x, ObjMove_GetY(objBoss) + y, speed, theta, graphic, 20);
		theta+=360/numBul;
	}
}


task moveParam{
    wait(180);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	    ObjMove_SetDestAtFrame(objBoss, rand(3*width/10, 7*width/10), rand(3*height/20, 75), 60);
	    let t=0;
		while(t<240){t++; yield;}
		yield;
	}
}
	    
	    
task laser{
    let las = LoadEffect( GetCurrentScriptDirectory~ "../SFX/LASER2.WAV", 100);
	ObjSound_SetRestartEnable(las, true);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > life/2){yield;}
	let wvel = 1;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	    ascent(i in 0..2){
		    if(wvel == 1){DelayLaser(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 60+30*i, 500, 50, 13, 60);}
			else{DelayLaser(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 30*i, 500, 50, 13, 60);}
		}
		ascent(i in 0..2){
		    ObjSound_Play(las);
			let obj=CreateCurveLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 40, 30+30*i, 60, 20, 33, 0);
			ObjLaser_SetInvalidLength(obj, 10, 10);
			ObjCrLaser_SetTipDecrement(obj, 0.25);
			ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
			ObjRender_SetAlpha(obj, 255);
			control(obj, wvel, 60, 300);
		}
		ascent(i in 3..5){
		    if(wvel == 1){DelayLaser(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 60+30*i, 500, 50, 13, 60);}
			else{DelayLaser(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 30*i, 500, 50, 13, 60);}
		}
		ascent(i in 3..5){
			let obj=CreateCurveLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 30, 30+30*i, 60, 20, 33, 0);
			ObjLaser_SetInvalidLength(obj, 10, 10);
			ObjCrLaser_SetTipDecrement(obj, 0.25);
			ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
			ObjRender_SetAlpha(obj, 255);
			control(obj, wvel, 60, 300);
		}
		wvel= -wvel;
		wait(240);
	}
}