//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Hahn-Banachstension"]
#include "./../ZUNShot/ZUNShot_Const.txt"
#include "./sharedfuncJames.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(80);
    }
    case(EV_REQUEST_SPELL_SCORE){
        SetScriptResult(1000000);
    }
}


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
	
	animSpell(objBoss);
	TFinalizeSpell(scene, objBoss);
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, 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);
    progress;
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/SPELLCARD.WAV", 100));
	Spell("Hahn-Banachstension", scene, objBoss);
    wait(240);
	let obj1= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjShot_SetSpellResist(obj1, true);
	ObjMove_SetDestAtFrame(obj1, 170, 220, 60);
	wait(60);
	move(objBoss, 2*width/5, 3*width/5, 2*height/5, 2*height/5+1, 180, objBoss, 0);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
    balls;
	wait(320);
	ObjEnemy_SetDamageRate(objBoss, 60, 30);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>3*life/4){yield;}
	snakes;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/2){yield;}
	let col = [270,281];
	let i=0;
	let shot =LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK5.WAV", 70);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ObjSound_Play(shot);
	    launch(objBoss, 80, 270, 89, 3, -0.04, -0.02, 0, col[i%2], 0, true, 30, 0, 0.01, 0.02, 1, true, objBoss);
		i++;
		wait(240);
	}
}

task progress{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>3*life/4){yield;}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK3.WAV", 80));
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/2){yield;}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK3.WAV", 80));
}

task snakes{
    let s =LoadEffect(GetCurrentScriptDirectory~ "./../SFX/snakehiss.WAV", 90);
    let j = 4; let inc = -1; let cycle = 1;
	let ang1;
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let angIni = rand(10, 20);
		ObjSound_Play(s);
	    if(j>0){
		    ang1 = -90-cycle *angIni;
		    ascent(i in 0..6){
			    let obj = CreateShotA1(width/2-40*j, height, 4, ang1, 333, 3*i); controlWvel(obj, cycle * 3, 20, 400, 4);
				ObjShot_SetSpellResist(obj, true);
				Obj_SetRenderPriorityI(obj, 80);
			}
			ascent(i in 0..6){
			    let obj = CreateShotA1(width/2+40*j, height, 4, ang1, 333, 3*i); controlWvel(obj, cycle * 3, 20, 400, 4);
				ObjShot_SetSpellResist(obj, true);
				Obj_SetRenderPriorityI(obj, 80);
			}
			j+= inc;
			if(j == 4){inc*=-1;}
		}
		else{
		    if(cycle==1){ang1= -90-angIni;} else{ang1 = -90 + angIni;}
		    ascent(i in 0..6){
			    let obj = CreateShotA1(width/2, height, 4, ang1, 333, 3*i); controlWvel(obj, cycle*3, 20, 400, 4);
				ObjShot_SetSpellResist(obj, true);
				Obj_SetRenderPriorityI(obj, 80);
			} 
			inc*=-1; j+= inc;
			cycle*=-1;
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/4){wait(90);}
		else{wait(50);}
	}
}


//Cool-looking blocking technique!	    

/*task snakes{
    let j = 4; let inc = -1; let dir = 1; let ang1; let ang2;
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    if(j>0){
		    if(dir>0){ang1 = 90-15; ang2 = 90+15;} else{ang1 = 90+15; ang2 = 90-15;}
		    ascent(i in 0..6){
			    let obj = CreateShotA1(width/2+40*j, height, 4, -115, 333, 3*i); controlWvel(obj, -3*dir, ang1, 400, 4);}
			ascent(i in 0..6){let obj = CreateShotA1(width/2-40*j, height, 4, -115, 333, 3*i); controlWvel(obj, 3*dir, ang2, 400, 4);}
			j+= inc;
			if(j == 5){inc*=-1;}
		}
		else{ascent(i in 0..6){let obj = CreateShotA1(width/2, height, 4, -115, 333, 3*i); controlWvel(obj, 3, 20, 400, 4);} inc*=-1; j+= inc;}
		wait(40);
	}
}*/
		

task balls{
    let horn =LoadEffect(GetCurrentScriptDirectory~ "./../SFX/Horn.WAV", 70);
	let shot =LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK4.WAV", 60);
    let goUp = false;
	if(rand(0,1)>=1/2){goUp = true;}
	let graphic;
	if(rand(0,1)>= 1/2){graphic = [365, 368];}
	else{graphic = [367,369];}
	let cycle =0;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	    ObjSound_Play(horn);
	    ascent(i in 0..4){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}DelayLaser(170, 220, 90*i, 300, 25, 13, 60);}
		wait(60);
		ObjSound_Play(shot);
		if(goUp){
		    ascent(i in 0..9){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    let obja = CreateShotA1(170, 20+50*i, 0, 0, graphic[0], 20);
				ObjRender_SetAlpha(obja, 150);
				Obj_SetRenderPriorityI(obja, 70);
				spawn(obja, i, true, graphic, cycle);
			}
		    ascent(i in 0..8){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			     let obja=CreateShotA1(20+50*i, 220, 0, 0, graphic[(i+1)%2], 20); ObjRender_SetAlpha(obja, 150); Obj_SetRenderPriorityI(obja, 70);}
		}
		else{
		    ascent(i in 0..8){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    let obja = CreateShotA1(20+50*i, 220, 0, 0, graphic[0], 20);
				ObjRender_SetAlpha(obja, 150);
				Obj_SetRenderPriorityI(obja, 70);
				spawn(obja, i, false, graphic, cycle);
			}
			ascent(i in 0..9){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    let obja = CreateShotA1(170, 20+50*i, 0, 0, graphic[i%2], 10); ObjRender_SetAlpha(obja, 150); Obj_SetRenderPriorityI(obja, 70);}
		}
		wait(420);
		DeleteShotAll(TYPE_SHOT, TYPE_FADE);
		goUp= !goUp;
		cycle++;
	}
}
	
task spawn(obj, i, goUp, graphic, cycle){
    let horn =LoadEffect(GetCurrentScriptDirectory~ "./../SFX/Horn.WAV", 70);
    let shot =LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK4.WAV", 60);
	if(goUp){
		wait(60);
		ObjSound_Play(horn);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		DelayLaser(0, ObjMove_GetY(obj), 0, 500, 25, 13, 60);
		wait(30);
		ascent(j in 1..5){DelayLaser(ObjMove_GetX(obj)+50*j, 0, 90, 500, 25, 13, 60); DelayLaser(ObjMove_GetX(obj)-50*j, 0, 90, 500, 25, 13, 60);}
		wait(30);
		let obj3=CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 0, 0, graphic[0], 20);
		if(cycle%2 == 0){up(obj3, false, 100);} else{up(obj3, true, 100);}
		ascent(j in 1..5){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    ObjSound_Play(shot);
			let obj1=CreateShotA1(ObjMove_GetX(obj) + 50*j, ObjMove_GetY(obj), 0, 0, graphic[j%2], 20);
			let obj2=CreateShotA1(ObjMove_GetX(obj) - 50*j, ObjMove_GetY(obj), 0, 0, graphic[j%2], 20);
			ObjRender_SetAlpha(obj1, 150); ObjRender_SetAlpha(obj2, 150); Obj_SetRenderPriorityI(obj1, 70); Obj_SetRenderPriorityI(obj2, 70);
			if(j%2==1 && cycle%2 == 0){up(obj1, true, 100-10*j); up(obj2, true, 100-10*j);}
			else if(j%2 == 1 && cycle%2 == 1){up(obj1, false, 100-10*j); up(obj2, false, 100-10*j);}
			else if(j%2 == 0 && cycle%2 == 0){up(obj1, false, 100-10*j); up(obj2, false, 100-10*j);}
			else{up(obj1, true, 100-10*j); up(obj2, true, 100-10*j);}
			wait(10);
		}
	}
	else{
	    wait(60);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(horn);
		DelayLaser(ObjMove_GetX(obj), 0, 90, 500, 25, 13, 60);
		wait(30);
		ascent(j in 1..5){DelayLaser(0, ObjMove_GetY(obj)+50*j, 0, 500, 25, 13, 60); DelayLaser(0, ObjMove_GetY(obj)-50*j, 0, 500, 25, 13, 60);}
		wait(30);
		let obj3=CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 0, 0, graphic[0], 20);
		if(cycle%2 == 0){right(obj3, false, 100);} else{right(obj3, true, 100);}
		ascent(j in 1..5){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    ObjSound_Play(shot);
			let obj1=CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj)+50*j, 0, 0, graphic[j%2], 20);
			let obj2=CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj)-50*j, 0, 0, graphic[j%2], 20);
			ObjRender_SetAlpha(obj1, 150); ObjRender_SetAlpha(obj2, 150); Obj_SetRenderPriorityI(obj1, 70); Obj_SetRenderPriorityI(obj2, 70);
			if(j%2==1 && cycle%2 == 0){right(obj1, true, 100-10*j); right(obj2, true, 100-10*j);}
			else if(j%2 == 1 && cycle%2 == 1){right(obj1, false, 100-10*j); right(obj2, false, 100-10*j);}
			else if(j%2 == 0 && cycle%2 == 0){right(obj1, false, 100-10*j); right(obj2, false, 100-10*j);}
			else{right(obj1, true, 100-10*j); right(obj2, true, 100-10*j);}
			wait(10);
		}
	}	
}

	
task up(obj, yes, delay){
    let t =LoadEffect(GetCurrentScriptDirectory~ "./../SFX/Wave.WAV", 70);
    wait(delay);
	ObjSound_Play(t);
    if(yes){ObjMove_AddPatternA1(obj, 0, 2, -90);}
	else{ObjMove_AddPatternA1(obj, 0, 2, 90);}
}
	
task right(obj, yes, delay){
    let horn =LoadEffect(GetCurrentScriptDirectory~ "./../SFX/Wave.WAV", 70);
	wait(delay);
	ObjSound_Play(horn);
    if(yes){ObjMove_AddPatternA1(obj, 0, 2, 0);}
	else{ObjMove_AddPatternA1(obj, 0, 2, 180);}
}
	
	
	